Skip to content

Commit 03027d8

Browse files
committed
[IO-353] Add API IOUtils.copy(InputStream, OutputStream, int).
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1402280 13f79535-47bb-0310-9956-ffa450edef68
1 parent 965f666 commit 03027d8

3 files changed

Lines changed: 61 additions & 1 deletion

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ The <action> type attribute can be add,update,fix,remove.
4747
<body>
4848
<!-- The release date is the date RC is cut -->
4949
<release version="2.5" date="201?-??-??" description="New features and bug fixes.">
50+
<action issue="IO-353" dev="ggregory" type="add" due-to="ggregory">
51+
Add API IOUtils.copy(InputStream, OutputStream, int)
52+
</action>
5053
<action issue="IO-349" dev="ggregory" type="add" due-to="scop">
5154
Add API with array offset and length argument to FileUtils.writeByteArrayToFile.
5255
</action>

src/main/java/org/apache/commons/io/IOUtils.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1748,6 +1748,30 @@ public static int copy(InputStream input, OutputStream output) throws IOExceptio
17481748
return (int) count;
17491749
}
17501750

1751+
/**
1752+
* Copy bytes from an <code>InputStream</code> to an <code>OutputStream</code> using an internal buffer of the
1753+
* given size.
1754+
* <p>
1755+
* This method buffers the input internally, so there is no need to use a <code>BufferedInputStream</code>.
1756+
* <p>
1757+
*
1758+
* @param input
1759+
* the <code>InputStream</code> to read from
1760+
* @param output
1761+
* the <code>OutputStream</code> to write to
1762+
* @param bufferSize
1763+
* the bufferSize used to copy from the input to the output
1764+
* @return the number of bytes copied
1765+
* @throws NullPointerException
1766+
* if the input or output is null
1767+
* @throws IOException
1768+
* if an I/O error occurs
1769+
* @since 2.5
1770+
*/
1771+
public static long copy(InputStream input, OutputStream output, int bufferSize) throws IOException {
1772+
return copyLarge(input, output, new byte[bufferSize]);
1773+
}
1774+
17511775
/**
17521776
* Copy bytes from a large (over 2GB) <code>InputStream</code> to an
17531777
* <code>OutputStream</code>.
@@ -1766,7 +1790,7 @@ public static int copy(InputStream input, OutputStream output) throws IOExceptio
17661790
*/
17671791
public static long copyLarge(InputStream input, OutputStream output)
17681792
throws IOException {
1769-
return copyLarge(input, output, new byte[DEFAULT_BUFFER_SIZE]);
1793+
return copy(input, output, DEFAULT_BUFFER_SIZE);
17701794
}
17711795

17721796
/**

src/test/java/org/apache/commons/io/IOUtilsCopyTestCase.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,39 @@ public void testCopy_inputStreamToOutputStream() throws Exception {
8686
assertEquals(inData.length,count);
8787
}
8888

89+
public void testCopy_inputStreamToOutputStreamWithBufferSize() throws Exception {
90+
testCopy_inputStreamToOutputStreamWithBufferSize(1);
91+
testCopy_inputStreamToOutputStreamWithBufferSize(2);
92+
testCopy_inputStreamToOutputStreamWithBufferSize(4);
93+
testCopy_inputStreamToOutputStreamWithBufferSize(8);
94+
testCopy_inputStreamToOutputStreamWithBufferSize(16);
95+
testCopy_inputStreamToOutputStreamWithBufferSize(32);
96+
testCopy_inputStreamToOutputStreamWithBufferSize(64);
97+
testCopy_inputStreamToOutputStreamWithBufferSize(128);
98+
testCopy_inputStreamToOutputStreamWithBufferSize(256);
99+
testCopy_inputStreamToOutputStreamWithBufferSize(512);
100+
testCopy_inputStreamToOutputStreamWithBufferSize(1024);
101+
testCopy_inputStreamToOutputStreamWithBufferSize(2048);
102+
testCopy_inputStreamToOutputStreamWithBufferSize(4096);
103+
testCopy_inputStreamToOutputStreamWithBufferSize(8192);
104+
testCopy_inputStreamToOutputStreamWithBufferSize(16384);
105+
}
106+
107+
private void testCopy_inputStreamToOutputStreamWithBufferSize(int bufferSize) throws Exception {
108+
InputStream in = new ByteArrayInputStream(inData);
109+
in = new YellOnCloseInputStream(in);
110+
111+
ByteArrayOutputStream baout = new ByteArrayOutputStream();
112+
OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
113+
114+
long count = IOUtils.copy(in, out, bufferSize);
115+
116+
assertEquals("Not all bytes were read", 0, in.available());
117+
assertEquals("Sizes differ", inData.length, baout.size());
118+
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
119+
assertEquals(inData.length,count);
120+
}
121+
89122
public void testCopy_inputStreamToOutputStream_nullIn() throws Exception {
90123
OutputStream out = new ByteArrayOutputStream();
91124
try {

0 commit comments

Comments
 (0)