@@ -53,57 +53,57 @@ public int decode( byte[] b , BSONCallback callback ){
5353 }
5454 }
5555
56-
5756 public int decode ( InputStream in , BSONCallback callback )
5857 throws IOException {
5958 return _decode ( new BSONInput ( in ) , callback );
6059 }
61-
60+
6261 private int _decode ( BSONInput in , BSONCallback callback )
6362 throws IOException {
6463
6564 if ( _in != null || _callback != null )
6665 throw new IllegalStateException ( "not ready" );
67-
66+
6867 _in = in ;
6968 _callback = callback ;
70-
69+
7170 if ( in .numRead () != 0 )
7271 throw new IllegalArgumentException ( "i'm confused" );
7372
7473 try {
75-
74+
7675 final int len = _in .readInt ();
76+
7777 _in .setMax (len );
7878
7979 _callback .objectStart ();
8080 while ( decodeElement () );
8181 _callback .objectDone ();
82-
82+
8383 if ( _in .numRead () != len )
8484 throw new IllegalArgumentException ( "bad data. lengths don't match read:" + _in .numRead () + " != len:" + len );
85-
85+
8686 return len ;
8787 }
8888 finally {
8989 _in = null ;
9090 _callback = null ;
9191 }
9292 }
93-
93+
9494 int decode ( boolean first )
9595 throws IOException {
9696
9797 final int start = _in .numRead ();
98-
98+
9999 final int len = _in .readInt ();
100100 if ( first )
101101 _in .setMax (len );
102102
103103 _callback .objectStart ();
104104 while ( decodeElement () );
105105 _callback .objectDone ();
106-
106+
107107 final int read = _in .numRead () - start ;
108108
109109 if ( read != len ){
@@ -112,23 +112,24 @@ int decode( boolean first )
112112
113113 return len ;
114114 }
115-
115+
116116 boolean decodeElement ()
117117 throws IOException {
118118
119119 final byte type = _in .read ();
120+
120121 if ( type == EOO )
121122 return false ;
122-
123+
123124 String name = _in .readCStr ();
124-
125+
125126 switch ( type ){
126127 case NULL :
127- _callback .gotNull ( name );
128+ _callback .gotNull ( name );
128129 break ;
129-
130+
130131 case UNDEFINED :
131- _callback .gotUndefined ( name );
132+ _callback .gotUndefined ( name );
132133 break ;
133134
134135 case BOOLEAN :
@@ -138,20 +139,18 @@ boolean decodeElement()
138139 case NUMBER :
139140 _callback .gotDouble ( name , _in .readDouble () );
140141 break ;
141-
142+
142143 case NUMBER_INT :
143144 _callback .gotInt ( name , _in .readInt () );
144145 break ;
145146
146147 case NUMBER_LONG :
147148 _callback .gotLong ( name , _in .readLong () );
148- break ;
149+ break ;
149150
150-
151151 case SYMBOL :
152152 _callback .gotSymbol ( name , _in .readUTF8String () );
153153 break ;
154-
155154
156155 case STRING :
157156 _callback .gotString (name , _in .readUTF8String () );
@@ -161,26 +160,26 @@ boolean decodeElement()
161160 // OID is stored as big endian
162161 _callback .gotObjectId ( name , new ObjectId ( _in .readIntBE () , _in .readIntBE () , _in .readIntBE () ) );
163162 break ;
164-
163+
165164 case REF :
166165 _in .readInt (); // length of ctring that follows
167166 String ns = _in .readCStr ();
168167 ObjectId theOID = new ObjectId ( _in .readInt () , _in .readInt () , _in .readInt () );
169168 _callback .gotDBRef ( name , ns , theOID );
170169 break ;
171-
170+
172171 case DATE :
173172 _callback .gotDate ( name , _in .readLong () );
174173 break ;
175-
174+
176175 case REGEX :
177176 _callback .gotRegex ( name , _in .readCStr () , _in .readCStr () );
178177 break ;
179178
180179 case BINARY :
181180 _binary ( name );
182181 break ;
183-
182+
184183 case CODE :
185184 _callback .gotCode ( name , _in .readUTF8String () );
186185 break ;
@@ -199,17 +198,17 @@ boolean decodeElement()
199198 _callback .arrayDone ();
200199
201200 break ;
202-
203-
201+
202+
204203 case OBJECT :
205204 _in .readInt (); // total size - we don't care....
206-
205+
207206 _callback .objectStart ( name );
208207 while ( decodeElement () );
209208 _callback .objectDone ();
210209
211210 break ;
212-
211+
213212 case TIMESTAMP :
214213 int i = _in .readInt ();
215214 int time = _in .readInt ();
@@ -227,15 +226,15 @@ boolean decodeElement()
227226 default :
228227 throw new UnsupportedOperationException ( "BSONDecoder doesn't understand type : " + type + " name: " + name );
229228 }
230-
229+
231230 return true ;
232231 }
233232
234233 protected void _binary ( String name )
235234 throws IOException {
236235 final int totalLen = _in .readInt ();
237236 final byte bType = _in .read ();
238-
237+
239238 switch ( bType ){
240239 case B_GENERAL : {
241240 final byte [] data = new byte [totalLen ];
@@ -247,31 +246,31 @@ protected void _binary( String name )
247246 final int len = _in .readInt ();
248247 if ( len + 4 != totalLen )
249248 throw new IllegalArgumentException ( "bad data size subtype 2 len: " + len + " totalLen: " + totalLen );
250-
249+
251250 final byte [] data = new byte [len ];
252251 _in .fill ( data );
253252 _callback .gotBinary ( name , bType , data );
254253 return ;
255254 case B_UUID :
256255 if ( totalLen != 16 )
257256 throw new IllegalArgumentException ( "bad data size subtype 3 len: " + totalLen + " != 16" );
258-
257+
259258 long part1 = _in .readLong ();
260259 long part2 = _in .readLong ();
261260 _callback .gotUUID (name , part1 , part2 );
262- return ;
261+ return ;
263262 }
264-
263+
265264 byte [] data = new byte [totalLen ];
266265 _in .fill ( data );
267266
268267 _callback .gotBinary ( name , bType , data );
269268 }
270-
269+
271270 Object _readBasicObject ()
272271 throws IOException {
273272 _in .readInt ();
274-
273+
275274 BSONCallback save = _callback ;
276275 BSONCallback _basic = _callback .createBSONCallback ();
277276 _callback = _basic ;
0 commit comments