Skip to content

Commit b4ec334

Browse files
author
Ryan
committed
added constants and moved isAscii method to static so it can be inlined by jit.
1 parent 1e32401 commit b4ec334

1 file changed

Lines changed: 28 additions & 26 deletions

File tree

src/main/org/bson/BasicBSONDecoder.java

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ boolean decodeElement()
229229
return true;
230230
}
231231

232-
protected void _binary( String name )
232+
protected void _binary( final String name )
233233
throws IOException {
234234
final int totalLen = _in.readInt();
235235
final byte bType = _in.read();
@@ -246,21 +246,21 @@ protected void _binary( String name )
246246
if ( len + 4 != totalLen )
247247
throw new IllegalArgumentException( "bad data size subtype 2 len: " + len + " totalLen: " + totalLen );
248248

249-
final byte[] data = new byte[len];
249+
final byte [] data = new byte[len];
250250
_in.fill( data );
251251
_callback.gotBinary( name , bType , data );
252252
return;
253253
case B_UUID:
254254
if ( totalLen != 16 )
255255
throw new IllegalArgumentException( "bad data size subtype 3 len: " + totalLen + " != 16");
256256

257-
long part1 = _in.readLong();
258-
long part2 = _in.readLong();
257+
final long part1 = _in.readLong();
258+
final long part2 = _in.readLong();
259259
_callback.gotUUID(name, part1, part2);
260260
return;
261261
}
262262

263-
byte[] data = new byte[totalLen];
263+
final byte [] data = new byte[totalLen];
264264
_in.fill( data );
265265

266266
_callback.gotBinary( name , bType , data );
@@ -270,8 +270,8 @@ Object _readBasicObject()
270270
throws IOException {
271271
_in.readInt();
272272

273-
BSONCallback save = _callback;
274-
BSONCallback _basic = _callback.createBSONCallback();
273+
final BSONCallback save = _callback;
274+
final BSONCallback _basic = _callback.createBSONCallback();
275275
_callback = _basic;
276276
_basic.reset();
277277
_basic.objectStart(false);
@@ -281,11 +281,9 @@ Object _readBasicObject()
281281
return _basic.get();
282282
}
283283

284-
285-
286284
protected class BSONInput {
287285

288-
public BSONInput(InputStream in){
286+
public BSONInput(final InputStream in){
289287
_raw = in;
290288
_read = 0;
291289

@@ -374,8 +372,8 @@ public void fill( byte b[] )
374372
public void fill( byte b[] , int len )
375373
throws IOException {
376374
// first use what we have
377-
int have = _len - _pos;
378-
int tocopy = Math.min( len , have );
375+
final int have = _len - _pos;
376+
final int tocopy = Math.min( len , have );
379377
System.arraycopy( _inputBuffer , _pos , b , 0 , tocopy );
380378

381379
_pos += tocopy;
@@ -385,7 +383,7 @@ public void fill( byte b[] , int len )
385383

386384
int off = tocopy;
387385
while ( len > 0 ){
388-
int x = _raw.read( b , off , len );
386+
final int x = _raw.read( b , off , len );
389387
if (x <= 0)
390388
throw new IOException( "unexpected EOF" );
391389
_read += x;
@@ -394,12 +392,7 @@ public void fill( byte b[] , int len )
394392
}
395393
}
396394

397-
protected boolean _isAscii( byte b ){
398-
return b >=0 && b <= 127;
399-
}
400-
401-
public String readCStr()
402-
throws IOException {
395+
public String readCStr() throws IOException {
403396

404397
boolean isAscii = true;
405398

@@ -415,7 +408,7 @@ public String readCStr()
415408
if (out != null) {
416409
return out;
417410
}
418-
return new String(_random, 0, 1, "UTF-8");
411+
return new String(_random, 0, 1, DEFAULT_ENCODING);
419412
}
420413

421414
_stringBuffer.reset();
@@ -438,7 +431,7 @@ public String readCStr()
438431
}
439432
else {
440433
try {
441-
out = _stringBuffer.asString( "UTF-8" );
434+
out = _stringBuffer.asString( DEFAULT_ENCODING );
442435
}
443436
catch ( UnsupportedOperationException e ){
444437
throw new BSONException( "impossible" , e );
@@ -450,9 +443,9 @@ public String readCStr()
450443

451444
public String readUTF8String()
452445
throws IOException {
453-
int size = readInt();
446+
final int size = readInt();
454447
// this is just protection in case it's corrupted, to avoid huge strings
455-
if ( size <= 0 || size > ( 32 * 1024 * 1024 ) )
448+
if ( size <= 0 || size > MAX_STRING )
456449
throw new BSONException( "bad string size: " + size );
457450

458451
if ( size < _inputBuffer.length / 2 ){
@@ -461,15 +454,15 @@ public String readUTF8String()
461454
return "";
462455
}
463456

464-
return new String( _inputBuffer , _need(size) , size - 1 , "UTF-8" );
457+
return new String( _inputBuffer , _need(size) , size - 1 , DEFAULT_ENCODING );
465458
}
466459

467-
byte[] b = size < _random.length ? _random : new byte[size];
460+
final byte [] b = size < _random.length ? _random : new byte[size];
468461

469462
fill( b , size );
470463

471464
try {
472-
return new String( b , 0 , size - 1 , "UTF-8" );
465+
return new String( b , 0 , size - 1 , DEFAULT_ENCODING );
473466
}
474467
catch ( java.io.UnsupportedEncodingException uee ){
475468
throw new BSONException( "impossible" , uee );
@@ -498,16 +491,25 @@ public void setMax(int _max) {
498491
int _max = 4; // max number of total bytes allowed to ready
499492

500493
}
494+
501495
protected BSONInput _in;
502496
protected BSONCallback _callback;
497+
503498
private byte[] _random = new byte[1024]; // has to be used within a single function
504499
private byte[] _inputBuffer = new byte[1024];
500+
505501
private PoolOutputBuffer _stringBuffer = new PoolOutputBuffer();
506502

507503
protected int _pos; // current offset into _inputBuffer
508504
protected int _len; // length of valid data in _inputBuffer
509505

506+
private static final int MAX_STRING = ( 32 * 1024 * 1024 );
510507

508+
private static final String DEFAULT_ENCODING = "UTF-8";
509+
510+
private static final boolean _isAscii( final byte b ){
511+
return b >=0 && b <= 127;
512+
}
511513

512514
static final String[] ONE_BYTE_STRINGS = new String[128];
513515
static void _fillRange( byte min, byte max ){

0 commit comments

Comments
 (0)