@Test public void testWrite() { final RingBuffer b = new RingBuffer(4); b.write(new char[] { 1 }, 0, 1); assertEquals(1, b.getHead()); assertTrue(Arrays.equals(new char[] { 1, 0, 0, 0 }, b.getRawBuffer())); b.write(new char[] { 2 }, 0, 1); assertEquals(2, b.getHead()); assertTrue(Arrays.equals(new char[] { 1, 2, 0, 0 }, b.getRawBuffer())); b.write(new char[] { 3, 4 }, 0, 2); assertEquals(0, b.getHead()); assertTrue(Arrays.equals(new char[] { 1, 2, 3, 4 }, b.getRawBuffer())); b.write(new char[] { 5, 6, 7 }, 0, 3); assertEquals(3, b.getHead()); assertTrue(Arrays.equals(new char[] { 5, 6, 7, 4 }, b.getRawBuffer())); b.write(new char[] { 8, 9, 10, 11, 12, 13 }, 0, 6); assertEquals(1, b.getHead()); assertTrue(Arrays.equals(new char[] { 13, 10, 11, 12 }, b.getRawBuffer())); }
public synchronized void write(char[] chars, int start, int length) { int end = start + length; int available = buffer.length - head; while(start < end) { int toWrite = Math.min(end - start, available); System.arraycopy(chars, start, buffer, head, toWrite); head = (head + toWrite) % buffer.length; start += toWrite; available = buffer.length - head; } }
RingBuffer { public synchronized void write(char[] chars, int start, int length) { int end = start + length; int available = buffer.length - head; while(start < end) { int toWrite = Math.min(end - start, available); System.arraycopy(chars, start, buffer, head, toWrite); head = (head + toWrite) % buffer.length; start += toWrite; available = buffer.length - head; } } }
RingBuffer { public synchronized void write(char[] chars, int start, int length) { int end = start + length; int available = buffer.length - head; while(start < end) { int toWrite = Math.min(end - start, available); System.arraycopy(chars, start, buffer, head, toWrite); head = (head + toWrite) % buffer.length; start += toWrite; available = buffer.length - head; } } RingBuffer(int size); }
RingBuffer { public synchronized void write(char[] chars, int start, int length) { int end = start + length; int available = buffer.length - head; while(start < end) { int toWrite = Math.min(end - start, available); System.arraycopy(chars, start, buffer, head, toWrite); head = (head + toWrite) % buffer.length; start += toWrite; available = buffer.length - head; } } RingBuffer(int size); int size(); synchronized void write(char[] chars, int start, int length); synchronized char[] getTail(int count); synchronized char[] getTail(int charsBack, int max); }
RingBuffer { public synchronized void write(char[] chars, int start, int length) { int end = start + length; int available = buffer.length - head; while(start < end) { int toWrite = Math.min(end - start, available); System.arraycopy(chars, start, buffer, head, toWrite); head = (head + toWrite) % buffer.length; start += toWrite; available = buffer.length - head; } } RingBuffer(int size); int size(); synchronized void write(char[] chars, int start, int length); synchronized char[] getTail(int count); synchronized char[] getTail(int charsBack, int max);  }