/** * Tests the functionality of writeTo and readFrom. */ @Test public void testWriteToReadFrom() throws IOException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int count = 10; ArrayList<ByteArraySegment> sourceSegments = new ArrayList<>(); for (int i = 0; i < count; i++) { ByteArraySegment s = new ByteArraySegment(createFormattedBuffer()); sourceSegments.add(s); s.writeTo(outputStream); } InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); for (int i = 0; i < count; i++) { ByteArraySegment s = sourceSegments.get(i); ByteArraySegment t = new ByteArraySegment(new byte[s.getLength()]); t.readFrom(inputStream); Assert.assertEquals("Source and target lengths differ.", s.getLength(), t.getLength()); for (int j = 0; j < s.getLength(); j++) { if (t.get(j) != s.get(j)) { Assert.fail(String.format("Source at target differ at index %d.", j)); } } } }