0

I've got problem with my bytes array. I try to convert it to long , then to hex and show it as a toast. However I get java.nio.BufferUnderflowException. Here's my code :

 public void onReceivedData(byte[] arg0)
        {
            String tag = null;
            long tagValue = ByteBuffer.wrap(arg0).getLong();
            tag = Long.toHexString(tagValue).toUpperCase();

}

Problem is at line :

        long tagValue = ByteBuffer.wrap(arg0).getLong();

I looked for information about this exception but none of the answers was solution for my problem.

This is logcat:

Process: com.example.eltegps011.eltegps, PID: 17304
                                                                                java.nio.BufferUnderflowException
                                                                                    at java.nio.ByteArrayBuffer.getLong(ByteArrayBuffer.java:211)
                                                                                    at com.example.eltegps011.eltegps.fragments.StocktakingFragment$9.onReceivedData(StocktakingFragment.java:504)
                                                                                    at com.felhr.usbserial.UsbSerialDevice$WorkerThread.onReceivedData(UsbSerialDevice.java:261)
                                                                                    at com.felhr.usbserial.UsbSerialDevice$WorkerThread.run(UsbSerialDevice.java:235)
comrade
  • 4,590
  • 5
  • 33
  • 48
Bartos
  • 1,007
  • 3
  • 15
  • 38
  • 1
    Check the size of arg0 and do what you need if the size is wrong. A multile of 8 seems to be the right choice in you case. – daemmie Jul 08 '16 at 07:02
  • Is it possible to set arg0 dynamically ? – Bartos Jul 08 '16 at 07:28
  • I don't know what you exactly whant to do. the byte array is just wapped in the byte buffer. So if you cange the ByteBuffer the byte array will also be affected by the changes. So yes. and arg0 is not const. therefore an other yes. – daemmie Jul 08 '16 at 07:34
  • 1
    I should get one byte array with 9 bytes in it. Instead of this I get one array with one byte and one with 8 bytes. I think, this is the problem. – Bartos Jul 08 '16 at 07:41
  • could you tell me how can I fix it ? – Bartos Jul 08 '16 at 07:43
  • Check out my updated answer. use the function I added to convert your array into a long or make sure args has the correct size. – daemmie Jul 08 '16 at 07:54
  • Do you need to pass an index to getLong? `long tagValue = ByteBuffer.wrap(arg0).getLong(0);` – Dobz Jul 08 '16 at 07:59
  • Dobz, no I don't. I just need to get whole array (one byte from first, and eleven from second). – Bartos Jul 08 '16 at 08:03
  • You should probably open up a new question because of the wrong byte size array issue? – daemmie Jul 08 '16 at 08:08
  • I will do as you suggest :) – Bartos Jul 08 '16 at 08:15

2 Answers2

0

As you seem to have a variable number of bytes:

arg0 = Arrays.copy(arg0, 8);

So you have 8 bytes for a long: xx xx ... 00 00


Or maybe you just need a hex dump of the bytes array:

public String bytesToString(byte[] bytes) {
    StringBuilder sb = new StringBuilder(bytes.length * 3);
    for (int i = 0; i < bytes.length; ++i) {
        sb.append(String.format("%02X ", 0xFF & bytes[i]));
    }
    return sb.toString();
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • I still get two arrays :/ First one have one byte and second eight bytes. – Bartos Jul 08 '16 at 08:04
  • Extended the answer, with a hex dump, added a space after ever 2 hex digits for clarity. – Joop Eggen Jul 08 '16 at 08:18
  • Joop thx, it works , but still : 07-08 10:22:57.181 15705-16302/com.example.eltegps011.eltegps E/StocktakingFragment: onReceivedData: DA 07-08 10:22:57.181 15705-16302/com.example.eltegps011.eltegps E/StocktakingFragment: onReceivedData: AD 06 74 01 6F 6B 26 0B – Bartos Jul 08 '16 at 08:23
  • it should be DA AD 06 74 01 6F 6B 26 – Bartos Jul 08 '16 at 08:24
  • That DA seems to have been gone missing / read already. Good luck debugging. – Joop Eggen Jul 08 '16 at 08:38
-1

Add a check before getting long value from ByteBuffer

ByteBuffer byteBuffer = ByteBuffer.wrap(arg0);
            if(byteBuffer.remaining()>0){
                long tagValue= byteBuffer.getLong();
                tag = Long.toHexString(tagValue).toUpperCase();
            }