This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-io.git
The following commit(s) were added to refs/heads/master by this push: new 139cc89 Add comment and use try-with-resources. 139cc89 is described below commit 139cc8969ff21e9842ea436aa556027823cc73e1 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sun May 24 14:26:09 2020 -0400 Add comment and use try-with-resources. --- .../apache/commons/io/output/NullPrintStream.java | 1 + .../UnsynchronizedByteArrayOutputStream.java | 57 ++++++++-------------- 2 files changed, 21 insertions(+), 37 deletions(-) diff --git a/src/main/java/org/apache/commons/io/output/NullPrintStream.java b/src/main/java/org/apache/commons/io/output/NullPrintStream.java index 8942a5b..f5ddcf3 100644 --- a/src/main/java/org/apache/commons/io/output/NullPrintStream.java +++ b/src/main/java/org/apache/commons/io/output/NullPrintStream.java @@ -38,6 +38,7 @@ public class NullPrintStream extends PrintStream { * Constructs an instance. */ public NullPrintStream() { + // Relies on the default charset which is OK since we are not writing. super(NullOutputStream.NULL_OUTPUT_STREAM); } diff --git a/src/main/java/org/apache/commons/io/output/UnsynchronizedByteArrayOutputStream.java b/src/main/java/org/apache/commons/io/output/UnsynchronizedByteArrayOutputStream.java index 7e135ee..77b167e 100644 --- a/src/main/java/org/apache/commons/io/output/UnsynchronizedByteArrayOutputStream.java +++ b/src/main/java/org/apache/commons/io/output/UnsynchronizedByteArrayOutputStream.java @@ -23,8 +23,7 @@ import java.io.InputStream; 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 final class UnsynchronizedByteArrayOutputStream extends AbstractByteArray } /** - * 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 + * @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,60 +90,50 @@ public final class UnsynchronizedByteArrayOutputStream extends AbstractByteArray } /** - * 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); - output.write(input); - return output.toInputStream(); + try (final UnsynchronizedByteArrayOutputStream output = new UnsynchronizedByteArrayOutputStream(size)) { + output.write(input); + return output.toInputStream(); + } } @Override