I have written my own for practice and also some tweaks for my specific requirement.  There is an insert which can copy a range (well array copy really) which I am hoping is more efficient than individual element insert.Can anyone make any comments specifically as regards efficiency and code elegance.  I need to use this with Java 1.6 and am getting a warning on the array = newArray(size); line in the constructor.  But that is the only issue I am currently aware of.import java.util.Arrays;  //copyOfpublic class CircularBuffer<T> {    private T[] array;  //storage    private int put_idx, get_idx, count; //get/put & internal num elements counter    private final int size;   //size of internal array storage    public CircularBuffer(int size) {        get_idx = 0;        put_idx = 0;        count   = 0;        this.size = size;        //Type safety: A generic array of T is created for a varargs parameter - investigate how to remove warning        array = newArray(size);     }    //@SafeVarargs - ok in java 1.7    static <T> T[] newArray(int length, T... array)    {        return Arrays.copyOf(array, length);    }        void reset(){        get_idx=put_idx=count=0;    }    public boolean empty() { return count == 0; }    public boolean full() { return count == size; }    int get_count() { return count; }    //inserts a single new element    public void insert(T element)    {        if(full())            throw new ArrayIndexOutOfBoundsException(buffer full);        array[put_idx++] = element;        put_idx %= size;        ++count;    }    //insert array of specified length    public void insert(T[] thearray) {        if(thearray.length + count > size)            throw new ArrayIndexOutOfBoundsException(insufficient space to insert array);             //first copy to end - contiguous array copy        int remaining = size - put_idx;        if(remaining >= thearray.length) {            System.arraycopy(thearray, 0, array, put_idx, thearray.length);            put_idx += thearray.length;            put_idx %= size;        } else {            System.arraycopy(thearray, 0, array, put_idx, remaining);            int rest = thearray.length - remaining;            System.arraycopy(thearray, remaining, array, 0, rest);            put_idx = rest;        }        count += thearray.length;    }    //get the oldest element, move read pointer    public T get()    {        if(empty())            throw new ArrayIndexOutOfBoundsException(buffer empty);        final T ret = array[get_idx++];        get_idx %= size;        --count;        return ret;    }  }Code to exercise class:public class TestCircularBuffer {    static public void main(String args[]) {        new TestCircularBuffer().go();    }    void go() {        int SIZE = 1000;        int arr[] = new int[SIZE];        for(int i = 0; i < SIZE; ++i) {            arr[i] = i;        }        CircularBuffer<Integer> cb = new CircularBuffer<Integer>(10);        //exercise circ buffer        int i = 0;        while(i < SIZE) {            while(!cb.full())                cb.insert(arr[i++]);            while(!cb.empty())                System.out.print(cb.get() +  );        }        //now exercise array insert        System.out.println(\nrange insert test);        Integer marr[] = {1,2,3,4};        i = 0;        while(i < 20) {            //will only be able to fit 2 lots            cb.insert(marr);            cb.insert(marr);            while(!cb.empty())                System.out.print(cb.get() +  );            ++i;        }    }}
Circular buffer implementation for buffering TCP socket messages
java;circular list
I agree with everything from palacsint (7. could be discussed. If it is a requirement, then it is fine)I will add some more thoughts.import java.util.Arrays;  //copyOfThis comment is not necessary. Even more, noone (with an IDE) will read comments in the import section.public class CircularBuffer<T> {As far as I see it, your implementation is a fixed size (FIFO) queue. If I am right, I would choose this name instead of CircularBuffer.With this name, even if it is not well defined, I would expect some circularity. There is none. (Not for the caller, the inside details are not interesting for anyone using this class.)One could even argument, that using java collection (implement an interface, using collection classes) would be a good way to integrate it inside the java world.private T[] array;  //storageprivate int put_idx, get_idx, count; //get/put & internal num elements counterprivate final int size;   //size of internal array storageI would change the names, the array is in fact the buffer, put_idx the head, get_idx the tail, count the number of elements and size is not needed:private final T[] buffer;private int headIndex = 0;private int tailIndex = 0;private int numberOfElements = 0;void reset(){    get_idx=put_idx=count=0;}Do you need this method? If no, remove it. If you need it, it could be a good idea to clear the buffer, too. The garbage collector would appreciate this.public boolean empty() { return count == 0; }public boolean full() { return count == size; }A typical and good name for a method returning a method is isSomething() or hasSomething(). So in this case isEmpty(), isFull().int get_count() { return count; }If we look at java collections, the typical and expected name would bet getSize()//inserts a single new elementpublic void insert(T element){    if(full())        throw new ArrayIndexOutOfBoundsException(buffer full);    array[put_idx++] = element;    put_idx %= size;    ++count;}I would throw an IllegalStateException exception. ArrayIndexOutOfBoundsException is not true, the buffer is full, it can not be out of bounds, unless the implementation is wrong.In this case, the expected state is not full, but it is full.Remark: Depending on the requirements, you could start to overwrite old entries. This would be typical for a circular buffer.I would add some javadoc comments, at least to tell the caller about the exception he could catch.//insert array of specified lengthpublic void insert(T[] thearray) {This comment is already clear from the method signature.If we look at java collections again, I would rename it to addAll.Which is quite important if you try to add for example null. Does it go to insert(T element) or insert(T[] thearray) ? Should it be there?And again, javadoc    if(thearray.length + count > size)        throw new ArrayIndexOutOfBoundsException(insufficient space to insert array);     See above    //first copy to end - contiguous array copy    int remaining = size - put_idx;    if(remaining >= thearray.length) {        System.arraycopy(thearray, 0, array, put_idx, thearray.length);        put_idx += thearray.length;        put_idx %= size;    } else {        System.arraycopy(thearray, 0, array, put_idx, remaining);        int rest = thearray.length - remaining;        System.arraycopy(thearray, remaining, array, 0, rest);        put_idx = rest;    }This could be simplified, you do not need 2 complex cases:     final int remaining = Math.min(thearray.length, size - put_idx);    System.arraycopy(thearray, 0, buffer, put_idx, remaining);    final int rest = thearray.length - remaining;    System.arraycopy(thearray, remaining, buffer, 0, rest);    put_idx = (put_idx + thearray.length) % size;    numberOfElements += thearray.length;And because the name and argument behavior of System.arraycopy is awful, I would introduce a small method. So anyone reading it could understand it without spending time on thinking about arraycopy:private void copyNumberOfElementsFromArraySourceStartToBuffer(final int numberOfElements, final T[] arraySource, final int sourceStart) {    System.arraycopy(arraySource, sourceStart, buffer, head, numberOfElements);    head = (head + numberOfElements) % buffer.length;}//get the oldest element, move read pointerpublic T get(){    if(empty())        throw new ArrayIndexOutOfBoundsException(buffer empty);    final T ret = array[get_idx++];    get_idx %= size;    --count;    return ret;}  If we look at java collections again, I would rename it too removeOldest(). Or if you want to be very specific removeAndGetOldest().It is not the typical expectation to remove the element if get is called. If you do so, add at least some javadoc comment indicating this behavior.Exception: See above.arr[var++]This is only a personal opinion: I would avoid using the postincrement operator overall. This case looks simple, but you can easily get complicated cases because of side effects in all languages.And with current compilers, it does (in most of the scenarios) not make any change in size, speed or whatever.So I would go for readability.All together, it could be something like this:/** * A circular buffer. This is practically a fixed size FIFO queue. Which means you can enter a maximum amount of overall elements and can get the oldest. */public class CircularBuffer<T> {    private final T[] buffer;    private int headIndex = 0;    private int tailIndex = 0;    private int numberOfElements = 0;    public CircularBuffer(final int size) {        buffer = newArray(size);    }    @SuppressWarnings(unchecked)    // generics and arrays do not mix. we should be safe here.    private T[] newArray(final int length) {        return (T[]) new Object[length];    }    public boolean isEmpty() {        return numberOfElements == 0;    }    public boolean isFull() {        return numberOfElements == buffer.length;    }    /** Returns the current number of elements inside the buffer */    public int getSize() {        return numberOfElements;    }    /**     * Inserts the given element.     * @throws IllegalStateException if buffer has not enough free space     */    public void add(final T newElement) {        if (isFull())            throw new IllegalStateException(buffer full);        buffer[headIndex] = newElement;        headIndex = (headIndex + 1) % buffer.length;        ++numberOfElements;    }    /**     * Inserts the given elements.     * @throws IllegalStateException if buffer has not enough free space     */    public void addAll(final T[] newElements) {        if (newElements.length + numberOfElements > buffer.length)            throw new IllegalStateException(insufficient space to insert array);        // or: call add() method for all elements. Which should be the favored way.        final int elementsToFillAtHead = Math.min(newElements.length, buffer.length - headIndex);        copyNumberOfElementsFromArraySourceStartToBuffer(elementsToFillAtHead, newElements, 0);        final int rest = newElements.length - elementsToFillAtHead;        copyNumberOfElementsFromArraySourceStartToBuffer(rest, newElements, elementsToFillAtHead);        numberOfElements += newElements.length;    }    private void copyNumberOfElementsFromArraySourceStartToBuffer(final int numberOfElements, final T[] arraySource, final int sourceStart) {        System.arraycopy(arraySource, sourceStart, buffer, headIndex, numberOfElements);        headIndex = (headIndex + numberOfElements) % buffer.length;    }    /**      * Get and remove the oldest element     * @throws  IllegalStateException if buffer is empty     */    public T removeOldest() {        if (isEmpty())            throw new IllegalStateException(buffer empty);        final T oldest = buffer[tailIndex];        tailIndex = (tailIndex + 1) % buffer.length;        --numberOfElements;        return oldest;    }}