Reputation: 3133
In a Bluetooth app , i'm receiving data from a external device in byte[]. then putting data into ByteBuffer for further use
like shown below
ByteBuffer localByteBuffer = null;
byte[] arrayOfByte = new byte[4096];
int bytes = 0;
Log.d("ZeoTest", "++++ Listening...");
while (true) {
try {
bytes = in.read(arrayOfByte);
localByteBuffer = ByteBuffer.wrap(arrayOfByte);
localByteBuffer.get(arrayOfByte, 0, arrayOfByte.length);
Log.d("Zeo", "input :"+((localByteBuffer)));
String data =bytesToHex(arrayOfByte) ;
Log.d("Zeo", "input stream :"+(new String(data)));
Log.d("ZeoTest", "++++ Read "+ bytes +" bytes");
} catch (IOException e) {
e.printStackTrace();
try { Thread.sleep(0); } catch (InterruptedException ie) {}
} Log.d("ZeoTest", "++++ Done: test()");
onReceive(localByteBuffer);} }
In onReceive methode checking header is valid or no...
as shown below
public static boolean isValidHeader(EnhancedByteBuffer paramEnhancedByteBuffer)
{
boolean bool = false;
if (paramEnhancedByteBuffer.remaining() < 12);
while (true)
{
paramEnhancedByteBuffer.order(ByteOrder.LITTLE_ENDIAN);
int i = paramEnhancedByteBuffer.position();
long l = paramEnhancedByteBuffer.getUint32();
paramEnhancedByteBuffer.getUint16();
int j = paramEnhancedByteBuffer.getUint8();
int k = paramEnhancedByteBuffer.getUint8();
paramEnhancedByteBuffer.getBoolean();
paramEnhancedByteBuffer.getUint8();
int m = paramEnhancedByteBuffer.getUint16();
paramEnhancedByteBuffer.position(i);
if ((l == 1196641608L) && (j <= 2) && (k < 14) && (m == MessageType.getMessageContentSize(k)))
bool = true;
return bool;
}
}
but getting buffer under flow exception on getUint32 method
what may be possible reason...????
logcat....
FATAL EXCEPTION: main
java.nio.BufferUnderflowException
at java.nio.HeapByteBuffer.getInt(HeapByteBuffer.java:117)
at com.myzeo.zeo.utility.EnhancedByteBuffer.getUint32(EnhancedByteBuffer.java:87)
at com.myzeo.bluetooth.ZeoTest.isValidHeader(ZeoTest.java:195)
at com.myzeo.bluetooth.ZeoTest.isValidHeader(ZeoTest.java:183)
at com.myzeo.bluetooth.ZeoTest.onReceive(ZeoTest.java:153)
at com.myzeo.bluetooth.ZeoTest.setup(ZeoTest.java:125)
at com.myzeo.bluetooth.ZeoTest.access$0(ZeoTest.java:64)
at com.myzeo.bluetooth.ZeoTest$1.onClick(ZeoTest.java:47)
at android.view.View.performClick(View.java:2629)
at android.view.View$PerformClick.run(View.java:9374)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Upvotes: 2
Views: 4977
Reputation: 3283
You didn't consider the position of the reading head.
If you want to get an integer value, and the reading head is at position 4 of a ByteBuffer that has a capacity of 4, then you will get a BufferUnderflowException, because he can not read 4 more bytes for the integer value to be returned.
You have to give him the reading position (non-persistent): getInt( position ); e.g.: getInt( 0 );
Upvotes: 2
Reputation: 16603
The possible reason is that you read more bytes from the buffer than are available. Check the buffer's position after each read() in debugger to see which call advances the buffer more than it should (maybe readBoolean())
Upvotes: 3