2323import java .io .OutputStream ;
2424
2525/**
26- * Implements a version of {@link AbstractByteArrayOutputStream}
27- * <b>without</b> any concurrent thread safety.
26+ * Implements a version of {@link AbstractByteArrayOutputStream} <b>without</b> any concurrent thread safety.
2827 *
2928 * @since 2.7
3029 */
@@ -40,28 +39,22 @@ public UnsynchronizedByteArrayOutputStream() {
4039 }
4140
4241 /**
43- * Creates a new byte array output stream, with a buffer capacity of
44- * the specified size, in bytes.
42+ * Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
4543 *
46- * @param size the initial size
44+ * @param size the initial size
4745 * @throws IllegalArgumentException if size is negative
4846 */
4947 public UnsynchronizedByteArrayOutputStream (final int size ) {
5048 if (size < 0 ) {
51- throw new IllegalArgumentException (
52- "Negative initial size: " + size );
49+ throw new IllegalArgumentException ("Negative initial size: " + size );
5350 }
5451 needNewBuffer (size );
5552 }
5653
5754 @ Override
5855 public void write (final byte [] b , final int off , final int len ) {
59- if ((off < 0 )
60- || (off > b .length )
61- || (len < 0 )
62- || ((off + len ) > b .length )
63- || ((off + len ) < 0 )) {
64- throw new IndexOutOfBoundsException ();
56+ if ((off < 0 ) || (off > b .length ) || (len < 0 ) || ((off + len ) > b .length ) || ((off + len ) < 0 )) {
57+ throw new IndexOutOfBoundsException (String .format ("offset=%,d, length=%,d" , off , len ));
6558 } else if (len == 0 ) {
6659 return ;
6760 }
@@ -97,60 +90,50 @@ public void writeTo(final OutputStream out) throws IOException {
9790 }
9891
9992 /**
100- * Fetches entire contents of an <code>InputStream</code> and represent
101- * same data as result InputStream.
93+ * Fetches entire contents of an <code>InputStream</code> and represent same data as result InputStream.
10294 * <p>
10395 * This method is useful where,
10496 * </p>
10597 * <ul>
10698 * <li>Source InputStream is slow.</li>
107- * <li>It has network resources associated, so we cannot keep it open for
108- * long time.</li>
99+ * <li>It has network resources associated, so we cannot keep it open for long time.</li>
109100 * <li>It has network timeout associated.</li>
110101 * </ul>
111- * It can be used in favor of {@link #toByteArray()}, since it
112- * avoids unnecessary allocation and copy of byte[].<br>
113- * This method buffers the input internally, so there is no need to use a
114- * <code>BufferedInputStream</code>.
102+ * It can be used in favor of {@link #toByteArray()}, since it avoids unnecessary allocation and copy of byte[].<br>
103+ * This method buffers the input internally, so there is no need to use a <code>BufferedInputStream</code>.
115104 *
116105 * @param input Stream to be fully buffered.
117106 * @return A fully buffered stream.
118107 * @throws IOException if an I/O error occurs
119108 */
120- public static InputStream toBufferedInputStream (final InputStream input )
121- throws IOException {
109+ public static InputStream toBufferedInputStream (final InputStream input ) throws IOException {
122110 return toBufferedInputStream (input , DEFAULT_SIZE );
123111 }
124112
125113 /**
126- * Fetches entire contents of an <code>InputStream</code> and represent
127- * same data as result InputStream.
114+ * Fetches entire contents of an <code>InputStream</code> and represent same data as result InputStream.
128115 * <p>
129116 * This method is useful where,
130117 * </p>
131118 * <ul>
132119 * <li>Source InputStream is slow.</li>
133- * <li>It has network resources associated, so we cannot keep it open for
134- * long time.</li>
120+ * <li>It has network resources associated, so we cannot keep it open for long time.</li>
135121 * <li>It has network timeout associated.</li>
136122 * </ul>
137- * It can be used in favor of {@link #toByteArray()}, since it
138- * avoids unnecessary allocation and copy of byte[].<br>
139- * This method buffers the input internally, so there is no need to use a
140- * <code>BufferedInputStream</code>.
123+ * It can be used in favor of {@link #toByteArray()}, since it avoids unnecessary allocation and copy of byte[].<br>
124+ * This method buffers the input internally, so there is no need to use a <code>BufferedInputStream</code>.
141125 *
142126 * @param input Stream to be fully buffered.
143127 * @param size the initial buffer size
144128 * @return A fully buffered stream.
145129 * @throws IOException if an I/O error occurs
146130 */
147- public static InputStream toBufferedInputStream (final InputStream input , final int size )
148- throws IOException {
131+ public static InputStream toBufferedInputStream (final InputStream input , final int size ) throws IOException {
149132 // It does not matter if a ByteArrayOutputStream is not closed as close() is a no-op
150- @ SuppressWarnings ( "resource" )
151- final UnsynchronizedByteArrayOutputStream output = new UnsynchronizedByteArrayOutputStream ( size );
152- output .write ( input );
153- return output . toInputStream ();
133+ try ( final UnsynchronizedByteArrayOutputStream output = new UnsynchronizedByteArrayOutputStream ( size )) {
134+ output . write ( input );
135+ return output .toInputStream ( );
136+ }
154137 }
155138
156139 @ Override
0 commit comments