【问题标题】:ByteArrayBuffer throws IndexOutOfBounds Exception - cannot get the causeByteArrayBuffer 抛出 IndexOutOfBounds 异常 - 无法获取原因
【发布时间】:2012-10-25 08:35:15
【问题描述】:

我正在通过 LocalBroadcastManager 在 Android 中接收一个 ByteArray。它工作正常,但是我分块发送。

当我想合并块时,我得到一个错误。

代码如下:

else if (message.equals(BtConstants.BtStateUpdates.BT_UPDATE_STATE_RXRAW_COMPLETE.getString())) {
    Log.i(TAG, "length [" + Integer.toString(picRaw.length()) + "] / capacity [" + Integer.toString(picRaw.capacity()) + "]");
    Log.i(TAG,"append" + Integer.toString(intent.getExtras().getByteArray(BtConstants.btBytePayload).length));              
    picRaw.append(intent.getExtras().getByteArray(BtConstants.btBytePayload), picRaw.length(), 
    intent.getExtras().getByteArray(BtConstants.btBytePayload).length);                 
    String s = "";
    try {
        s = new String(picRaw.buffer(), "US-ASCII");
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Log.i(TAG, "RCVD RAW: " + s);   
}

这是错误。请注意,一开始它会附加较小的 650 字节,但随后会在附加较大的值时失败。

**10-25 11:21:40.670: I/BluetoothService(9537): length [0] / capacity [15000]

10-25 11:21:40.675: I/BluetoothService(9537): append665**

Works FINE so far

10-25 11:21:40.680: V/BluetoothSocket.cpp(9537): readNative

10-25 11:21:40.680: I/BluetoothService(9537): RCVD RAW: �..edited out for brevity.�

10-25 11:21:40.680: D/BluetoothService(9537): Rcvd broadcast: com.test.ppt.bt_state_raw_connected 

**10-25 11:21:40.695: I/BluetoothService(9537): length [665] / capacity [15000]

10-25 11:21:40.695: I/BluetoothService(9537): append5320

10-25 11:21:40.695: D/AndroidRuntime(9537): Shutting down VM

10-25 11:21:40.695: W/dalvikvm(9537): threadid=1: thread exiting with uncaught exception (group=0x40c3c1f8)

10-25 11:21:40.730: E/AndroidRuntime(9537): FATAL EXCEPTION: main

10-25 11:21:40.730: E/AndroidRuntime(9537): java.lang.IndexOutOfBoundsException**

10-25 11:21:40.730: E/AndroidRuntime(9537):     at org.apache.http.util.ByteArrayBuffer.append(ByteArrayBuffer.java:68)

10-25 11:21:40.730: E/AndroidRuntime(9537):     at com.test.ppt.btService.BluetoothService$1.onReceive(BluetoothService.java:217)

10-25 11:21:40.730: E/AndroidRuntime(9537):     at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:297)

10-25 11:21:40.730: E/AndroidRuntime(9537):     at android.support.v4.content.LocalBroadcastManager.access$000(LocalBroadcastManager.java:46)

10-25 11:21:40.730: E/AndroidRuntime(9537):     at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:116)

10-25 11:21:40.730: E/AndroidRuntime(9537):     at android.os.Handler.dispatchMessage(Handler.java:99)

10-25 11:21:40.730: E/AndroidRuntime(9537):     at android.os.Looper.loop(Looper.java:137)

10-25 11:21:40.730: E/AndroidRuntime(9537):     at android.app.ActivityThread.main(ActivityThread.java:4507)

10-25 11:21:40.730: E/AndroidRuntime(9537):     at java.lang.reflect.Method.invokeNative(Native Method)

10-25 11:21:40.730: E/AndroidRuntime(9537):     at java.lang.reflect.Method.invoke(Method.java:511)

10-25 11:21:40.730: E/AndroidRuntime(9537):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)

10-25 11:21:40.730: E/AndroidRuntime(9537):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)

10-25 11:21:40.730: E/AndroidRuntime(9537):     at dalvik.system.NativeStart.main(Native Method)

初始容量是15000——应该够用了。

【问题讨论】:

    标签: android bytearray byte indexoutofboundsexception


    【解决方案1】:
      try {
                    s = new String(picRaw.buffer(), "US-ASCII");
                } 
    catch (ArrayIndexOutOfBoundsException e) {
                // TODO: handle exception
            }
            catch (IndexOutOfBoundsException e) {
                // TODO: handle exception
            }
    
    catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    

    希望以上代码对您有所帮助。

    【讨论】:

      【解决方案2】:

      我想我找到了解决方案here。 ByteArrayBuffer.append 的工作方式与我预期的不同。 引用此新文档:

      void append(char[] b, int off, int len)

            Appends len chars to this buffer from the given source array starting at index off.
      

      从 char[]b 中的 index off 开始。追加总是在 ByteArrayBuffer 的末尾完成。

      因此我的代码应该如下所示:

      picRaw.append(intent.getExtras().
          getByteArray(BtConstants.btBytePayload), picRaw.length(), 
              intent.getExtras().getByteArray(BtConstants.btBytePayload).length);
      

      【讨论】:

        猜你喜欢
        • 2014-05-10
        • 1970-01-01
        • 2014-04-03
        • 2013-08-01
        • 1970-01-01
        • 2013-06-23
        • 1970-01-01
        • 2021-09-02
        • 1970-01-01
        相关资源
        最近更新 更多