reapzor / FiloFirmata

MIT License
7 stars 2 forks source link

Two-Seven-Bit encoding #1

Closed MatzeS closed 8 years ago

MatzeS commented 8 years ago

I'm looking on Firmata now for roughtly 4 hours and maybe I'm comleptly wrong posting this issue, but:

I tried to setup some custom messages and used there for the DataTypeHelpers.encodeTwoSevenBitByteSequence(String) method. Using this method produces following exception:

Exception in thread "main" java.lang.IndexOutOfBoundsException
at java.nio.Buffer.checkIndex(Buffer.java:540)
at java.nio.HeapByteBuffer.put(HeapByteBuffer.java:178)
at com.bortbort.helpers.DataTypeHelpers.encodeTwoSevenBitByteSequence(DataTypeHelpers.java:68)
at com.bortbort.helpers.DataTypeHelpers.encodeTwoSevenBitByteSequence(DataTypeHelpers.java:60)
at com.bortbort.helpers.DataTypeHelpers.encodeTwoSevenBitByteSequence(DataTypeHelpers.java:56)    

Tracking down this stack trace the problem is located at DataTypeHelpers line 68: byteBuffer.put((byte) (bytes[offset] & 0x7F), (byte) ((bytes[offset] >> 7) & 0x7F));

I guess idea of this code is to put two bytes to the buffer, assuming the method is like: ByteBuffer.put(byte... bytes). This is not the case! (API: https://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html#put(byte) )

So, localy I fixed it into two lines: byteBuffer.put((byte) (bytes[offset] & 0x7F)); byteBuffer.put((byte) ((bytes[offset] >> 7) & 0x7F));

working out perfectly :)

(first issue report in my life, don't hate me) Matthias

reapzor commented 8 years ago

Sorry for the delay! I have no idea how this was even working for me! The tests must be flawed :) (I know they are) I'll try to get that fix pushed tonight with a couple more tweaks to make this functionality actually do what its supposed to :)