В настоящее время я пытаюсь реализовать класс для использования в качестве буфера для входящих данных через соединение Bluetooth:
public class IncomingBuffer {
private static final String TAG = "IncomingBuffer";
private BlockingQueue<byte[]> inBuffer;
public IncomingBuffer(){
inBuffer = new LinkedBlockingQueue<byte[]>();
Log.i(TAG, "Initialized");
}
public int getSize(){
int size = inBuffer.size();
byte[] check=new byte[1];
String total=" ";
if(size>20){
while(inBuffer.size()>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();
}
}
У меня проблемы с этим буфером. Всякий раз, когда я добавляю байтовый массив, все байтовые массивы в буфере становятся такими же, как я только что добавил.
Я пытался использовать блокировку очереди того же типа в остальной части моего кода (не делая его своим собственным классом), и он отлично работает. Кажется, проблема в том, что я использую этот класс.
Способ объявления класса следующий:
private IncomingBuffer ringBuffer;
ringBuffer = new IncomingBuffer();
Кто-нибудь может увидеть, где я совершаю ошибку?