Skip to content

Commit 8766580

Browse files
JAVA-329: default binary subtype to 0 (from 2)
1 parent e20b5b1 commit 8766580

File tree

5 files changed

+30
-33
lines changed

5 files changed

+30
-33
lines changed

src/main/org/bson/BSONCallback.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
package org.bson;
44

5-
import org.bson.types.*;
5+
import org.bson.types.ObjectId;
66

77
public interface BSONCallback {
88

@@ -39,7 +39,7 @@ public interface BSONCallback {
3939
void gotDBRef( String name , String ns , ObjectId id );
4040

4141
/**
42-
* subtype 2
42+
* subtype 2/0
4343
*/
4444
void gotBinaryArray( String name , byte[] b );
4545
void gotBinary( String name , byte type , byte[] data );

src/main/org/bson/BSONDecoder.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
import java.io.*;
88

9-
import org.bson.io.*;
10-
import org.bson.types.*;
9+
import org.bson.io.Bits;
10+
import org.bson.io.PoolOutputBuffer;
11+
import org.bson.types.ObjectId;
1112

1213
public class BSONDecoder {
1314

@@ -220,13 +221,8 @@ protected void _binary( String name )
220221
final byte bType = _in.read();
221222

222223
switch ( bType ){
223-
case B_GENERAL: {
224-
final byte[] data = new byte[totalLen];
225-
_in.fill( data );
226-
_callback.gotBinaryArray( name , data );
227-
return;
228-
}
229-
case B_BINARY: {
224+
case B_GENERAL:
225+
case B_BINARY:
230226
final int len = _in.readInt();
231227
if ( len + 4 != totalLen )
232228
throw new IllegalArgumentException( "bad data size subtype 2 len: " + len + " totalLen: " + totalLen );
@@ -235,7 +231,6 @@ protected void _binary( String name )
235231
_in.fill( data );
236232
_callback.gotBinaryArray( name , data );
237233
return;
238-
}
239234
case B_UUID:
240235
if ( totalLen != 16 )
241236
throw new IllegalArgumentException( "bad data size subtype 3 len: " + totalLen + " != 16");

src/main/org/bson/BSONEncoder.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@
22

33
package org.bson;
44

5-
import com.mongodb.DBRef;
6-
import com.mongodb.DBRefBase;
75
import static org.bson.BSON.*;
86

9-
import java.lang.reflect.*;
10-
import java.nio.*;
11-
import java.nio.charset.*;
7+
import java.lang.reflect.Array;
8+
import java.nio.Buffer;
129
import java.util.*;
1310
import java.util.Map.Entry;
14-
import java.util.concurrent.atomic.*;
15-
import java.util.regex.*;
11+
import java.util.concurrent.atomic.AtomicInteger;
12+
import java.util.concurrent.atomic.AtomicLong;
13+
import java.util.regex.Pattern;
1614

17-
import org.bson.io.*;
15+
import org.bson.io.BasicOutputBuffer;
16+
import org.bson.io.OutputBuffer;
1817
import org.bson.types.*;
1918

19+
import com.mongodb.DBRefBase;
20+
2021
/**
2122
* this is meant to be pooled or cached
2223
* there is some per instance memory for string conversion, etc...
@@ -321,7 +322,7 @@ protected void putBinary( String name , byte[] data ){
321322
_put( BINARY , name );
322323
_buf.writeInt( 4 + data.length );
323324

324-
_buf.write( B_BINARY );
325+
_buf.write( B_GENERAL );
325326
_buf.writeInt( data.length );
326327
int before = _buf.getPosition();
327328
_buf.write( data );

src/test/com/mongodb/ByteTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@
1818

1919
import java.io.*;
2020
import java.util.*;
21-
import java.util.regex.*;
22-
import java.io.IOException;
21+
import java.util.regex.Pattern;
2322

23+
import org.bson.*;
24+
import org.bson.types.ObjectId;
2425
import org.testng.annotations.Test;
2526

26-
import com.mongodb.util.*;
27-
28-
import org.bson.*;
29-
import org.bson.types.*;
27+
import com.mongodb.util.TestCase;
3028

3129

3230
@SuppressWarnings("unchecked")
@@ -182,6 +180,8 @@ public void testBinary() {
182180

183181
BSONObject read = BSON.decode( encoded );
184182
byte[] b = (byte[])read.get( "bytes" );
183+
184+
assertEquals(barray.length, b.length);
185185
for( int i=0; i<256; i++ ) {
186186
assertEquals( b[i], barray[i] );
187187
}

src/test/org/bson/BSONTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@
1818

1919
package org.bson;
2020

21-
import java.io.*;
21+
import java.io.ByteArrayInputStream;
22+
import java.io.IOException;
2223
import java.util.*;
2324

24-
import org.bson.io.*;
25-
import org.bson.types.*;
26-
import org.testng.annotations.*;
27-
25+
import org.bson.io.BasicOutputBuffer;
26+
import org.bson.io.OutputBuffer;
27+
import org.bson.types.CodeWScope;
2828
import org.testng.Assert;
29+
import org.testng.annotations.Test;
2930

3031
public class BSONTest extends Assert {
3132

@@ -128,7 +129,7 @@ public void testBinary()
128129
data[i] = 1;
129130
}
130131
BSONObject binary_object = new BasicBSONObject( "bin" , data);
131-
_test( binary_object , 10019 , "682d9a636619b135fa9801ac42c48a10" );
132+
_test( binary_object , 10019 , "71c3247d9e9a26d73bf64efc0b9fe90f" );
132133
}
133134

134135
@Test

0 commit comments

Comments
 (0)