@@ -172,7 +172,7 @@ public class IOUtils {
172172 /**
173173 * The default buffer to use for the skip() methods.
174174 */
175- private static final byte [] SKIP_BYTE_BUFFER = new byte [ DEFAULT_BUFFER_SIZE ] ;
175+ private static final byte [] SKIP_BYTE_BUFFER = byteArray () ;
176176
177177 // Allocated in the relevant skip method if necessary.
178178 /*
@@ -315,6 +315,29 @@ public static BufferedWriter buffer(final Writer writer, final int size) {
315315 return writer instanceof BufferedWriter ? (BufferedWriter ) writer : new BufferedWriter (writer , size );
316316 }
317317
318+ /**
319+ * Returns a new byte array of size {@link #DEFAULT_BUFFER_SIZE}.
320+ *
321+ * @return a new byte array of size {@link #DEFAULT_BUFFER_SIZE}.
322+ * @since 2.9.0
323+ */
324+ public static byte [] byteArray () {
325+ return byteArray (DEFAULT_BUFFER_SIZE );
326+ }
327+
328+ /**
329+ * Returns a new byte array of the given size.
330+ *
331+ * TODO Consider guarding or warning against large allocations...
332+ *
333+ * @param size array size.
334+ * @return a new byte array of the given size.
335+ * @since 2.9.0
336+ */
337+ public static byte [] byteArray (final int size ) {
338+ return new byte [size ];
339+ }
340+
318341 /**
319342 * Closes the given {@link Closeable} as a null-safe operation.
320343 *
@@ -754,8 +777,8 @@ public static boolean contentEquals(final InputStream input1, final InputStream
754777 return false ;
755778 }
756779
757- final byte [] array1 = new byte [ DEFAULT_BUFFER_SIZE ] ;
758- final byte [] array2 = new byte [ DEFAULT_BUFFER_SIZE ] ;
780+ final byte [] array1 = byteArray () ;
781+ final byte [] array2 = byteArray () ;
759782 int pos1 ;
760783 int pos2 ;
761784 int count1 ;
@@ -925,7 +948,7 @@ public static int copy(final InputStream inputStream, final OutputStream outputS
925948 */
926949 public static long copy (final InputStream inputStream , final OutputStream outputStream , final int bufferSize )
927950 throws IOException {
928- return copyLarge (inputStream , outputStream , new byte [ bufferSize ] );
951+ return copyLarge (inputStream , outputStream , IOUtils . byteArray ( bufferSize ) );
929952 }
930953
931954 /**
@@ -1243,7 +1266,7 @@ public static long copyLarge(final InputStream inputStream, final OutputStream o
12431266 */
12441267 public static long copyLarge (final InputStream input , final OutputStream output , final long inputOffset ,
12451268 final long length ) throws IOException {
1246- return copyLarge (input , output , inputOffset , length , new byte [ DEFAULT_BUFFER_SIZE ] );
1269+ return copyLarge (input , output , inputOffset , length , byteArray () );
12471270 }
12481271
12491272 /**
@@ -1730,7 +1753,7 @@ public static void readFully(final InputStream input, final byte[] buffer, final
17301753 * @since 2.5
17311754 */
17321755 public static byte [] readFully (final InputStream input , final int length ) throws IOException {
1733- final byte [] buffer = new byte [ length ] ;
1756+ final byte [] buffer = IOUtils . byteArray ( length ) ;
17341757 readFully (input , buffer , 0 , buffer .length );
17351758 return buffer ;
17361759 }
@@ -2317,7 +2340,7 @@ public static byte[] toByteArray(final InputStream input, final int size) throws
23172340 return EMPTY_BYTE_ARRAY ;
23182341 }
23192342
2320- final byte [] data = new byte [ size ] ;
2343+ final byte [] data = IOUtils . byteArray ( size ) ;
23212344 int offset = 0 ;
23222345 int read ;
23232346
0 commit comments