RUBY-272 ensure that JRuby deserializes binary type 2 properly.
This commit is contained in:
parent
e0c22e26ae
commit
93d7e1512a
Binary file not shown.
Binary file not shown.
|
@ -14,14 +14,24 @@ import org.bson.types.*;
|
||||||
|
|
||||||
public class RubyBSONDecoder extends BSONDecoder {
|
public class RubyBSONDecoder extends BSONDecoder {
|
||||||
|
|
||||||
// public int decode( RubyString s , BSONCallback callback ){
|
protected void _binary( String name )
|
||||||
// byte[] b = s.getBytes();
|
throws IOException {
|
||||||
// try {
|
final int totalLen = _in.readInt();
|
||||||
// return decode( new Input( new ByteArrayInputStream(b) ) , callback );
|
final byte bType = _in.read();
|
||||||
// }
|
|
||||||
// catch ( IOException ioe ){
|
|
||||||
// throw new RuntimeException( "should be impossible" , ioe );
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
if( bType == 2 ) {
|
||||||
|
final int len = _in.readInt();
|
||||||
|
if ( len + 4 != totalLen )
|
||||||
|
throw new IllegalArgumentException( "bad data size subtype 2 len: " + len + "totalLen: " + totalLen );
|
||||||
|
|
||||||
|
final byte[] data = new byte[len];
|
||||||
|
_in.fill( data );
|
||||||
|
_callback.gotBinary( name, (byte)2, data );
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
byte[] data = new byte[totalLen];
|
||||||
|
_in.fill( data );
|
||||||
|
_callback.gotBinary( name, bType, data );
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -558,7 +558,7 @@ public class RubyBSONEncoder extends BSONEncoder {
|
||||||
long subtype = rbSubtype.longValue();
|
long subtype = rbSubtype.longValue();
|
||||||
byte[] data = ra2ba( rarray );
|
byte[] data = ra2ba( rarray );
|
||||||
if ( subtype == 2 ) {
|
if ( subtype == 2 ) {
|
||||||
putBinary( name, data );
|
putBinaryTypeTwo( name, data );
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
_put( BINARY , name );
|
_put( BINARY , name );
|
||||||
|
@ -568,11 +568,12 @@ public class RubyBSONEncoder extends BSONEncoder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void putBinary( String name , byte[] data ){
|
/* We have a special method because type 2 has a different format. */
|
||||||
|
protected void putBinaryTypeTwo( String name , byte[] data ){
|
||||||
_put( BINARY , name );
|
_put( BINARY , name );
|
||||||
_buf.writeInt( 4 + data.length );
|
_buf.writeInt( 4 + data.length );
|
||||||
|
|
||||||
_buf.write( B_BINARY );
|
_buf.write( 2 );
|
||||||
_buf.writeInt( data.length );
|
_buf.writeInt( data.length );
|
||||||
int before = _buf.getPosition();
|
int before = _buf.getPosition();
|
||||||
_buf.write( data );
|
_buf.write( data );
|
||||||
|
@ -581,6 +582,13 @@ public class RubyBSONEncoder extends BSONEncoder {
|
||||||
com.mongodb.util.MyAsserts.assertEquals( after - before , data.length );
|
com.mongodb.util.MyAsserts.assertEquals( after - before , data.length );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void putBinary( String name , byte[] data ){
|
||||||
|
_put( BINARY , name );
|
||||||
|
_buf.writeInt( data.length );
|
||||||
|
_buf.write( 0 );
|
||||||
|
_buf.write( data );
|
||||||
|
}
|
||||||
|
|
||||||
protected void putBinary( String name , Binary val ){
|
protected void putBinary( String name , Binary val ){
|
||||||
_put( BINARY , name );
|
_put( BINARY , name );
|
||||||
_buf.writeInt( val.length() );
|
_buf.writeInt( val.length() );
|
||||||
|
|
|
@ -57,7 +57,6 @@ end
|
||||||
if RUBY_PLATFORM =~ /java/
|
if RUBY_PLATFORM =~ /java/
|
||||||
jar_dir = File.join(File.dirname(__FILE__), '..', 'ext', 'java', 'jar')
|
jar_dir = File.join(File.dirname(__FILE__), '..', 'ext', 'java', 'jar')
|
||||||
require File.join(jar_dir, 'mongo-2.4.jar')
|
require File.join(jar_dir, 'mongo-2.4.jar')
|
||||||
require File.join(jar_dir, 'bson-2.2.jar')
|
|
||||||
require File.join(jar_dir, 'jbson.jar')
|
require File.join(jar_dir, 'jbson.jar')
|
||||||
require 'bson/bson_java'
|
require 'bson/bson_java'
|
||||||
module BSON
|
module BSON
|
||||||
|
|
|
@ -11,7 +11,7 @@ module BSON
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.deserialize(buf)
|
def self.deserialize(buf)
|
||||||
dec = Java::OrgBson::BSONDecoder.new
|
dec = Java::OrgJbson::RubyBSONDecoder.new
|
||||||
callback = Java::OrgJbson::RubyBSONCallback.new(JRuby.runtime)
|
callback = Java::OrgJbson::RubyBSONCallback.new(JRuby.runtime)
|
||||||
dec.decode(buf.to_s.to_java_bytes, callback)
|
dec.decode(buf.to_s.to_java_bytes, callback)
|
||||||
callback.get
|
callback.get
|
||||||
|
|
Loading…
Reference in New Issue