Author: niallp
Date: Thu Dec 4 16:23:20 2008
New Revision: 723507
URL: http://svn.apache.org/viewvc?rev=723507&view=rev
Log:
IO-137 Added method for getting InputStream from ByteArrayOutputStream &
IOUtils avoiding unnecessary array allocation and copy - thanks to for the patch
Modified:
commons/proper/io/trunk/src/java/org/apache/commons/io/IOUtils.java
commons/proper/io/trunk/src/java/org/apache/commons/io/output/ByteArrayOutputStream.java
commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsTestCase.java
Modified: commons/proper/io/trunk/src/java/org/apache/commons/io/IOUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/IOUtils.java?rev=723507&r1=723506&r2=723507&view=diff
==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/IOUtils.java
(original)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/IOUtils.java Thu Dec
4 16:23:20 2008
@@ -219,6 +219,31 @@
}
}
+ /**
+ * Fetches entire contents of an <code>InputStream</code> and represent
+ * same data as result InputStream.
+ * <p>
+ * This method is useful where,
+ * <ul>
+ * <li>Source InputStream is slow.</li>
+ * <li>It has network resources associated, so we cannot keep it open for
+ * long time.</li>
+ * <li>It has network timeout associated.</li>
+ * </ul>
+ * It can be used in favor of [EMAIL PROTECTED]
#toByteArray(InputStream)}, since it
+ * avoids unnecessary allocation and copy of byte[].<br>
+ * This method buffers the input internally, so there is no need to use a
+ * <code>BufferedInputStream</code>.
+ *
+ * @param input Stream to be fully buffered.
+ * @return A fully buffered stream.
+ * @throws IOException if an I/O error occurs
+ * @since Commons IO 2.0
+ */
+ public static InputStream toBufferedInputStream(InputStream input) throws
IOException {
+ return ByteArrayOutputStream.toBufferedInputStream(input);
+ }
+
// read toByteArray
//-----------------------------------------------------------------------
/**
Modified:
commons/proper/io/trunk/src/java/org/apache/commons/io/output/ByteArrayOutputStream.java
URL:
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/output/ByteArrayOutputStream.java?rev=723507&r1=723506&r2=723507&view=diff
==============================================================================
---
commons/proper/io/trunk/src/java/org/apache/commons/io/output/ByteArrayOutputStream.java
(original)
+++
commons/proper/io/trunk/src/java/org/apache/commons/io/output/ByteArrayOutputStream.java
Thu Dec 4 16:23:20 2008
@@ -16,12 +16,16 @@
*/
package org.apache.commons.io.output;
+import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.io.SequenceInputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
+import org.apache.commons.io.input.ClosedInputStream;
/**
* This class implements an output stream in which the data is
@@ -249,6 +253,61 @@
}
/**
+ * Fetches entire contents of an <code>InputStream</code> and represent
+ * same data as result InputStream.
+ * <p>
+ * This method is useful where,
+ * <ul>
+ * <li>Source InputStream is slow.</li>
+ * <li>It has network resources associated, so we cannot keep it open for
+ * long time.</li>
+ * <li>It has network timeout associated.</li>
+ * </ul>
+ * It can be used in favor of [EMAIL PROTECTED] #toByteArray()}, since it
+ * avoids unnecessary allocation and copy of byte[].<br>
+ * This method buffers the input internally, so there is no need to use a
+ * <code>BufferedInputStream</code>.
+ *
+ * @param input Stream to be fully buffered.
+ * @return A fully buffered stream.
+ * @throws IOException if an I/O error occurs
+ */
+ public static InputStream toBufferedInputStream(InputStream input)
+ throws IOException {
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
+ output.write(input);
+ return output.toBufferedInputStream();
+ }
+
+ /**
+ * Gets the current contents of this byte stream as a Input Stream. The
+ * returned stream is backed by buffers of <code>this</code> stream,
+ * avoiding memory allocation and copy, thus saving space and time.<br>
+ *
+ * @return the current contents of this output stream.
+ * @see java.io.ByteArrayOutputStream#toByteArray()
+ * @see #reset()
+ * @since Commons IO 2.0
+ */
+ private InputStream toBufferedInputStream() {
+ int remaining = count;
+ if (remaining == 0) {
+ return new ClosedInputStream();
+ }
+ List<ByteArrayInputStream> list = new
ArrayList<ByteArrayInputStream>(buffers.size());
+ for (int i = 0; i < buffers.size(); i++) {
+ byte[] buf = buffers.get(i);
+ int c = Math.min(buf.length, remaining);
+ list.add(new ByteArrayInputStream(buf, 0, c));
+ remaining -= c;
+ if (remaining == 0) {
+ break;
+ }
+ }
+ return new SequenceInputStream(Collections.enumeration(list));
+ }
+
+ /**
* Gets the curent contents of this byte stream as a byte array.
* The result is independent of this stream.
*
Modified:
commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsTestCase.java
URL:
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsTestCase.java?rev=723507&r1=723506&r2=723507&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsTestCase.java
(original)
+++ commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsTestCase.java
Thu Dec 4 16:23:20 2008
@@ -222,6 +222,21 @@
}
}
+ public void testInputStreamToBufferedInputStream() throws Exception {
+ FileInputStream fin = new FileInputStream(m_testFile);
+ try {
+ InputStream in = IOUtils.toBufferedInputStream(fin);
+ byte[] out = IOUtils.toByteArray(in);
+ assertNotNull(out);
+ assertTrue("Not all bytes were read", fin.available() == 0);
+ assertTrue("Wrong output size: out.length=" + out.length + "!="
+ + FILE_SIZE, out.length == FILE_SIZE);
+ assertEqualContent(out, m_testFile);
+ } finally {
+ fin.close();
+ }
+ }
+
public void testStringToByteArray()
throws Exception
{
@@ -443,5 +458,4 @@
deleteFile(file);
}
}
-
}