Skip to content

Commit bda102e

Browse files
committed
[IO-871] IOUtils.contentEquals is incorrect when InputStream.available
under-reports
1 parent ddb6c58 commit bda102e

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/main/java/org/apache/commons/io/channels/FileChannels.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,26 +135,28 @@ public static boolean contentEquals(final SeekableByteChannel channel1, final Se
135135
*
136136
* @param channel The source channel.
137137
* @param dst The buffer into which bytes are to be transferred.
138-
* @return The number of bytes read, possibly zero, or {@code-1} if the channel has reached end-of-stream
138+
* @return The number of bytes read, <em>never</em> zero, or {@code -1} if the channel has reached end-of-stream
139139
* @throws IOException If some other I/O error occurs.
140140
* @throws IllegalArgumentException If there is room in the given buffer.
141141
*/
142142
private static int readToLimit(final ReadableByteChannel channel, final ByteBuffer dst) throws IOException {
143143
if (!dst.hasRemaining()) {
144144
throw new IllegalArgumentException();
145145
}
146-
int numRead = 0;
147146
int totalRead = 0;
148147
while (dst.hasRemaining()) {
149-
if ((totalRead += numRead = channel.read(dst)) == IOUtils.EOF) {
148+
final int numRead;
149+
if ((numRead = channel.read(dst)) == IOUtils.EOF) {
150150
break;
151151
}
152152
if (numRead == 0) {
153153
// 0 may be returned from a non-blocking channel.
154154
Thread.yield();
155+
} else {
156+
totalRead += numRead;
155157
}
156158
}
157-
return totalRead;
159+
return totalRead != 0 ? totalRead : IOUtils.EOF;
158160
}
159161

160162
private static long size(final SeekableByteChannel channel) throws IOException {

0 commit comments

Comments
 (0)