<ByteQueue.java>
package com.example.water_bottle;
public class ByteQueue {
/**
* Constructor for ByteQueue.
* @param size int
*/
public ByteQueue(int size) {
mBuffer = new byte[size];
}
/**
* Method getBytesAvailable.
* @return int */
public int getBytesAvailable() {
synchronized(this) {
return mStoredBytes;
}
}
/**
* Method read.
* @param buffer byte[]
* @param offset int
* @param length int
* @return int * @throws InterruptedException */
public int read(byte[] buffer, int offset, int length)
throws InterruptedException {
if (length + offset > buffer.length) {
throw
new IllegalArgumentException("length + offset > buffer.length");
}
if (length < 0) {
throw
new IllegalArgumentException("length < 0");
}
if (length == 0) {
return 0;
}
synchronized(this) {
while (mStoredBytes == 0) {
wait();
}
int totalRead = 0;
int bufferLength = mBuffer.length;
boolean wasFull = bufferLength == mStoredBytes;
while (length > 0 && mStoredBytes > 0) {
int oneRun = Math.min(bufferLength - mHead, mStoredBytes);
int bytesToCopy = Math.min(length, oneRun);
System.arraycopy(mBuffer, mHead, buffer, offset, bytesToCopy);
mHead += bytesToCopy;
if (mHead >= bufferLength) {
mHead = 0;
}
mStoredBytes -= bytesToCopy;
length -= bytesToCopy;
offset += bytesToCopy;
totalRead += bytesToCopy;
}
if (wasFull) {
notify();
}
return totalRead;
}
}
/**
* Method write.
* @param buffer byte[]
* @param offset int
* @param length int
* @throws InterruptedException */
public void write(byte[] buffer, int offset, int length)
throws InterruptedException {
if (length + offset > buffer.length) {
throw
new IllegalArgumentException("length + offset > buffer.length");
}
if (length < 0) {
throw
new IllegalArgumentException("length < 0");
}
if (length == 0) {
return;
}
synchronized(this) {
int bufferLength = mBuffer.length;
boolean wasEmpty = mStoredBytes == 0;
while (length > 0) {
while(bufferLength == mStoredBytes) {
wait();
}
int tail = mHead + mStoredBytes;
int oneRun;
if (tail >= bufferLength) {
tail = tail - bufferLength;
oneRun = mHead - tail;
} else {
oneRun = bufferLength - tail;
}
int bytesToCopy = Math.min(oneRun, length);
System.arraycopy(buffer, offset, mBuffer, tail, bytesToCopy);
offset += bytesToCopy;
mStoredBytes += bytesToCopy;
length -= bytesToCopy;
}
if (wasEmpty) {
notify();
}
}
}
private byte[] mBuffer;
private int mHead;
private int mStoredBytes;
}
<BluetoothService> 의 ConnectedThread
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;
while (true) {
try {
bytes = mmInStream.read(buffer);
mByteQueue.write(buffer, 0, bytes);
mHandler.sendMessage(mHandler.obtainMessage(ServiceClass.MESSAGE_READ));
}catch (InterruptedException e) {
//@TODO log the exception;
break;
}catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}
로그창 오류
12-22 18:14:48.661: E/AndroidRuntime(17940): FATAL EXCEPTION: Thread-10273
12-22 18:14:48.661: E/AndroidRuntime(17940): Process: com.example.water_bottle, PID: 17940
12-22 18:14:48.661: E/AndroidRuntime(17940): java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.water_bottle.ByteQueue.write(byte[], int, int)' on a null object reference
12-22 18:14:48.661: E/AndroidRuntime(17940): at com.example.water_bottle.BluetoothService$ConnectedThread.run(BluetoothService.java:397)