|
24 | 24 | import java.io.InputStream; |
25 | 25 | import java.nio.file.Files; |
26 | 26 | import java.nio.file.Path; |
| 27 | +import java.util.concurrent.atomic.AtomicInteger; |
| 28 | +import java.util.function.Supplier; |
27 | 29 |
|
28 | 30 | import org.apache.commons.io.IOUtils; |
29 | 31 | import org.apache.commons.lang3.RandomUtils; |
@@ -56,7 +58,7 @@ static int[] getArrayLengths() { |
56 | 58 | @BeforeEach |
57 | 59 | public void setUp() throws IOException { |
58 | 60 | // Create a byte array of size 2 MB with random bytes |
59 | | - actualRandomBytes = RandomUtils.insecure().nextBytes(2 * 1024 * 1024); |
| 61 | + actualRandomBytes = RandomUtils.insecure().randomBytes(2 * 1024 * 1024); |
60 | 62 | expectedRandomBytes = actualRandomBytes; |
61 | 63 | inputFile = Files.createTempFile("temp-file", ".tmp"); |
62 | 64 | Files.write(inputFile, actualRandomBytes); |
@@ -171,6 +173,27 @@ public void testReadOneByOne() throws IOException { |
171 | 173 | } |
172 | 174 | } |
173 | 175 |
|
| 176 | + @Test |
| 177 | + public void testReadOneByOneCheckAvailable() throws IOException { |
| 178 | + final AtomicInteger refII = new AtomicInteger(); |
| 179 | + for (int idxInputs = 0; idxInputs < inputStreams.length; idxInputs++) { |
| 180 | + refII.set(idxInputs); |
| 181 | + final AtomicInteger refIB = new AtomicInteger(); |
| 182 | + @SuppressWarnings("resource") |
| 183 | + final InputStream inputStream = inputStreams[idxInputs]; |
| 184 | + for (int idxBytes = 0; idxBytes < expectedRandomBytes.length; idxBytes++) { |
| 185 | + refIB.set(idxBytes); |
| 186 | + final byte randomByte = expectedRandomBytes[idxBytes]; |
| 187 | + // Check that available() doesn't have a side effect on read() |
| 188 | + final int available = inputStream.available(); |
| 189 | + final Supplier<String> messageSupplier = () -> String.format("idxInputs = %,d, idxBytes = %,d, available = %,d", refII.get(), refIB.get(), |
| 190 | + available); |
| 191 | + assertTrue(available >= 0, messageSupplier); |
| 192 | + assertEquals(randomByte, (byte) inputStream.read()); |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + |
174 | 197 | @Test |
175 | 198 | public void testReadPastEOF() throws IOException { |
176 | 199 | final InputStream is = inputStreams[0]; |
|
0 commit comments