Skip to content

Commit 3545df6

Browse files
JAVA-329: corrected (broken) implementation
1 parent 0b2812b commit 3545df6

File tree

7 files changed

+71
-22
lines changed

7 files changed

+71
-22
lines changed

src/main/org/bson/BSONCallback.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ public interface BSONCallback {
3939
void gotDBRef( String name , String ns , ObjectId id );
4040

4141
/**
42-
* subtype 2/0
42+
*
4343
*/
44-
void gotBinaryArray( String name , byte[] b );
44+
@Deprecated
45+
void gotBinaryArray( String name , byte[] data );
4546
void gotBinary( String name , byte type , byte[] data );
4647
/**
4748
* subtype 3

src/main/org/bson/BSONDecoder.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,20 @@ protected void _binary( String name )
221221
final byte bType = _in.read();
222222

223223
switch ( bType ){
224-
case B_GENERAL:
224+
case B_GENERAL: {
225+
final byte[] data = new byte[totalLen];
226+
_in.fill( data );
227+
_callback.gotBinary( name, bType, data );
228+
return;
229+
}
225230
case B_BINARY:
226231
final int len = _in.readInt();
227232
if ( len + 4 != totalLen )
228233
throw new IllegalArgumentException( "bad data size subtype 2 len: " + len + " totalLen: " + totalLen );
229234

230235
final byte[] data = new byte[len];
231236
_in.fill( data );
232-
_callback.gotBinaryArray( name , data );
237+
_callback.gotBinary( name , bType , data );
233238
return;
234239
case B_UUID:
235240
if ( totalLen != 16 )

src/main/org/bson/BSONEncoder.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -319,24 +319,29 @@ else if ( n instanceof Float || n instanceof Double ) {
319319
}
320320

321321
protected void putBinary( String name , byte[] data ){
322+
putBinary( name, B_GENERAL, data );
323+
}
324+
325+
protected void putBinary( String name , Binary val ){
326+
putBinary( name, val.getType(), val.getData() );
327+
}
328+
329+
private void putBinary( String name , int type , byte[] data ){
322330
_put( BINARY , name );
323-
_buf.writeInt( 4 + data.length );
324-
325-
_buf.write( B_GENERAL );
326-
_buf.writeInt( data.length );
331+
int totalLen = data.length;
332+
333+
if (type == B_BINARY)
334+
totalLen += 4;
335+
336+
_buf.writeInt( totalLen );
337+
_buf.write( type );
338+
if (type == B_BINARY)
339+
_buf.writeInt( totalLen -4 );
327340
int before = _buf.getPosition();
328341
_buf.write( data );
329342
int after = _buf.getPosition();
330-
331343
com.mongodb.util.MyAsserts.assertEquals( after - before , data.length );
332344
}
333-
334-
protected void putBinary( String name , Binary val ){
335-
_put( BINARY , name );
336-
_buf.writeInt( val.length() );
337-
_buf.write( val.getType() );
338-
_buf.write( val.getData() );
339-
}
340345

341346
protected void putUUID( String name , UUID val ){
342347
_put( BINARY , name );

src/main/org/bson/BasicBSONCallback.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,16 @@ public void gotDBRef( String name , String ns , ObjectId id ){
130130
_put( name , new BasicBSONObject( "$ns" , ns ).append( "$id" , id ) );
131131
}
132132

133-
public void gotBinaryArray( String name , byte[] b ){
134-
_put( name , b );
133+
@Deprecated
134+
public void gotBinaryArray( String name , byte[] data ){
135+
gotBinary( name, BSON.B_GENERAL, data );
135136
}
136137

137138
public void gotBinary( String name , byte type , byte[] data ){
138-
_put( name , new Binary( type , data ) );
139+
if( type == BSON.B_GENERAL )
140+
_put( name , data );
141+
else
142+
_put( name , new Binary( type , data ) );
139143
}
140144

141145
public void gotUUID( String name , long part1, long part2){

src/test/com/mongodb/ByteTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public void testBinary() {
176176
o.put( "bytes", barray );
177177

178178
byte[] encoded = BSON.encode( o );
179-
assertEquals( 277 , encoded.length );
179+
assertEquals( 273 , encoded.length );
180180

181181
BSONObject read = BSON.decode( encoded );
182182
byte[] b = (byte[])read.get( "bytes" );

src/test/com/mongodb/JavaClientTest.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.*;
2424
import java.util.regex.Pattern;
2525

26+
import org.bson.BSON;
2627
import org.bson.Transformer;
2728
import org.bson.types.*;
2829
import org.testng.annotations.Test;
@@ -184,7 +185,7 @@ public void testBinary()
184185
bb.order( Bytes.ORDER );
185186
bb.putInt( 5 );
186187
bb.put( "eliot".getBytes() );
187-
out.put( "a" , new Binary( (byte)2 , raw ) );
188+
out.put( "a" , "eliot".getBytes() );
188189
c.save( out );
189190

190191
out = c.findOne();
@@ -198,6 +199,39 @@ public void testBinary()
198199
assertEquals( Util.toHex( raw ) , Util.toHex( blah.getData() ) );
199200
}
200201

202+
}
203+
204+
@Test
205+
public void testBinaryOld()
206+
throws MongoException {
207+
DBCollection c = _db.getCollection( "testBinary" );
208+
c.drop();
209+
c.save( BasicDBObjectBuilder.start().add( "a" , "eliot".getBytes() ).get() );
210+
211+
DBObject out = c.findOne();
212+
byte[] b = (byte[])(out.get( "a" ) );
213+
assertEquals( "eliot" , new String( b ) );
214+
215+
{
216+
byte[] raw = new byte[9];
217+
ByteBuffer bb = ByteBuffer.wrap( raw );
218+
bb.order( Bytes.ORDER );
219+
bb.putInt( 5 );
220+
bb.put( "eliot".getBytes() );
221+
out.put( "a" , new Binary( BSON.B_BINARY , "eliot".getBytes() ) );
222+
c.save( out );
223+
224+
out = c.findOne();
225+
Binary blah = (Binary)(out.get( "a" ) );
226+
assertEquals( "eliot" , new String( blah.getData() ) );
227+
228+
out.put( "a" , new Binary( (byte)111 , raw ) );
229+
c.save( out );
230+
blah = (Binary)c.findOne().get( "a" );
231+
assertEquals( 111 , blah.getType() );
232+
assertEquals( Util.toHex( raw ) , Util.toHex( blah.getData() ) );
233+
}
234+
201235
}
202236
@Test
203237
public void testUUID()

src/test/org/bson/BSONTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public void testBinary()
129129
data[i] = 1;
130130
}
131131
BSONObject binary_object = new BasicBSONObject( "bin" , data);
132-
_test( binary_object , 10019 , "71c3247d9e9a26d73bf64efc0b9fe90f" );
132+
_test( binary_object , 10015 , "1d439ba5b959ecfe297a7862bf95bc10" );
133133
}
134134

135135
@Test

0 commit comments

Comments
 (0)