This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-io.git
The following commit(s) were added to refs/heads/master by this push: new bda102e36 [IO-871] IOUtils.contentEquals is incorrect when InputStream.available under-reports bda102e36 is described below commit bda102e36114d1dac58504c03b658f43a295c662 Author: Gary D. Gregory <garydgreg...@gmail.com> AuthorDate: Sun Mar 23 09:15:42 2025 -0400 [IO-871] IOUtils.contentEquals is incorrect when InputStream.available under-reports --- src/main/java/org/apache/commons/io/channels/FileChannels.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/apache/commons/io/channels/FileChannels.java b/src/main/java/org/apache/commons/io/channels/FileChannels.java index f4d611493..3c731aba9 100644 --- a/src/main/java/org/apache/commons/io/channels/FileChannels.java +++ b/src/main/java/org/apache/commons/io/channels/FileChannels.java @@ -135,7 +135,7 @@ public static boolean contentEquals(final SeekableByteChannel channel1, final Se * * @param channel The source channel. * @param dst The buffer into which bytes are to be transferred. - * @return The number of bytes read, possibly zero, or {@code-1} if the channel has reached end-of-stream + * @return The number of bytes read, <em>never</em> zero, or {@code -1} if the channel has reached end-of-stream * @throws IOException If some other I/O error occurs. * @throws IllegalArgumentException If there is room in the given buffer. */ @@ -143,18 +143,20 @@ private static int readToLimit(final ReadableByteChannel channel, final ByteBuff if (!dst.hasRemaining()) { throw new IllegalArgumentException(); } - int numRead = 0; int totalRead = 0; while (dst.hasRemaining()) { - if ((totalRead += numRead = channel.read(dst)) == IOUtils.EOF) { + final int numRead; + if ((numRead = channel.read(dst)) == IOUtils.EOF) { break; } if (numRead == 0) { // 0 may be returned from a non-blocking channel. Thread.yield(); + } else { + totalRead += numRead; } } - return totalRead; + return totalRead != 0 ? totalRead : IOUtils.EOF; } private static long size(final SeekableByteChannel channel) throws IOException {