ppkarwasz commented on code in PR #776: URL: https://github.com/apache/commons-io/pull/776#discussion_r2340632143
########## src/main/java/org/apache/commons/io/IOUtils.java: ########## @@ -2697,6 +2711,68 @@ public static byte[] toByteArray(final InputStream input, final long size) throw return toByteArray(input, (int) size); } + /** + * Reads exactly {@code size} bytes from the given {@link InputStream} into a new {@code byte[]}. + * + * <p>The memory used by this method is <strong>proportional</strong> to the number + * of bytes read and limited by the specified {@code size}. This makes it suitable for + * processing large input streams, provided that <strong>sufficient</strong> heap space is + * available.</p> + * + * <p>This method processes the input stream in successive chunks of up to + * {@code chunkSize} bytes.</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}. + * The actual bytes read are validated to equal {@code size}. + * @param chunkSize The chunk size for incremental reading; must be {@code > 0}. + * @return a new byte array of length {@code size}. + * @throws IllegalArgumentException if {@code size} is negative or {@code chunkSize <= 0}. + * @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.21.0 + */ + public static byte[] toByteArray(final InputStream input, final int size, final int chunkSize) throws IOException { + Objects.requireNonNull(input, "input"); + if (chunkSize <= 0) { + throw new IllegalArgumentException("Chunk size must be greater than zero: " + chunkSize); + } + if (size <= chunkSize) { + // throws if size < 0 + return toByteArray(input::read, size); + } + final UnsynchronizedByteArrayOutputStream output = copyToOutputStream(input, size, chunkSize); + if (output.size() != size) { + throw new EOFException("Unexpected read size, current: " + output.size() + ", expected: " + size); Review Comment: Those two calls have slightly different checks: - `toByteArray(InputStream)` checks if the size is **at most** `SOFT_MAX_ARRAY_LENGTH` and throws an IAE if the check fails. - `toByteArray(InputStream, int, int)` checks if the size is **equal** `size` and throws an EOF if the check fails. To move the check inside the method, I would need to pass an `IOConsumer` as parameter. In practice this would not change the location of the check code. -- 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