CircularBuffer { public void write(final byte data){ buffer[nextAvailablePosition] = data; nextAvailablePosition = forwards(nextAvailablePosition); if (availableReadLength > 0 && nextAvailablePosition == startValidDataIndex + 1){ startValidDataIndex = forwards(startValidDataIndex); } updateAvailableReadLength(true); } 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 testWrite_bufferSize_1() throws Exception { final CircularBuffer buffer = new CircularBuffer(1); assertEquals(0, buffer.startValidDataIndex); assertEquals(0, buffer.nextAvailablePosition); assertEquals(0, buffer.availableReadLength); for(int i=0; i<10; i++){ buffer.write((byte)i); assertEquals(0, buffer.startValidDataIndex); assertEquals(0, buffer.nextAvailablePosition); assertEquals(1, buffer.availableReadLength); assertEquals((byte)i,buffer.buffer[0]); } }
@Test public void testWrite() throws Exception { final CircularBuffer buffer = new CircularBuffer(10); log.info("Buffer initial status:\n" + circularBufferToString(buffer) + "\n"); assertEquals(0, buffer.startValidDataIndex); assertEquals(0, buffer.nextAvailablePosition); assertEquals(0, buffer.availableReadLength); byte[] chunk1 = {0x01}; writeDataToCircularBuffer(buffer, chunk1); log.info("Buffer after writing 1 byte:\n" + circularBufferToString(buffer) + "\n"); assertEquals(0, buffer.startValidDataIndex); assertEquals(1, buffer.nextAvailablePosition); assertEquals(1, buffer.availableReadLength); byte[] chunk2 = {0x02, 0x03, 0x04, 0x05}; writeDataToCircularBuffer(buffer, chunk2); log.info("Buffer after writing 5 bytes:\n" + circularBufferToString(buffer) + "\n"); assertEquals(0, buffer.startValidDataIndex); assertEquals(5, buffer.nextAvailablePosition); assertEquals(5, buffer.availableReadLength); byte[] chunk3 = {0x06, 0x07, 0x08, 0x09}; writeDataToCircularBuffer(buffer, chunk3); log.info("Buffer after writing 9 bytes:\n" + circularBufferToString(buffer) + "\n"); assertEquals(0, buffer.startValidDataIndex); assertEquals(9, buffer.nextAvailablePosition); assertEquals(9, buffer.availableReadLength); byte[] chunk4 = {0x10}; writeDataToCircularBuffer(buffer, chunk4); log.info("Buffer after writing 10 bytes:\n" + circularBufferToString(buffer) + "\n"); assertEquals(0, buffer.startValidDataIndex); assertEquals(0, buffer.nextAvailablePosition); assertEquals(10, buffer.availableReadLength); byte[] chunk5 = {0x11}; writeDataToCircularBuffer(buffer, chunk5); log.info("Buffer after writing 11 bytes:\n" + circularBufferToString(buffer) + "\n"); assertEquals(1, buffer.startValidDataIndex); assertEquals(1, buffer.nextAvailablePosition); assertEquals(10, buffer.availableReadLength); byte[] chunk6 = {0x12}; writeDataToCircularBuffer(buffer, chunk6); log.info("Buffer after writing 12 bytes:\n" + circularBufferToString(buffer) + "\n"); assertEquals(2, buffer.startValidDataIndex); assertEquals(2, buffer.nextAvailablePosition); assertEquals(10, buffer.availableReadLength); }