You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by br...@apache.org on 2013/10/25 04:42:12 UTC
svn commit: r1535612 - in /commons/proper/io/trunk/src: changes/changes.xml
main/java/org/apache/commons/io/IOUtils.java
main/java/org/apache/commons/io/output/ByteArrayOutputStream.java
test/java/org/apache/commons/io/IOUtilsTestCase.java
Author: brentworden
Date: Fri Oct 25 02:42:12 2013
New Revision: 1535612
URL: http://svn.apache.org/r1535612
Log:
IO-395 overload IOUtils buffer methods to accept buffer size.
Modified:
commons/proper/io/trunk/src/changes/changes.xml
commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java
commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/ByteArrayOutputStream.java
commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsTestCase.java
Modified: commons/proper/io/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/changes/changes.xml?rev=1535612&r1=1535611&r2=1535612&view=diff
==============================================================================
--- commons/proper/io/trunk/src/changes/changes.xml (original)
+++ commons/proper/io/trunk/src/changes/changes.xml Fri Oct 25 02:42:12 2013
@@ -47,6 +47,9 @@ The <action> type attribute can be add,u
<body>
<!-- The release date is the date RC is cut -->
<release version="2.5" date="2013-??-??" description="New features and bug fixes.">
+ <action issue="IO-395" dev="brentworden" type="add" due-to="BELUGA BEHR">
+ Overload IOUtils buffer methods to accept buffer size
+ </action>
<action issue="IO-389" dev="sebb" type="fix" due-to="Austin Doupnik">
FileUtils.sizeOfDirectory can throw IllegalArgumentException
</action>
Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java?rev=1535612&r1=1535611&r2=1535612&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java Fri Oct 25 02:42:12 2013
@@ -436,6 +436,32 @@ public class IOUtils {
}
/**
+ * 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 {@link #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.
+ * @param size the initial buffer size
+ * @return A fully buffered stream.
+ * @throws IOException if an I/O error occurs
+ * @since 2.5
+ */
+ public static InputStream toBufferedInputStream(final InputStream input, int size) throws IOException {
+ return ByteArrayOutputStream.toBufferedInputStream(input, size);
+ }
+
+ /**
* Returns the given reader if it is a {@link BufferedReader}, otherwise creates a BufferedReader from the given
* reader.
*
@@ -451,6 +477,22 @@ public class IOUtils {
}
/**
+ * Returns the given reader if it is a {@link BufferedReader}, otherwise creates a BufferedReader from the given
+ * reader.
+ *
+ * @param reader
+ * the reader to wrap or return (not null)
+ * @param size the buffer size, if a new BufferedReader is created.
+ * @return the given reader or a new {@link BufferedReader} for the given reader
+ * @since 2.5
+ * @see #buffer(Reader)
+ * @throws NullPointerException if the input parameter is null
+ */
+ public static BufferedReader toBufferedReader(final Reader reader, int size) {
+ return reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader, size);
+ }
+
+ /**
* Returns the given reader if it is already a {@link BufferedReader}, otherwise creates a BufferedReader from the given
* reader.
*
@@ -465,6 +507,21 @@ public class IOUtils {
}
/**
+ * Returns the given reader if it is already a {@link BufferedReader}, otherwise creates a BufferedReader from the given
+ * reader.
+ *
+ * @param reader
+ * the reader to wrap or return (not null)
+ * @param size the buffer size, if a new BufferedReader is created.
+ * @return the given reader or a new {@link BufferedReader} for the given reader
+ * @since 2.5
+ * @throws NullPointerException if the input parameter is null
+ */
+ public static BufferedReader buffer(final Reader reader, int size) {
+ return reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader, size);
+ }
+
+ /**
* Returns the given Writer if it is already a {@link BufferedWriter}, otherwise creates a BufferedWriter from the given
* Writer.
*
@@ -479,6 +536,21 @@ public class IOUtils {
}
/**
+ * Returns the given Writer if it is already a {@link BufferedWriter}, otherwise creates a BufferedWriter from the given
+ * Writer.
+ *
+ * @param writer
+ * the Writer to wrap or return (not null)
+ * @param size the buffer size, if a new BufferedWriter is created.
+ * @return the given Writer or a new {@link BufferedWriter} for the given Writer
+ * @since 2.5
+ * @throws NullPointerException if the input parameter is null
+ */
+ public static BufferedWriter buffer(final Writer writer, int size) {
+ return writer instanceof BufferedWriter ? (BufferedWriter) writer : new BufferedWriter(writer, size);
+ }
+
+ /**
* Returns the given OutputStream if it is already a {@link BufferedOutputStream}, otherwise creates a BufferedOutputStream from the given
* OutputStream.
*
@@ -497,6 +569,25 @@ public class IOUtils {
}
/**
+ * Returns the given OutputStream if it is already a {@link BufferedOutputStream}, otherwise creates a BufferedOutputStream from the given
+ * OutputStream.
+ *
+ * @param outputStream
+ * the OutputStream to wrap or return (not null)
+ * @param size the buffer size, if a new BufferedOutputStream is created.
+ * @return the given OutputStream or a new {@link BufferedOutputStream} for the given OutputStream
+ * @since 2.5
+ * @throws NullPointerException if the input parameter is null
+ */
+ public static BufferedOutputStream buffer(final OutputStream outputStream, int size) {
+ // reject null early on rather than waiting for IO operation to fail
+ if (outputStream == null) { // not checked by BufferedOutputStream
+ throw new NullPointerException();
+ }
+ return outputStream instanceof BufferedOutputStream ? (BufferedOutputStream) outputStream : new BufferedOutputStream(outputStream, size);
+ }
+
+ /**
* Returns the given InputStream if it is already a {@link BufferedInputStream}, otherwise creates a BufferedInputStream from the given
* InputStream.
*
@@ -514,6 +605,25 @@ public class IOUtils {
return inputStream instanceof BufferedInputStream ? (BufferedInputStream) inputStream : new BufferedInputStream(inputStream);
}
+ /**
+ * Returns the given InputStream if it is already a {@link BufferedInputStream}, otherwise creates a BufferedInputStream from the given
+ * InputStream.
+ *
+ * @param inputStream
+ * the InputStream to wrap or return (not null)
+ * @param size the buffer size, if a new BufferedInputStream is created.
+ * @return the given InputStream or a new {@link BufferedInputStream} for the given InputStream
+ * @since 2.5
+ * @throws NullPointerException if the input parameter is null
+ */
+ public static BufferedInputStream buffer(final InputStream inputStream, int size) {
+ // reject null early on rather than waiting for IO operation to fail
+ if (inputStream == null) { // not checked by BufferedInputStream
+ throw new NullPointerException();
+ }
+ return inputStream instanceof BufferedInputStream ? (BufferedInputStream) inputStream : new BufferedInputStream(inputStream, size);
+ }
+
// read toByteArray
//-----------------------------------------------------------------------
/**
Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/ByteArrayOutputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/ByteArrayOutputStream.java?rev=1535612&r1=1535611&r2=1535612&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/ByteArrayOutputStream.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/ByteArrayOutputStream.java Fri Oct 25 02:42:12 2013
@@ -287,9 +287,36 @@ public class ByteArrayOutputStream exten
*/
public static InputStream toBufferedInputStream(final InputStream input)
throws IOException {
+ return toBufferedInputStream(input, 1024);
+ }
+
+ /**
+ * 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 {@link #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.
+ * @param size the initial buffer size
+ * @return A fully buffered stream.
+ * @throws IOException if an I/O error occurs
+ * @since 2.5
+ */
+ public static InputStream toBufferedInputStream(final InputStream input, int size)
+ throws IOException {
// It does not matter if a ByteArrayOutputStream is not closed as close() is a no-op
@SuppressWarnings("resource")
- final ByteArrayOutputStream output = new ByteArrayOutputStream();
+ final ByteArrayOutputStream output = new ByteArrayOutputStream(size);
output.write(input);
return output.toInputStream();
}
Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsTestCase.java?rev=1535612&r1=1535611&r2=1535612&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsTestCase.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsTestCase.java Fri Oct 25 02:42:12 2013
@@ -908,6 +908,20 @@ public class IOUtilsTestCase extends Fil
}
}
+ public void testToBufferedInputStreamWithBufferSize_InputStream() throws Exception {
+ final FileInputStream fin = new FileInputStream(m_testFile);
+ try {
+ final InputStream in = IOUtils.toBufferedInputStream(fin, 2048);
+ final byte[] out = IOUtils.toByteArray(in);
+ assertNotNull(out);
+ assertEquals("Not all bytes were read", 0, fin.available());
+ assertEquals("Wrong output size", FILE_SIZE, out.length);
+ assertEqualContent(out, m_testFile);
+ } finally {
+ fin.close();
+ }
+ }
+
public void testToByteArray_InputStream() throws Exception {
final FileInputStream fin = new FileInputStream(m_testFile);
try {
@@ -1249,6 +1263,19 @@ public class IOUtilsTestCase extends Fil
assertSame(bis, IOUtils.buffer(bis));
}
+ public void testAsBufferedInputStreamWithBufferSize() {
+ InputStream is = new InputStream() {
+ @Override
+ public int read() throws IOException {
+ return 0;
+ }
+ };
+ final BufferedInputStream bis = IOUtils.buffer(is, 2048);
+ assertNotSame(is, bis);
+ assertSame(bis, IOUtils.buffer(bis));
+ assertSame(bis, IOUtils.buffer(bis, 1024));
+ }
+
public void testAsBufferedOutputStream() {
OutputStream is = new OutputStream() {
@Override
@@ -1259,6 +1286,17 @@ public class IOUtilsTestCase extends Fil
assertSame(bis, IOUtils.buffer(bis));
}
+ public void testAsBufferedOutputStreamWithBufferSize() {
+ OutputStream os = new OutputStream() {
+ @Override
+ public void write(int b) throws IOException { }
+ };
+ final BufferedOutputStream bos = IOUtils.buffer(os, 2048);
+ assertNotSame(os, bos);
+ assertSame(bos, IOUtils.buffer(bos));
+ assertSame(bos, IOUtils.buffer(bos, 1024));
+ }
+
public void testAsBufferedReader() {
Reader is = new Reader() {
@Override
@@ -1273,6 +1311,21 @@ public class IOUtilsTestCase extends Fil
assertSame(bis, IOUtils.buffer(bis));
}
+ public void testAsBufferedReaderWithBufferSize() {
+ Reader r = new Reader() {
+ @Override
+ public int read(char[] cbuf, int off, int len) throws IOException {
+ return 0;
+ }
+ @Override
+ public void close() throws IOException { }
+ };
+ final BufferedReader br = IOUtils.buffer(r, 2048);
+ assertNotSame(r, br);
+ assertSame(br, IOUtils.buffer(br));
+ assertSame(br, IOUtils.buffer(br, 1024));
+ }
+
public void testAsBufferedWriter() {
Writer is = new Writer() {
@Override
@@ -1291,4 +1344,24 @@ public class IOUtilsTestCase extends Fil
assertNotSame(is, bis);
assertSame(bis, IOUtils.buffer(bis));
}
+
+ public void testAsBufferedWriterWithBufferSize() {
+ Writer w = new Writer() {
+ @Override
+ public void write(int b) throws IOException { }
+
+ @Override
+ public void write(char[] cbuf, int off, int len) throws IOException { }
+
+ @Override
+ public void flush() throws IOException { }
+
+ @Override
+ public void close() throws IOException { }
+ };
+ final BufferedWriter bw = IOUtils.buffer(w, 2024);
+ assertNotSame(w, bw);
+ assertSame(bw, IOUtils.buffer(bw));
+ assertSame(bw, IOUtils.buffer(bw, 1024));
+ }
}