import java.util.LinkedList; import java.util.Queue; public class CircularBuffer { private Queue buffer; private int capacity; public CircularBuffer(int size) { this.buffer = new LinkedList<>(); this.capacity = size; } public T read() throws BufferIOException { if (this.buffer.size() == 0) { throw new BufferIOException("Tried to read from empty buffer"); } return this.buffer.remove(); } public void write(T data) throws BufferIOException { if (this.buffer.size() == this.capacity) { throw new BufferIOException("Tried to write to full buffer"); } this.buffer.add(data); } public void overwrite(T data) { if (this.buffer.size() == this.capacity) { this.buffer.remove(); } this.buffer.add(data); } public void clear() { this.buffer.clear(); } }