/** * Tests the readAll method that copies data into an existing array. */ @Test public void testReadAllIntoArray() throws IOException { final int itemCount = 100; final byte[] buffer = new byte[itemCount]; final int readStartOffset = 5; final int readLength = itemCount - readStartOffset - 5; for (int i = 0; i < itemCount; i++) { buffer[i] = (byte) i; } TestInputStream is = new TestInputStream(buffer); byte[] readResult = new byte[itemCount + 10]; int readBytes = StreamHelpers.readAll(is, readResult, readStartOffset, readLength); Assert.assertEquals("Unexpected number of bytes read.", readLength, readBytes); for (int i = 0; i < readResult.length; i++) { if (i < readStartOffset || i >= readStartOffset + readLength) { Assert.assertEquals("readAll wrote data at wrong offset " + i, 0, readResult[i]); } else { int originalOffset = i - readStartOffset; Assert.assertEquals("unexpected value at target index " + i, buffer[originalOffset], readResult[i]); } } }
/** * Tests the readAll method that copies data into an existing array. */ @Test public void testReadAllNewArray() throws IOException { final int itemCount = 100; final byte[] buffer = new byte[itemCount]; for (int i = 0; i < itemCount; i++) { buffer[i] = (byte) i; } byte[] readFullyData = StreamHelpers.readAll(new TestInputStream(buffer), buffer.length); Assert.assertArrayEquals(buffer, readFullyData); AssertExtensions.assertThrows( "readAll accepted a length higher than the given input stream length.", () -> StreamHelpers.readAll(new TestInputStream(buffer), buffer.length + 1), ex -> ex instanceof IllegalArgumentException); }