26
27
import java.io.Writer;
30
30
* This class provides static utility methods for buffered
31
* copying between sources (<code>InputStream</code>, <code>Reader</code>, <code>String</code> and
32
* <code>byte[]</code>) and destinations (<code>OutputStream</code>, <code>Writer</code>,
33
* <code>String</code> and <code>byte[]</code>).
36
* <p>Unless otherwise noted, these <code>copy</code> methods do <em>not</em> flush or close the
37
* streams. Often doing so would require making non-portable assumptions about the streams' origin
38
* and further use. This means that both streams' <code>close()</code> methods must be called after
39
* copying. if one omits this step, then the stream resources (sockets, file descriptors) are
40
* released when the associated Stream is garbage-collected. It is not a good idea to rely on this
41
* mechanism. For a good overview of the distinction between "memory management" and "resource
42
* management", see <a href="http://www.unixreview.com/articles/1998/9804/9804ja/ja.htm">this
43
* UnixReview article</a>.</p>
45
* <p>For byte-to-char methods, a <code>copy</code> variant allows the encoding
46
* to be selected (otherwise the platform default is used). We would like to
31
* copying between sources (<code>InputStream</code>, <code>Reader</code>,
32
* <code>String</code> and <code>byte[]</code>) and destinations
33
* (<code>OutputStream</code>, <code>Writer</code>, <code>String</code> and
34
* <code>byte[]</code>).
36
* Unless otherwise noted, these <code>copy</code> methods do <em>not</em>
37
* flush or close the streams. Often doing so would require making non-portable
38
* assumptions about the streams' origin and further use. This means that both
39
* streams' <code>close()</code> methods must be called after copying. if one
40
* omits this step, then the stream resources (sockets, file descriptors) are
41
* released when the associated Stream is garbage-collected. It is not a good
42
* idea to rely on this mechanism. For a good overview of the distinction
43
* between "memory management" and "resource management", see
44
* <a href="http://www.unixreview.com/articles/1998/9804/9804ja/ja.htm">this
45
* UnixReview article</a>.
47
* For byte-to-char methods, a <code>copy</code> variant allows the encoding
48
* to be selected (otherwise the platform default is used). We would like to
47
49
* encourage you to always specify the encoding because relying on the platform
48
* default can lead to unexpected results.</p>
50
* <p>We don't provide special variants for the <code>copy</code> methods that
50
* default can lead to unexpected results.
52
* We don't provide special variants for the <code>copy</code> methods that
51
53
* let you specify the buffer size because in modern VMs the impact on speed
52
* seems to be minimal. We're using a default buffer size of 4 KB.</p>
54
* <p>The <code>copy</code> methods use an internal buffer when copying. It is therefore advisable
55
* <em>not</em> to deliberately wrap the stream arguments to the <code>copy</code> methods in
56
* <code>Buffered*</code> streams. For example, don't do the
59
* <code>copy( new BufferedInputStream( in ), new BufferedOutputStream( out ) );</code>
61
* <p>The rationale is as follows:</p>
63
* <p>Imagine that an InputStream's read() is a very expensive operation, which would usually suggest
64
* wrapping in a BufferedInputStream. The BufferedInputStream works by issuing infrequent
65
* {@link java.io.InputStream#read(byte[] b, int off, int len)} requests on the underlying InputStream, to
66
* fill an internal buffer, from which further <code>read</code> requests can inexpensively get
67
* their data (until the buffer runs out).</p>
68
* <p>However, the <code>copy</code> methods do the same thing, keeping an internal buffer,
69
* populated by {@link InputStream#read(byte[] b, int off, int len)} requests. Having two buffers
70
* (or three if the destination stream is also buffered) is pointless, and the unnecessary buffer
71
* management hurts performance slightly (about 3%, according to some simple experiments).</p>
73
* <p>Behold, intrepid explorers; a map of this class:</p>
54
* seems to be minimal. We're using a default buffer size of 4 KB.
56
* The <code>copy</code> methods use an internal buffer when copying. It is
57
* therefore advisable <em>not</em> to deliberately wrap the stream arguments
58
* to the <code>copy</code> methods in <code>Buffered*</code> streams. For
59
* example, don't do the following:
61
* copy( new BufferedInputStream( in ), new BufferedOutputStream( out ) );
63
* The rationale is as follows:
65
* Imagine that an InputStream's read() is a very expensive operation, which
66
* would usually suggest wrapping in a BufferedInputStream. The
67
* BufferedInputStream works by issuing infrequent
68
* {@link java.io.InputStream#read(byte[] b, int off, int len)} requests on the
69
* underlying InputStream, to fill an internal buffer, from which further
70
* <code>read</code> requests can inexpensively get their data (until the buffer
73
* However, the <code>copy</code> methods do the same thing, keeping an
74
* internal buffer, populated by
75
* {@link InputStream#read(byte[] b, int off, int len)} requests. Having two
76
* buffers (or three if the destination stream is also buffered) is pointless,
77
* and the unnecessary buffer management hurts performance slightly (about 3%,
78
* according to some simple experiments).
80
* Behold, intrepid explorers; a map of this class:
75
82
* Method Input Output Dependency
76
83
* ------ ----- ------ -------
87
94
* 7 copy byte[] Writer 3
88
95
* 8 copy byte[] OutputStream (trivial)
91
* <p>Note that only the first two methods shuffle bytes; the rest use these
98
* Note that only the first two methods shuffle bytes; the rest use these
92
99
* two, or (if possible) copy using native Java copy methods. As there are
93
100
* method variants to specify the encoding, each row may
94
* correspond to up to 2 methods.</p>
96
* <p>Origin of code: Apache Avalon (Excalibur)</p>
101
* correspond to up to 2 methods.
103
* Origin of code: Excalibur.
98
105
* @author Peter Donald
99
106
* @author Jeff Turner
100
107
* @author Matthew Hawthorne
101
* @version $Id: CopyUtils.java,v 1.6 2004/04/24 23:49:25 bayard Exp $
108
* @version $Id: CopyUtils.java 437680 2006-08-28 11:57:00Z scolebourne $
109
* @deprecated Use IOUtils. Will be removed in 2.0.
110
* Methods renamed to IOUtils.write() or IOUtils.copy().
111
* Null handling behaviour changed in IOUtils (null data does not
112
* throw NullPointerException).
103
114
public class CopyUtils {
106
* The name says it all.
117
* The default size of the buffer.
108
119
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
111
122
* Instances should NOT be constructed in standard programming.
113
public CopyUtils() {}
124
public CopyUtils() { }
115
126
// ----------------------------------------------------------------
116
127
// byte[] -> OutputStream