garydgregory commented on code in PR #776: URL: https://github.com/apache/commons-io/pull/776#discussion_r2336501535
########## src/main/java/org/apache/commons/io/IOUtils.java: ########## @@ -2637,57 +2645,61 @@ public static BufferedReader toBufferedReader(final Reader reader, final int siz } /** - * Gets the contents of an {@link InputStream} as a {@code byte[]}. - * <p> - * This method buffers the input internally, so there is no need to use a {@link BufferedInputStream}. - * </p> + * Reads all the bytes from an input stream in a byte array. * - * @param inputStream the {@link InputStream} to read. - * @return the requested byte array. - * @throws NullPointerException if the InputStream is {@code null}. - * @throws IOException if an I/O error occurs or reading more than {@link Integer#MAX_VALUE} occurs. + * <p>The memory used by this method is <strong>proportional</strong> to the number + * of bytes read, which is only limited by {@link Integer#MAX_VALUE}. This makes it unsuitable for + * processing large input streams, unless sufficient heap space is available.</p> + * + * @param inputStream The {@link InputStream} to read; must not be {@code null}. + * @return A new byte array. + * @throws IllegalArgumentException If the size of the stream is greater than the maximum array size. + * @throws IOException If an I/O error occurs while reading. + * @throws NullPointerException If {@code inputStream} is {@code null}. */ public static byte[] toByteArray(final InputStream inputStream) throws IOException { - // We use a ThresholdingOutputStream to avoid reading AND writing more than Integer.MAX_VALUE. - try (UnsynchronizedByteArrayOutputStream ubaOutput = UnsynchronizedByteArrayOutputStream.builder().get(); - ThresholdingOutputStream thresholdOutput = new ThresholdingOutputStream(Integer.MAX_VALUE, os -> { - throw new IllegalArgumentException(String.format("Cannot read more than %,d into a byte array", Integer.MAX_VALUE)); - }, os -> ubaOutput)) { - copy(inputStream, thresholdOutput); - return ubaOutput.toByteArray(); + final UnsynchronizedByteArrayOutputStream output = copyToOutputStream(inputStream, SOFT_MAX_ARRAY_LENGTH + 1, DEFAULT_BUFFER_SIZE); + if (output.size() > SOFT_MAX_ARRAY_LENGTH) { + throw new IllegalArgumentException(String.format("Cannot read more than %,d into a byte array", SOFT_MAX_ARRAY_LENGTH)); } + return output.toByteArray(); } /** - * Gets the contents of an {@link InputStream} as a {@code byte[]}. Use this method instead of - * {@link #toByteArray(InputStream)} when {@link InputStream} size is known. + * Reads exactly {@code size} bytes from the given {@link InputStream} into a new {@code byte[]}. * - * @param input the {@link InputStream} to read. - * @param size the size of {@link InputStream} to read, where 0 < {@code size} <= length of input stream. - * @return byte [] of length {@code size}. - * @throws IOException if an I/O error occurs or {@link InputStream} length is smaller than parameter {@code size}. - * @throws IllegalArgumentException if {@code size} is less than zero. + * <p>This variant provides no safeguards against allocating very large arrays. + * For large streams, prefer {@link #toByteArray(InputStream, int, int)}, + * which enforces stricter memory usage constraints.</p> + * + * @param input the {@link InputStream} to read; must not be {@code null}. + * @param size the exact number of bytes to read; must be {@code >= 0}. + * @return a new byte array of length {@code size}. + * @throws IllegalArgumentException if {@code size} is negative. + * @throws EOFException if the stream ends before {@code size} bytes are read. + * @throws IOException if an I/O error occurs while reading. + * @throws NullPointerException if {@code input} is {@code null}. * @since 2.1 */ - @SuppressWarnings("resource") public static byte[] toByteArray(final InputStream input, final int size) throws IOException { - return toByteArray(Objects.requireNonNull(input, "input")::read, size); + return toByteArray(input::read, size); Review Comment: With this change, we are losing the better NPE message on Java < 14. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@commons.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org