@@ -53,57 +53,57 @@ public int decode( byte[] b , BSONCallback callback ){
53
53
}
54
54
}
55
55
56
-
57
56
public int decode ( InputStream in , BSONCallback callback )
58
57
throws IOException {
59
58
return _decode ( new BSONInput ( in ) , callback );
60
59
}
61
-
60
+
62
61
private int _decode ( BSONInput in , BSONCallback callback )
63
62
throws IOException {
64
63
65
64
if ( _in != null || _callback != null )
66
65
throw new IllegalStateException ( "not ready" );
67
-
66
+
68
67
_in = in ;
69
68
_callback = callback ;
70
-
69
+
71
70
if ( in .numRead () != 0 )
72
71
throw new IllegalArgumentException ( "i'm confused" );
73
72
74
73
try {
75
-
74
+
76
75
final int len = _in .readInt ();
76
+
77
77
_in .setMax (len );
78
78
79
79
_callback .objectStart ();
80
80
while ( decodeElement () );
81
81
_callback .objectDone ();
82
-
82
+
83
83
if ( _in .numRead () != len )
84
84
throw new IllegalArgumentException ( "bad data. lengths don't match read:" + _in .numRead () + " != len:" + len );
85
-
85
+
86
86
return len ;
87
87
}
88
88
finally {
89
89
_in = null ;
90
90
_callback = null ;
91
91
}
92
92
}
93
-
93
+
94
94
int decode ( boolean first )
95
95
throws IOException {
96
96
97
97
final int start = _in .numRead ();
98
-
98
+
99
99
final int len = _in .readInt ();
100
100
if ( first )
101
101
_in .setMax (len );
102
102
103
103
_callback .objectStart ();
104
104
while ( decodeElement () );
105
105
_callback .objectDone ();
106
-
106
+
107
107
final int read = _in .numRead () - start ;
108
108
109
109
if ( read != len ){
@@ -112,23 +112,24 @@ int decode( boolean first )
112
112
113
113
return len ;
114
114
}
115
-
115
+
116
116
boolean decodeElement ()
117
117
throws IOException {
118
118
119
119
final byte type = _in .read ();
120
+
120
121
if ( type == EOO )
121
122
return false ;
122
-
123
+
123
124
String name = _in .readCStr ();
124
-
125
+
125
126
switch ( type ){
126
127
case NULL :
127
- _callback .gotNull ( name );
128
+ _callback .gotNull ( name );
128
129
break ;
129
-
130
+
130
131
case UNDEFINED :
131
- _callback .gotUndefined ( name );
132
+ _callback .gotUndefined ( name );
132
133
break ;
133
134
134
135
case BOOLEAN :
@@ -138,20 +139,18 @@ boolean decodeElement()
138
139
case NUMBER :
139
140
_callback .gotDouble ( name , _in .readDouble () );
140
141
break ;
141
-
142
+
142
143
case NUMBER_INT :
143
144
_callback .gotInt ( name , _in .readInt () );
144
145
break ;
145
146
146
147
case NUMBER_LONG :
147
148
_callback .gotLong ( name , _in .readLong () );
148
- break ;
149
+ break ;
149
150
150
-
151
151
case SYMBOL :
152
152
_callback .gotSymbol ( name , _in .readUTF8String () );
153
153
break ;
154
-
155
154
156
155
case STRING :
157
156
_callback .gotString (name , _in .readUTF8String () );
@@ -161,26 +160,26 @@ boolean decodeElement()
161
160
// OID is stored as big endian
162
161
_callback .gotObjectId ( name , new ObjectId ( _in .readIntBE () , _in .readIntBE () , _in .readIntBE () ) );
163
162
break ;
164
-
163
+
165
164
case REF :
166
165
_in .readInt (); // length of ctring that follows
167
166
String ns = _in .readCStr ();
168
167
ObjectId theOID = new ObjectId ( _in .readInt () , _in .readInt () , _in .readInt () );
169
168
_callback .gotDBRef ( name , ns , theOID );
170
169
break ;
171
-
170
+
172
171
case DATE :
173
172
_callback .gotDate ( name , _in .readLong () );
174
173
break ;
175
-
174
+
176
175
case REGEX :
177
176
_callback .gotRegex ( name , _in .readCStr () , _in .readCStr () );
178
177
break ;
179
178
180
179
case BINARY :
181
180
_binary ( name );
182
181
break ;
183
-
182
+
184
183
case CODE :
185
184
_callback .gotCode ( name , _in .readUTF8String () );
186
185
break ;
@@ -199,17 +198,17 @@ boolean decodeElement()
199
198
_callback .arrayDone ();
200
199
201
200
break ;
202
-
203
-
201
+
202
+
204
203
case OBJECT :
205
204
_in .readInt (); // total size - we don't care....
206
-
205
+
207
206
_callback .objectStart ( name );
208
207
while ( decodeElement () );
209
208
_callback .objectDone ();
210
209
211
210
break ;
212
-
211
+
213
212
case TIMESTAMP :
214
213
int i = _in .readInt ();
215
214
int time = _in .readInt ();
@@ -227,15 +226,15 @@ boolean decodeElement()
227
226
default :
228
227
throw new UnsupportedOperationException ( "BSONDecoder doesn't understand type : " + type + " name: " + name );
229
228
}
230
-
229
+
231
230
return true ;
232
231
}
233
232
234
233
protected void _binary ( String name )
235
234
throws IOException {
236
235
final int totalLen = _in .readInt ();
237
236
final byte bType = _in .read ();
238
-
237
+
239
238
switch ( bType ){
240
239
case B_GENERAL : {
241
240
final byte [] data = new byte [totalLen ];
@@ -247,31 +246,31 @@ protected void _binary( String name )
247
246
final int len = _in .readInt ();
248
247
if ( len + 4 != totalLen )
249
248
throw new IllegalArgumentException ( "bad data size subtype 2 len: " + len + " totalLen: " + totalLen );
250
-
249
+
251
250
final byte [] data = new byte [len ];
252
251
_in .fill ( data );
253
252
_callback .gotBinary ( name , bType , data );
254
253
return ;
255
254
case B_UUID :
256
255
if ( totalLen != 16 )
257
256
throw new IllegalArgumentException ( "bad data size subtype 3 len: " + totalLen + " != 16" );
258
-
257
+
259
258
long part1 = _in .readLong ();
260
259
long part2 = _in .readLong ();
261
260
_callback .gotUUID (name , part1 , part2 );
262
- return ;
261
+ return ;
263
262
}
264
-
263
+
265
264
byte [] data = new byte [totalLen ];
266
265
_in .fill ( data );
267
266
268
267
_callback .gotBinary ( name , bType , data );
269
268
}
270
-
269
+
271
270
Object _readBasicObject ()
272
271
throws IOException {
273
272
_in .readInt ();
274
-
273
+
275
274
BSONCallback save = _callback ;
276
275
BSONCallback _basic = _callback .createBSONCallback ();
277
276
_callback = _basic ;
0 commit comments