首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BufferUnderflowException

BufferUnderflowException
EN

Stack Overflow用户
提问于 2013-02-11 06:43:08
回答 2查看 4.5K关注 0票数 2

在蓝牙应用程序中,我从byte[]中的外部设备接收数据。然后将数据放入ByteBuffer以供进一步使用

如下所示

代码语言:javascript
复制
        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);}   }

在onReceive中,检查标头是否有效。

如下所示

代码语言:javascript
复制
  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;
    }
  }

但是在getUint32方法中在流异常下获取缓冲区

可能是什么原因.?

logcat.

代码语言:javascript
复制
   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)
EN

回答 2

Stack Overflow用户

发布于 2013-02-11 06:45:49

可能的原因是您从缓冲区中读取的字节比可用的字节多。在调试器中的每个read()之后检查缓冲区的位置,看看哪个调用比它更多地推进了缓冲区(可能是readBoolean())

票数 3
EN

Stack Overflow用户

发布于 2013-10-10 09:13:15

你没有考虑到阅读头的位置

如果您想获得一个整数值,并且读取头位于容量为4的ByteBuffer的第4位置,那么您将得到一个BufferUnderflowException,因为他不能再读取4个字节来返回整数值。

你必须给他阅读的位置(非持久性):getInt(位置);,例如: getInt( 0 );

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14807125

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档