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
commit e48e3e3fd06995078a1d1832124c5bc77df28969 Author: Gary D. Gregory <garydgreg...@gmail.com> AuthorDate: Sun Apr 6 13:51:28 2025 -0400 Migrate from deprecated code Add a test to check that available() doesn't have a side effect on read() --- .../commons/io/input/AbstractInputStreamTest.java | 25 +++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/apache/commons/io/input/AbstractInputStreamTest.java b/src/test/java/org/apache/commons/io/input/AbstractInputStreamTest.java index 5de74b06b..0cf985f81 100644 --- a/src/test/java/org/apache/commons/io/input/AbstractInputStreamTest.java +++ b/src/test/java/org/apache/commons/io/input/AbstractInputStreamTest.java @@ -24,6 +24,8 @@ import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Supplier; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.RandomUtils; @@ -56,7 +58,7 @@ static int[] getArrayLengths() { @BeforeEach public void setUp() throws IOException { // Create a byte array of size 2 MB with random bytes - actualRandomBytes = RandomUtils.insecure().nextBytes(2 * 1024 * 1024); + actualRandomBytes = RandomUtils.insecure().randomBytes(2 * 1024 * 1024); expectedRandomBytes = actualRandomBytes; inputFile = Files.createTempFile("temp-file", ".tmp"); Files.write(inputFile, actualRandomBytes); @@ -171,6 +173,27 @@ public void testReadOneByOne() throws IOException { } } + @Test + public void testReadOneByOneCheckAvailable() throws IOException { + final AtomicInteger refII = new AtomicInteger(); + for (int idxInputs = 0; idxInputs < inputStreams.length; idxInputs++) { + refII.set(idxInputs); + final AtomicInteger refIB = new AtomicInteger(); + @SuppressWarnings("resource") + final InputStream inputStream = inputStreams[idxInputs]; + for (int idxBytes = 0; idxBytes < expectedRandomBytes.length; idxBytes++) { + refIB.set(idxBytes); + final byte randomByte = expectedRandomBytes[idxBytes]; + // Check that available() doesn't have a side effect on read() + final int available = inputStream.available(); + final Supplier<String> messageSupplier = () -> String.format("idxInputs = %,d, idxBytes = %,d, available = %,d", refII.get(), refIB.get(), + available); + assertTrue(available >= 0, messageSupplier); + assertEquals(randomByte, (byte) inputStream.read()); + } + } + } + @Test public void testReadPastEOF() throws IOException { final InputStream is = inputStreams[0];