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 90c87a483 FileChannels.contentEquals(FileChannel, FileChannel, int)
can return false when comparing a non-blocking channel
90c87a483 is described below
commit 90c87a4836c4267b4e5c50e5d21c129d6e2e4a4b
Author: Gary D. Gregory <[email protected]>
AuthorDate: Mon Mar 3 07:30:48 2025 -0500
FileChannels.contentEquals(FileChannel, FileChannel, int) can return
false when comparing a non-blocking channel
- Add FileChannels.contentEquals(SeekableByteChannel,
SeekableByteChannel, int)
---
src/changes/changes.xml | 3 +
.../org/apache/commons/io/RandomAccessFiles.java | 3 +-
.../apache/commons/io/channels/FileChannels.java | 49 ++++++--
.../commons/io/channels/FileChannelProxy.java | 138 +++++++++++++++++++++
.../commons/io/channels/FileChannelsTest.java | 112 +++++++++++++++--
.../io/channels/NonBlockingFileChannelProxy.java | 56 +++++++++
6 files changed, 345 insertions(+), 16 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 59e5fdfb2..6528d0290 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -62,6 +62,8 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="fix" due-to="Gary
Gregory">Avoid unnecessary boxing and unboxing of int values in
UncheckedBufferedReader.read().</action>
<action dev="ggregory" type="fix" due-to="Gary
Gregory">Avoid unnecessary boxing and unboxing of int values in
UncheckedFilterInputStream.available() and read().</action>
<action dev="ggregory" type="fix" due-to="Gary
Gregory">Avoid unnecessary boxing and unboxing of int values in
UncheckedFilterReader.read().</action>
+ <action dev="ggregory" type="fix" due-to="Gary
Gregory">FileChannels.contentEquals(FileChannel, FileChannel, int) can return
false when comparing a non-blocking channel.</action>
+ <action dev="ggregory" type="fix" due-to="Gary
Gregory">Deprecate FileChannels.contentEquals(FileChannel, FileChannel, int) in
favor of FileChannels.contentEquals(SeekableByteChannel, SeekableByteChannel,
int).</action>
<!-- ADD -->
<action dev="ggregory" type="add" issue="IO-860" due-to="Nico Strecker,
Gary Gregory">Add ThrottledInputStream.Builder.setMaxBytes(long,
ChronoUnit).</action>
<action dev="ggregory" type="add" due-to="Gary
Gregory">Add IOIterable.</action>
@@ -77,6 +79,7 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary
Gregory">Add BrokenOutputStream.BrokenOutputStream(Function<String>,
Throwable>) and deprecate Supplier<String> constructor.</action>
<action dev="ggregory" type="add" due-to="Gary
Gregory">Add IOBooleanSupplier.</action>
<action dev="ggregory" type="add" due-to="Gary
Gregory">Add Uncheck.getAsBoolean(IOBooleanSupplier).</action>
+ <action dev="ggregory" type="fix" due-to="Gary
Gregory">Add FileChannels.contentEquals(SeekableByteChannel,
SeekableByteChannel, int).</action>
<!-- UPDATE -->
<action dev="ggregory" type="update" due-to="Dependabot,
Gary Gregory">Bump commons.bytebuddy.version from 1.15.10 to 1.17.1 #710, #715,
#720.</action>
<action dev="ggregory" type="update" due-to="Gary
Gregory">Bump commons-codec:commons-codec from 1.17.1 to 1.18.0. #717.</action>
diff --git a/src/main/java/org/apache/commons/io/RandomAccessFiles.java
b/src/main/java/org/apache/commons/io/RandomAccessFiles.java
index db91e7d77..178d9ef0b 100644
--- a/src/main/java/org/apache/commons/io/RandomAccessFiles.java
+++ b/src/main/java/org/apache/commons/io/RandomAccessFiles.java
@@ -19,6 +19,7 @@
import java.io.IOException;
import java.io.RandomAccessFile;
+import java.nio.channels.SeekableByteChannel;
import java.util.Objects;
import org.apache.commons.io.channels.FileChannels;
@@ -57,7 +58,7 @@ public static boolean contentEquals(final RandomAccessFile
raf1, final RandomAcc
// Dig in and to the work
// We do not close FileChannels because that would close the owning
RandomAccessFile.
// Instead, the caller is assumed to manage the given RandomAccessFile
objects.
- return FileChannels.contentEquals(raf1.getChannel(),
raf2.getChannel(), IOUtils.DEFAULT_BUFFER_SIZE);
+ return FileChannels.contentEquals((SeekableByteChannel)
raf1.getChannel(), raf2.getChannel(), IOUtils.DEFAULT_BUFFER_SIZE);
}
private static long length(final RandomAccessFile raf) throws IOException {
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 6db35be1e..5dc960ac5 100644
--- a/src/main/java/org/apache/commons/io/channels/FileChannels.java
+++ b/src/main/java/org/apache/commons/io/channels/FileChannels.java
@@ -20,6 +20,7 @@
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
+import java.nio.channels.SeekableByteChannel;
import java.util.Objects;
import org.apache.commons.io.IOUtils;
@@ -39,8 +40,27 @@ public final class FileChannels {
* @param byteBufferSize The two internal buffer capacities, in bytes.
* @return true if the contents of both RandomAccessFiles are equal, false
otherwise.
* @throws IOException if an I/O error occurs.
+ * @deprecated Use {@link #contentEquals(SeekableByteChannel,
SeekableByteChannel, int)}.
*/
+ @Deprecated
public static boolean contentEquals(final FileChannel channel1, final
FileChannel channel2, final int byteBufferSize) throws IOException {
+ return contentEquals((SeekableByteChannel) channel1, channel2,
byteBufferSize);
+ }
+
+ /**
+ * Tests if two readable byte channel contents are equal starting at their
respective current positions.
+ * <p>
+ * If a file channel is a non-blocking file channel, it may return 0 bytes
read for any given call. In order to avoid waiting forever when trying again, a
+ * timeout Duration can be specified, which when met, throws an
IOException.
+ * </p>
+ *
+ * @param channel1 A readable byte channel.
+ * @param channel2 Another readable byte channel.
+ * @param byteBufferSize The two internal buffer capacities, in bytes.
+ * @return true if the contents of both RandomAccessFiles are equal, false
otherwise.
+ * @throws IOException if an I/O error occurs or the timeout is met.
+ */
+ public static boolean contentEquals(final SeekableByteChannel channel1,
final SeekableByteChannel channel2, final int byteBufferSize) throws
IOException {
// Short-circuit test
if (Objects.equals(channel1, channel2)) {
return true;
@@ -57,15 +77,30 @@ public static boolean contentEquals(final FileChannel
channel1, final FileChanne
// Dig in and do the work
final ByteBuffer byteBuffer1 =
ByteBuffer.allocateDirect(byteBufferSize);
final ByteBuffer byteBuffer2 =
ByteBuffer.allocateDirect(byteBufferSize);
+ int numRead1 = 0;
+ int numRead2 = 0;
+ boolean read0On1 = false;
+ boolean read0On2 = false;
while (true) {
- final int read1 = channel1.read(byteBuffer1);
- final int read2 = channel2.read(byteBuffer2);
- byteBuffer1.clear();
- byteBuffer2.clear();
- if (read1 == IOUtils.EOF && read2 == IOUtils.EOF) {
+ if (!read0On2) {
+ numRead1 = channel1.read(byteBuffer1);
+ byteBuffer1.clear();
+ read0On1 = numRead1 == 0;
+ }
+ if (!read0On1) {
+ numRead2 = channel2.read(byteBuffer2);
+ byteBuffer2.clear();
+ read0On2 = numRead2 == 0;
+ }
+ if (numRead1 == IOUtils.EOF && numRead2 == IOUtils.EOF) {
return byteBuffer1.equals(byteBuffer2);
}
- if (read1 != read2) {
+ if (numRead1 == 0 || numRead2 == 0) {
+ // 0 may be returned from a non-blocking channel.
+ Thread.yield();
+ continue;
+ }
+ if (numRead1 != numRead2) {
return false;
}
if (!byteBuffer1.equals(byteBuffer2)) {
@@ -74,7 +109,7 @@ public static boolean contentEquals(final FileChannel
channel1, final FileChanne
}
}
- private static long size(final FileChannel channel) throws IOException {
+ private static long size(final SeekableByteChannel channel) throws
IOException {
return channel != null ? channel.size() : 0;
}
diff --git a/src/test/java/org/apache/commons/io/channels/FileChannelProxy.java
b/src/test/java/org/apache/commons/io/channels/FileChannelProxy.java
new file mode 100644
index 000000000..859f6ad04
--- /dev/null
+++ b/src/test/java/org/apache/commons/io/channels/FileChannelProxy.java
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.io.channels;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.MappedByteBuffer;
+import java.nio.channels.FileChannel;
+import java.nio.channels.FileLock;
+import java.nio.channels.ReadableByteChannel;
+import java.nio.channels.WritableByteChannel;
+
+/**
+ * Proxies a FileChannel.
+ */
+class FileChannelProxy extends FileChannel {
+
+ FileChannel fileChannel;
+
+ FileChannelProxy(final FileChannel fileChannel) {
+ this.fileChannel = fileChannel;
+ }
+
+ @Override
+ public boolean equals(final Object o) {
+ return fileChannel.equals(o);
+ }
+
+ @Override
+ public void force(final boolean metaData) throws IOException {
+ fileChannel.force(metaData);
+ }
+
+ @Override
+ public int hashCode() {
+ return fileChannel.hashCode();
+ }
+
+ @Override
+ protected void implCloseChannel() throws IOException {
+ fileChannel.close();
+ }
+
+ @Override
+ public FileLock lock(final long position, final long size, final boolean
shared) throws IOException {
+ return fileChannel.lock(position, size, shared);
+ }
+
+ @Override
+ public MappedByteBuffer map(final MapMode mode, final long position, final
long size) throws IOException {
+ return fileChannel.map(mode, position, size);
+ }
+
+ @Override
+ public long position() throws IOException {
+ return fileChannel.position();
+ }
+
+ @Override
+ public FileChannel position(final long newPosition) throws IOException {
+ return fileChannel.position(newPosition);
+ }
+
+ @Override
+ public int read(final ByteBuffer dst) throws IOException {
+ return fileChannel.read(dst);
+ }
+
+ @Override
+ public int read(final ByteBuffer dst, final long position) throws
IOException {
+ return fileChannel.read(dst, position);
+ }
+
+ @Override
+ public long read(final ByteBuffer[] dsts, final int offset, final int
length) throws IOException {
+ return fileChannel.read(dsts, offset, length);
+ }
+
+ @Override
+ public long size() throws IOException {
+ return fileChannel.size();
+ }
+
+ @Override
+ public String toString() {
+ return fileChannel.toString();
+ }
+
+ @Override
+ public long transferFrom(final ReadableByteChannel src, final long
position, final long count) throws IOException {
+ return fileChannel.transferFrom(src, position, count);
+ }
+
+ @Override
+ public long transferTo(final long position, final long count, final
WritableByteChannel target) throws IOException {
+ return fileChannel.transferTo(position, count, target);
+ }
+
+ @Override
+ public FileChannel truncate(final long size) throws IOException {
+ return fileChannel.truncate(size);
+ }
+
+ @Override
+ public FileLock tryLock(final long position, final long size, final
boolean shared) throws IOException {
+ return fileChannel.tryLock(position, size, shared);
+ }
+
+ @Override
+ public int write(final ByteBuffer src) throws IOException {
+ return fileChannel.write(src);
+ }
+
+ @Override
+ public int write(final ByteBuffer src, final long position) throws
IOException {
+ return fileChannel.write(src, position);
+ }
+
+ @Override
+ public long write(final ByteBuffer[] srcs, final int offset, final int
length) throws IOException {
+ return fileChannel.write(srcs, offset, length);
+ }
+}
diff --git a/src/test/java/org/apache/commons/io/channels/FileChannelsTest.java
b/src/test/java/org/apache/commons/io/channels/FileChannelsTest.java
index 59658e547..f9ee13449 100644
--- a/src/test/java/org/apache/commons/io/channels/FileChannelsTest.java
+++ b/src/test/java/org/apache/commons/io/channels/FileChannelsTest.java
@@ -14,12 +14,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
package org.apache.commons.io.channels;
import static java.nio.charset.StandardCharsets.US_ASCII;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
import java.io.File;
import java.io.FileInputStream;
@@ -27,6 +29,7 @@
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.time.Duration;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
@@ -35,12 +38,18 @@
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
+import org.junitpioneer.jupiter.cartesian.CartesianTest;
+import org.junitpioneer.jupiter.cartesian.CartesianTest.Values;
/**
* Tests {@link FileChannels}.
*/
public class FileChannelsTest extends AbstractTempDirTest {
+ enum FileChannelType {
+ STOCK, PROXY, NON_BLOCKING
+ }
+
private static final int SMALL_BUFFER_SIZE = 1024;
private static final String CONTENT = StringUtils.repeat("x",
SMALL_BUFFER_SIZE);
@@ -49,6 +58,12 @@ public static int[] getBufferSizes() {
return new int[] { 1, 2, IOUtils.DEFAULT_BUFFER_SIZE / 10,
IOUtils.DEFAULT_BUFFER_SIZE, IOUtils.DEFAULT_BUFFER_SIZE * 10 };
}
+ public static Object[][] getBufferSizesAndTimeouts() {
+ // 1 and 2 are unusual and slow edge cases, but edge cases nonetheless.
+ return new Object[][] { { 1, Duration.ZERO }, { 2, Duration.ZERO }, {
IOUtils.DEFAULT_BUFFER_SIZE / 10, Duration.ZERO },
+ { IOUtils.DEFAULT_BUFFER_SIZE, Duration.ZERO }, {
IOUtils.DEFAULT_BUFFER_SIZE * 10, Duration.ZERO } };
+ }
+
private static byte reverse(final byte b) {
return (byte) (~b & 0xff);
}
@@ -57,14 +72,14 @@ private boolean isEmpty(final File empty) {
return empty.length() == 0;
}
-
private FileChannel reset(final FileChannel fc) throws IOException {
return fc.position(0);
}
- @ParameterizedTest()
- @MethodSource("getBufferSizes")
- public void testContentEquals(final int bufferSize) throws IOException {
+ @CartesianTest()
+ public void testContentEquals(
+ @Values(ints = { 1, 2, IOUtils.DEFAULT_BUFFER_SIZE / 10,
IOUtils.DEFAULT_BUFFER_SIZE, IOUtils.DEFAULT_BUFFER_SIZE * 10 }) final int
bufferSize,
+ @CartesianTest.Enum final FileChannelType fileChannelType1,
@CartesianTest.Enum final FileChannelType fileChannelType2) throws IOException {
final Path bigFile1 = Files.createTempFile(getClass().getSimpleName(),
"-1.bin");
final Path bigFile2 = Files.createTempFile(getClass().getSimpleName(),
"-2.bin");
final Path bigFile3 = Files.createTempFile(getClass().getSimpleName(),
"-3.bin");
@@ -80,7 +95,7 @@ public void testContentEquals(final int bufferSize) throws
IOException {
bytes2[0] = 2;
Files.write(bigFile1, bytes1);
Files.write(bigFile2, bytes2);
- try (FileChannel fc1 = FileChannel.open(bigFile1); FileChannel fc2
= FileChannel.open(bigFile2)) {
+ try (FileChannel fc1 = open(bigFile1, fileChannelType1);
FileChannel fc2 = open(bigFile2, fileChannelType2)) {
assertFalse(FileChannels.contentEquals(fc1, fc2, bufferSize));
assertFalse(FileChannels.contentEquals(reset(fc2), reset(fc1),
bufferSize));
assertTrue(FileChannels.contentEquals(reset(fc1), reset(fc1),
bufferSize));
@@ -90,7 +105,7 @@ public void testContentEquals(final int bufferSize) throws
IOException {
final int last = bytes3.length - 1;
bytes3[last] = reverse(bytes3[last]);
Files.write(bigFile3, bytes3);
- try (FileChannel fc1 = FileChannel.open(bigFile1); FileChannel fc3
= FileChannel.open(bigFile3)) {
+ try (FileChannel fc1 = open(bigFile1, null); FileChannel fc3 =
open(bigFile3, fileChannelType2)) {
assertFalse(FileChannels.contentEquals(fc1, fc3, bufferSize));
assertFalse(FileChannels.contentEquals(reset(fc3), reset(fc1),
bufferSize));
// Test just the last byte
@@ -101,7 +116,7 @@ public void testContentEquals(final int bufferSize) throws
IOException {
// Make the LAST byte equal.
bytes3 = bytes1.clone();
Files.write(bigFile3, bytes3);
- try (FileChannel fc1 = FileChannel.open(bigFile1); FileChannel fc3
= FileChannel.open(bigFile3)) {
+ try (FileChannel fc1 = open(bigFile1, fileChannelType1);
FileChannel fc3 = open(bigFile3, fileChannelType2)) {
// Test just the last byte
fc1.position(last);
fc3.position(last);
@@ -112,7 +127,7 @@ public void testContentEquals(final int bufferSize) throws
IOException {
final int middle = bytes3.length / 2;
bytes3[middle] = reverse(bytes3[middle]);
Files.write(bigFile3, bytes3);
- try (FileChannel fc1 = FileChannel.open(bigFile1); FileChannel fc3
= FileChannel.open(bigFile3)) {
+ try (FileChannel fc1 = open(bigFile1, fileChannelType1);
FileChannel fc3 = open(bigFile3, fileChannelType2)) {
assertFalse(FileChannels.contentEquals(fc1, fc3, bufferSize));
assertFalse(FileChannels.contentEquals(reset(fc3), reset(fc1),
bufferSize));
}
@@ -124,6 +139,23 @@ public void testContentEquals(final int bufferSize) throws
IOException {
}
}
+ private static FileChannel open(final Path path, final FileChannelType
fileChannelType) throws IOException {
+ final FileChannel fc = FileChannel.open(path);
+ if (fileChannelType != null) {
+ switch (fileChannelType) {
+ case NON_BLOCKING:
+ return new NonBlockingFileChannelProxy(fc);
+ case STOCK:
+ return fc;
+ case PROXY:
+ return new FileChannelProxy(fc);
+ default:
+ fail("Unexpected FileChannelType " + fileChannelType);
+ }
+ }
+ return FileChannel.open(path);
+ }
+
private void testContentEquals(final String content1, final String
content2, final int bufferSize) throws IOException {
assertTrue(FileChannels.contentEquals(null, null, bufferSize));
// Prepare test files with same size but different content
@@ -187,4 +219,68 @@ public void testContentEqualsEmpty(final int bufferSize)
throws IOException {
assertTrue(FileChannels.contentEquals(channelNotEmpty,
channelNotEmpty, bufferSize));
}
}
+
+ @CartesianTest
+ public void testContentEqualsTimeout(
+ @Values(ints = { 1, 2, IOUtils.DEFAULT_BUFFER_SIZE / 10,
IOUtils.DEFAULT_BUFFER_SIZE, IOUtils.DEFAULT_BUFFER_SIZE * 10 }) final int
bufferSize,
+ @Values(ints = { 0, 1 }) final int timeoutMillis) throws
IOException {
+ final Duration timeout = Duration.ofMillis(timeoutMillis);
+ final Path bigFile1 = Files.createTempFile(getClass().getSimpleName(),
"-1.bin");
+ final Path bigFile2 = Files.createTempFile(getClass().getSimpleName(),
"-2.bin");
+ final Path bigFile3 = Files.createTempFile(getClass().getSimpleName(),
"-3.bin");
+ try {
+ // This length must match any restriction from the Surefire
configuration.
+ final int newLength = 2_000_000;
+ final byte[] bytes1 = new byte[newLength];
+ final byte[] bytes2 = new byte[newLength];
+ // Make sure bytes1 and bytes2 are different despite the shuffle
+ ArrayUtils.shuffle(bytes1);
+ bytes1[0] = 1;
+ ArrayUtils.shuffle(bytes2);
+ bytes2[0] = 2;
+ Files.write(bigFile1, bytes1);
+ Files.write(bigFile2, bytes2);
+ try (FileChannel fc1 = open(bigFile1, null); FileChannel fc2 =
open(bigFile2, null)) {
+ assertFalse(FileChannels.contentEquals(fc1, fc2, bufferSize));
+ assertFalse(FileChannels.contentEquals(reset(fc2), reset(fc1),
bufferSize));
+ assertTrue(FileChannels.contentEquals(reset(fc1), reset(fc1),
bufferSize));
+ }
+ // Make the LAST byte different.
+ byte[] bytes3 = bytes1.clone();
+ final int last = bytes3.length - 1;
+ bytes3[last] = reverse(bytes3[last]);
+ Files.write(bigFile3, bytes3);
+ try (FileChannel fc1 = open(bigFile1, null); FileChannel fc3 =
open(bigFile3, null)) {
+ assertFalse(FileChannels.contentEquals(fc1, fc3, bufferSize));
+ assertFalse(FileChannels.contentEquals(reset(fc3), reset(fc1),
bufferSize));
+ // Test just the last byte
+ fc1.position(last);
+ fc3.position(last);
+ assertFalse(FileChannels.contentEquals(fc1, fc3, bufferSize));
+ }
+ // Make the LAST byte equal.
+ bytes3 = bytes1.clone();
+ Files.write(bigFile3, bytes3);
+ try (FileChannel fc1 = open(bigFile1, null); FileChannel fc3 =
open(bigFile3, null)) {
+ // Test just the last byte
+ fc1.position(last);
+ fc3.position(last);
+ assertTrue(FileChannels.contentEquals(fc1, fc3, bufferSize));
+ }
+ // Make a byte in the middle different
+ bytes3 = bytes1.clone();
+ final int middle = bytes3.length / 2;
+ bytes3[middle] = reverse(bytes3[middle]);
+ Files.write(bigFile3, bytes3);
+ try (FileChannel fc1 = open(bigFile1, null); FileChannel fc3 =
open(bigFile3, null)) {
+ assertFalse(FileChannels.contentEquals(fc1, fc3, bufferSize));
+ assertFalse(FileChannels.contentEquals(reset(fc3), reset(fc1),
bufferSize));
+ }
+ } finally {
+ // Delete ASAP
+ Files.deleteIfExists(bigFile1);
+ Files.deleteIfExists(bigFile2);
+ Files.deleteIfExists(bigFile3);
+ }
+ }
}
diff --git
a/src/test/java/org/apache/commons/io/channels/NonBlockingFileChannelProxy.java
b/src/test/java/org/apache/commons/io/channels/NonBlockingFileChannelProxy.java
new file mode 100644
index 000000000..f391a3b05
--- /dev/null
+++
b/src/test/java/org/apache/commons/io/channels/NonBlockingFileChannelProxy.java
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.io.channels;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.nio.channels.ReadableByteChannel;
+
+/**
+ * Simulates a non-blocking file channel by returning 0 from reads every other
call as allowed by {@link ReadableByteChannel} and {@link FileChannel}.
+ */
+public class NonBlockingFileChannelProxy extends FileChannelProxy {
+
+ boolean toggleRead0;
+
+ NonBlockingFileChannelProxy(final FileChannel fileChannel) {
+ super(fileChannel);
+ }
+
+ private boolean flipState() {
+ return toggleRead0 = !toggleRead0;
+ }
+
+ @Override
+ public int read(final ByteBuffer dst) throws IOException {
+ return flipState() ? 0 : super.read(dst);
+ }
+
+ @Override
+ public int read(final ByteBuffer dst, final long position) throws
IOException {
+ flipState();
+ return flipState() ? 0 : super.read(dst, position);
+ }
+
+ @Override
+ public long read(final ByteBuffer[] dsts, final int offset, final int
length) throws IOException {
+ flipState();
+ return flipState() ? 0 : super.read(dsts, offset, length);
+ }
+}