私は現在、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();
私が間違いを犯している場所を誰かが見ることができますか?