RUBY-272 ensure that JRuby deserializes binary type 2 properly.

This commit is contained in:
Kyle Banker 2011-05-11 10:59:21 -04:00
parent e0c22e26ae
commit 93d7e1512a
6 changed files with 31 additions and 14 deletions

Binary file not shown.

Binary file not shown.

View File

@ -14,14 +14,24 @@ import org.bson.types.*;
public class RubyBSONDecoder extends BSONDecoder {
// public int decode( RubyString s , BSONCallback callback ){
// byte[] b = s.getBytes();
// try {
// return decode( new Input( new ByteArrayInputStream(b) ) , callback );
// }
// catch ( IOException ioe ){
// throw new RuntimeException( "should be impossible" , ioe );
// }
// }
protected void _binary( String name )
throws IOException {
final int totalLen = _in.readInt();
final byte bType = _in.read();
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 );
}
}
}

View File

@ -558,7 +558,7 @@ public class RubyBSONEncoder extends BSONEncoder {
long subtype = rbSubtype.longValue();
byte[] data = ra2ba( rarray );
if ( subtype == 2 ) {
putBinary( name, data );
putBinaryTypeTwo( name, data );
}
else {
_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 );
_buf.writeInt( 4 + data.length );
_buf.write( B_BINARY );
_buf.write( 2 );
_buf.writeInt( data.length );
int before = _buf.getPosition();
_buf.write( data );
@ -581,6 +582,13 @@ public class RubyBSONEncoder extends BSONEncoder {
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 ){
_put( BINARY , name );
_buf.writeInt( val.length() );

View File

@ -57,7 +57,6 @@ end
if RUBY_PLATFORM =~ /java/
jar_dir = File.join(File.dirname(__FILE__), '..', 'ext', 'java', '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 'bson/bson_java'
module BSON

View File

@ -11,7 +11,7 @@ module BSON
end
def self.deserialize(buf)
dec = Java::OrgBson::BSONDecoder.new
dec = Java::OrgJbson::RubyBSONDecoder.new
callback = Java::OrgJbson::RubyBSONCallback.new(JRuby.runtime)
dec.decode(buf.to_s.to_java_bytes, callback)
callback.get