Skip to content

Commit f8727b8

Browse files
committed
more cleaning prep
1 parent 8210323 commit f8727b8

5 files changed

Lines changed: 50 additions & 34 deletions

File tree

src/main/com/mongodb/Bytes.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,20 +125,5 @@ public static byte getType( Object o ){
125125
return 0;
126126
}
127127

128-
public static byte[] encode( DBObject o ){
129-
ByteEncoder e = ByteEncoder.get();
130-
e.putObject( o );
131-
byte b[] = e.getBytes();
132-
e.done();
133-
return b;
134-
}
135-
136-
public static DBObject decode( byte[] b ){
137-
ByteBuffer bb = ByteBuffer.wrap( b );
138-
bb.order( Bytes.ORDER );
139-
ByteDecoder d = new ByteDecoder( bb );
140-
return d.readObject();
141-
}
142-
143128
static final ObjectId COLLECTION_REF_ID = new ObjectId( -1 , -1 , -1 );
144129
}

src/main/com/mongodb/DBMessage.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,6 @@ enum State { BUILDING , SENDING , READABLE , DONE }
4242

4343
static AtomicInteger ID = new AtomicInteger(1);
4444
static int HEADER_LENGTH = 16;
45-
46-
DBMessage( int operation ){
47-
_len = 0;
48-
_id = ID.getAndIncrement();
49-
_responseTo = 0;
50-
_operation = operation;
51-
52-
_encoder = ByteEncoder.get();
53-
_buf = _encoder._buf;
54-
55-
_buf.putInt( 0 ); // saving space for len
56-
_buf.putInt( _id );
57-
_buf.putInt( _responseTo );
58-
_buf.putInt( _operation );
59-
60-
_state = State.BUILDING;
61-
}
6245

6346
DBMessage( ByteBuffer buf ){
6447
_buf = buf;
@@ -69,7 +52,6 @@ enum State { BUILDING , SENDING , READABLE , DONE }
6952
_operation = buf.getInt();
7053

7154
_state = State.READABLE;
72-
_encoder = null;
7355
}
7456

7557
ByteBuffer forBuilding(){
@@ -121,6 +103,5 @@ public String toString(){
121103
final int _responseTo;
122104
final int _operation;
123105

124-
final ByteEncoder _encoder;
125106
final ByteBuffer _buf;
126107
}

src/main/org/bson/BSON.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import java.util.regex.*;
77
import java.nio.charset.*;
88

9+
import org.bson.io.*;
10+
911
public class BSON {
1012

1113
// ---- basics ----
@@ -200,5 +202,29 @@ public static void clearAllHooks(){
200202
Collections.synchronizedMap( new HashMap<Byte,List<Transformer>>() );
201203

202204
static protected Charset _utf8 = Charset.forName( "UTF-8" );
205+
206+
// ----- static encode/decode -----
207+
208+
public static byte[] encode( BSONObject o ){
209+
BSONEncoder e = _staticEncoder.get();
210+
return e.encode( o );
211+
}
212+
213+
public static BSONObject decode( byte[] b ){
214+
BSONDecoder d = _staticDecoder.get();
215+
return d.readObject( b );
216+
}
217+
218+
static ThreadLocal<BSONEncoder> _staticEncoder = new ThreadLocal<BSONEncoder>(){
219+
protected BSONEncoder initialValue(){
220+
return new BSONEncoder();
221+
}
222+
};
223+
224+
static ThreadLocal<BSONDecoder> _staticDecoder = new ThreadLocal<BSONDecoder>(){
225+
protected BSONDecoder initialValue(){
226+
return new BSONDecoder();
227+
}
228+
};
203229

204230
}

src/main/org/bson/BSONDecoder.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@
1010

1111
public class BSONDecoder {
1212

13+
public BSONObject readObject( byte[] b ){
14+
try {
15+
return readObject( new ByteArrayInputStream( b ) );
16+
}
17+
catch ( IOException ioe ){
18+
throw new RuntimeException( "should be impossible" , ioe );
19+
}
20+
}
21+
22+
public BSONObject readObject( InputStream in )
23+
throws IOException {
24+
BasicBSONCallback c = new BasicBSONCallback();
25+
decode( in , c );
26+
return c.get();
27+
}
28+
1329
public int decode( InputStream in , BSONCallback callback )
1430
throws IOException {
1531
return decode( new Input( in ) , callback );

src/main/org/bson/BSONEncoder.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ public BSONEncoder(){
2525

2626
}
2727

28+
public byte[] encode( BSONObject o ){
29+
BasicOutputBuffer buf = new BasicOutputBuffer();
30+
set( buf );
31+
putObject( o );
32+
done();
33+
return buf.toByteArray();
34+
}
35+
2836
public void set( OutputBuffer out ){
2937
if ( _buf != null )
3038
throw new IllegalStateException( "in the middle of something" );

0 commit comments

Comments
 (0)