CircularBuffer { public void readAll(final OutputStream outputStream) throws IOException { if (isEmpty()){ return; } readChunk(outputStream, availableReadLength); } CircularBuffer(final int size); void write(final byte data); void readAll(final OutputStream outputStream); void readChunk(final OutputStream outputStream, final int chunkSize); boolean isFull(); boolean isEmpty(); int getAvailableDataLength(); int getBufferSize(); void reset();  }
@Test public void testReadAll() throws Exception { ByteArrayOutputStream readBaos = new ByteArrayOutputStream(); byte[] readBytes; final CircularBuffer buffer = new CircularBuffer(10); log.info("Buffer initial status:\n" + circularBufferToString(buffer) + "\n"); buffer.readAll(readBaos); readBytes = readBaos.toByteArray(); log.info("Buffer after reading all:\n" + circularBufferToString(buffer)); log.info("Read bytes: " + TestUtils.printBytesHexEncoded(readBytes) + "\n"); assertArrayEquals(new byte[]{}, readBaos.toByteArray()); assertEquals(0, buffer.startValidDataIndex); assertEquals(0, buffer.nextAvailablePosition); assertEquals(0, buffer.availableReadLength); buffer.reset(); readBaos.reset(); log.info("Buffer after recycle:\n" + circularBufferToString(buffer) + "\n"); byte[] chunk1 = {0x01}; writeDataToCircularBuffer(buffer, chunk1); log.info("Buffer after writing 1 byte:\n" + circularBufferToString(buffer) + "\n"); buffer.readAll(readBaos); readBytes = readBaos.toByteArray(); log.info("Buffer after reading all:\n" + circularBufferToString(buffer)); log.info("Read bytes: " + TestUtils.printBytesHexEncoded(readBytes) + "\n"); assertArrayEquals(chunk1, readBaos.toByteArray()); assertEquals(1, buffer.startValidDataIndex); assertEquals(1, buffer.nextAvailablePosition); assertEquals(0, buffer.availableReadLength); buffer.reset(); readBaos.reset(); log.info("Buffer after recycle:\n" + circularBufferToString(buffer) + "\n"); byte[] chunk2 = {0x01, 0x02, 0x03, 0x04, 0x05}; writeDataToCircularBuffer(buffer, chunk2); log.info("Buffer after writing 5 byte:\n" + circularBufferToString(buffer) + "\n"); buffer.readAll(readBaos); readBytes = readBaos.toByteArray(); log.info("Buffer after reading all:\n" + circularBufferToString(buffer)); log.info("Read bytes: " + TestUtils.printBytesHexEncoded(readBytes) + "\n"); assertArrayEquals(chunk2, readBytes); assertEquals(5, buffer.startValidDataIndex); assertEquals(5, buffer.nextAvailablePosition); assertEquals(0, buffer.availableReadLength); readBaos.reset(); byte[] chunk3 = {0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12}; writeDataToCircularBuffer(buffer, chunk3); log.info("Buffer after writing 7 byte more:\n" + circularBufferToString(buffer) + "\n"); buffer.readAll(readBaos); readBytes = readBaos.toByteArray(); log.info("Buffer after reading all:\n" + circularBufferToString(buffer)); log.info("Read bytes: " + TestUtils.printBytesHexEncoded(readBytes) + "\n"); assertArrayEquals(chunk3, readBytes); assertEquals(2, buffer.startValidDataIndex); assertEquals(2, buffer.nextAvailablePosition); assertEquals(0, buffer.availableReadLength); }