Add comment and use try-with-resources.
MODIFY
src/main/java/org/apache/commons/io/output/NullPrintStream.java
src/main/java/org/apache/commons/io/output/NullPrintStream.java
* Constructs an instance.
*/
public NullPrintStream() {
+ // Relies on the default charset which is OK since we are not writing.
super(NullOutputStream.NULL_OUTPUT_STREAM);
}

MODIFY
src/main/java/org/apache/commons/io/output/UnsynchronizedByteArrayOutputStream.java
src/main/java/org/apache/commons/io/output/UnsynchronizedByteArrayOutputStream.java
import java.io.OutputStream;
/**
- * Implements a version of {@link AbstractByteArrayOutputStream}
- * <b>without</b> any concurrent thread safety.
+ * Implements a version of {@link AbstractByteArrayOutputStream} <b>without</b> any concurrent thread safety.
*
* @since 2.7
*/
@@ -40,28 +39,22 @@ public UnsynchronizedByteArrayOutputStream() {
}
/**
- * Creates a new byte array output stream, with a buffer capacity of
- * the specified size, in bytes.
+ * Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
*
* @param size the initial size
* @throws IllegalArgumentException if size is negative
*/
public UnsynchronizedByteArrayOutputStream(final int size) {
if (size < 0) {
- throw new IllegalArgumentException(
- "Negative initial size: " + size);
+ throw new IllegalArgumentException("Negative initial size: " + size);
}
needNewBuffer(size);
}
@Override
public void write(final byte[] b, final int off, final int len) {
- if ((off < 0)
- || (off > b.length)
- || (len < 0)
- || ((off + len) > b.length)
- || ((off + len) < 0)) {
- throw new IndexOutOfBoundsException();
+ if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) {
+ throw new IndexOutOfBoundsException(String.format("offset=%,d, length=%,d", off, len));
} else if (len == 0) {
return;
}
@@ -97,61 +90,51 @@ public void writeTo(final OutputStream out) throws IOException {
}
/**
- * Fetches entire contents of an <code>InputStream</code> and represent
- * same data as result InputStream.
+ * Fetches entire contents of an <code>InputStream</code> and represent same data as result InputStream.
* <p>
* This method is useful where,
* </p>
* <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 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>.
+ * 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.
* @return A fully buffered stream.
* @throws IOException if an I/O error occurs
*/
- public static InputStream toBufferedInputStream(final InputStream input)
- throws IOException {
+ public static InputStream toBufferedInputStream(final InputStream input) throws IOException {
return toBufferedInputStream(input, DEFAULT_SIZE);
}
/**
- * Fetches entire contents of an <code>InputStream</code> and represent
- * same data as result InputStream.
+ * Fetches entire contents of an <code>InputStream</code> and represent same data as result InputStream.
* <p>
* This method is useful where,
* </p>
* <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 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>.
+ * 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
*/
- public static InputStream toBufferedInputStream(final InputStream input, final int size)
- throws IOException {
+ public static InputStream toBufferedInputStream(final InputStream input, final int size) throws IOException {
// It does not matter if a ByteArrayOutputStream is not closed as close() is a no-op
- @SuppressWarnings("resource")
- final UnsynchronizedByteArrayOutputStream output = new UnsynchronizedByteArrayOutputStream(size);
+ try (final UnsynchronizedByteArrayOutputStream output = new UnsynchronizedByteArrayOutputStream(size)) {
output.write(input);
return output.toInputStream();
}
+ }
@Override
public InputStream toInputStream() {

Add comment and use try-with-resources.