title: Creating a Buffer Class Using a Blocking Queue in Android
body: <p>I am currently trying to implement a class to use as a buffer for incoming data over a bluetooth connection:</p>
  
  <pre><code>public class IncomingBuffer {
  
  private static final String TAG = "IncomingBuffer";
  
  private BlockingQueue&lt;byte[]&gt; inBuffer;
  
  public IncomingBuffer(){
      inBuffer = new LinkedBlockingQueue&lt;byte[]&gt;();
      Log.i(TAG, "Initialized");
  }
  
  public  int getSize(){
      int size = inBuffer.size();
      byte[] check=new byte[1];
      String total=" ";
      if(size&gt;20){
          while(inBuffer.size()&gt;1){
              check=inBuffer.remove();
              total=total+ " " +check[0];
          }
          Log.i(TAG, "All the values inside are "+total);
      }
      size=inBuffer.size();
      return size;
  }
  
  //Inserts the specified element into this queue, if possible. Returns True if successful.
  public boolean insert(byte[] element){
      Log.i(TAG, "Inserting "+element[0]);
      boolean success=inBuffer.offer(element);
      return success;
  }
  
  //Retrieves and removes the head of this queue, or null if this queue is empty.
  public byte[] retrieve(){       
      Log.i(TAG, "Retrieving");
      return inBuffer.remove();
  
  }
  
  // Retrieves, but does not remove, the head of this queue, returning null if this queue is empty.
  public byte[] peek(){
  
      Log.i(TAG, "Peeking");
      return inBuffer.peek();
  }   
  }
  </code></pre>
  
  <p>I'm having problems with this buffer. Whenever I add a byte array, all of the byte arrays in the buffer become the same as the one I just added.</p>
  
  <p>I have tried using the same type of blocking queue within the rest of my code (without making it its own class), and it works fine. The problem seems to be when I use this class.</p>
  
  <p>The way I declare the class is the following:</p>
  
  <pre><code>private IncomingBuffer ringBuffer;
  ringBuffer = new IncomingBuffer();
  </code></pre>
  
  <p>Can anyone see where I am making a mistake? </p>