ppkarwasz commented on code in PR #776: URL: https://github.com/apache/commons-io/pull/776#discussion_r2325248753
########## src/main/java/org/apache/commons/io/IOUtils.java: ########## @@ -2659,35 +2660,61 @@ public static byte[] toByteArray(final InputStream inputStream) throws IOExcepti } /** - * 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 allocates the target array immediately and attempts to fill it in one pass. + * It assumes that {@code size} is correct. + * If the stream ends prematurely, an {@link EOFException} is thrown. + * </p> + * + * <p> + * <strong>Important:</strong> This method does <em>not</em> defend against corrupted + * or untrusted {@code size} values. + * For untrusted input, use {@link #toByteArray(InputStream, int, int)} instead, + * which validates that the stream contains at least {@code size} bytes before allocating the target array. + * </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); + Objects.requireNonNull(input, "input"); + if (size < 0) { + throw new IllegalArgumentException("Size must be equal or greater than zero: " + size); + } + return toByteArray(input::read, size); } /** - * Gets contents of an {@link InputStream} as a {@code byte[]}. - * Use this method instead of {@link #toByteArray(InputStream)} - * when {@link InputStream} size is known. - * <strong>NOTE:</strong> the method checks that the length can safely be cast to an int without truncation - * before using {@link IOUtils#toByteArray(InputStream, int)} to read into the byte array. - * (Arrays can have no more than Integer.MAX_VALUE entries anyway.) + * 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} <= min(Integer.MAX_VALUE, length of input stream). - * @return byte [] the requested byte array, of length {@code size}. - * @throws IOException if an I/O error occurs or {@link InputStream} length is less than {@code size}. - * @throws IllegalArgumentException if size is less than zero or size is greater than Integer.MAX_VALUE. - * @see IOUtils#toByteArray(InputStream, int) + * <p> + * This is a convenience overload of {@link #toByteArray(InputStream, int, int)} that accepts a Review Comment: Since this method throws an unchecked exception if the `long` does not fit into an `int`, most callers have to check the parameter, before calling this. So this methods looks like a convenient composition of a size check and `toByteArray(InputStream, int, int)`. Fixed in https://github.com/apache/commons-io/pull/776/commits/7362d3e34801662b029ea6ef4e005dfb1194c57b -- 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