The way JRuby currently reads and writes from the Java ByteBuffer is by using a flipping method, wherein on any read or write, the ByteBuffer is flipped.
The problem with this is when reading and writing multiple times from the buffer, the correct values are not returned. Take the following example:
buffer = BSON::ByteBuffer.new
buffer.put_int32(3)
buffer.put_int32(2)
puts buffer.get_int32
buffer.put_int32(1)
puts buffer.get_int32
puts buffer.get_int32
Results on master branch:
MRI:
3
2
1
JRUBY:
3
3
3
Here there are three numbers added to the buffer, and while MRI reads them out in order, JRuby only returns the first number.
My proposed solution is to no longer use the flipping method for reading and writing from the buffer, rather to use the read/write position pointers that are already there to index into the buffer and get/write the value from/into the correct location.