-
Type: Bug
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: None
-
Component/s: BSON
-
Minor Change
In MRI the following works:
irb(main):001:0> Hash.from_bson({}.to_bson) => {}
This is because read and write positions in the buffer are tracked separately, and when from_bson is called the write position is at the end of the buffer but the read position is at the beginning.
In JRuby the same operation fails:
irb(main):001:0> Hash.from_bson({}.to_bson) Traceback (most recent call last): 16: from org.jruby.ir.interpreter.StartupInterpreterEngine.interpret(StartupInterpreterEngine.java:72) 15: from org.jruby.ir.interpreter.InterpreterEngine.processCall(InterpreterEngine.java:317) 14: from org.jruby.runtime.callsite.CachingCallSite.call(CachingCallSite.java:170) 13: from org.jruby.runtime.callsite.CachingCallSite.cacheAndCall(CachingCallSite.java:339) 12: from org.jruby.internal.runtime.methods.DynamicMethod.call(DynamicMethod.java:200) 11: from org.jruby.internal.runtime.methods.MixedModeIRMethod.call(MixedModeIRMethod.java:158) 10: from org.jruby.internal.runtime.methods.MixedModeIRMethod.INTERPRET_METHOD(MixedModeIRMethod.java:171) 9: from org.jruby.ir.interpreter.InterpreterEngine.interpret(InterpreterEngine.java:86) 8: from org.jruby.ir.interpreter.StartupInterpreterEngine.interpret(StartupInterpreterEngine.java:72) 7: from org.jruby.ir.interpreter.InterpreterEngine.processCall(InterpreterEngine.java:346) 6: from org.jruby.runtime.callsite.CachingCallSite.call(CachingCallSite.java:139) 5: from org.jruby.runtime.callsite.CachingCallSite.cacheAndCall(CachingCallSite.java:318) 4: from org.bson.ByteBuf$INVOKER$i$0$0$getCString.call(ByteBuf$INVOKER$i$0$0$getCString.gen) 3: from org.bson.ByteBuf.getCString(ByteBuf.java:257) 2: from java.nio.HeapByteBuffer.get(HeapByteBuffer.java:135) 1: from java.nio.Buffer.nextGetIndex(Buffer.java:500) Java::JavaNio::BufferUnderflowException ()
I presume this is because both positions are updated together (or only a single position is tracked), and thus when from_bson is called the buffer is already consumed/exhausted.