ByteArrayBuffer抛出IndexOutOfBounds异常 - 无法得到原因

时间:2012-10-25 08:35:15

标签: android bytearray byte indexoutofboundsexception

我通过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 - 这应该足够了。

2 个答案:

答案 0 :(得分:0)

  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();
            }

我希望上面的代码可以帮到你。

答案 1 :(得分:0)

我想我找到了解决方案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);