Skip to content

Commit 306e36c

Browse files
committed
removed some UTF-8 conversions by checking values JAVA-194
1 parent 88b3240 commit 306e36c

1 file changed

Lines changed: 21 additions & 8 deletions

File tree

src/main/org/bson/BSONDecoder.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,15 @@ void fill( byte b[] , int len )
295295
}
296296
}
297297

298+
boolean _isAscii( byte b ){
299+
return b >=0 && b <= 127;
300+
}
301+
298302
String readCStr()
299303
throws IOException {
300304

305+
boolean isAcii = true;
306+
301307
// short circuit 1 byte strings
302308
{
303309
_random[0] = read();
@@ -315,23 +321,30 @@ String readCStr()
315321
_stringBuffer.reset();
316322
_stringBuffer.write( _random[0] );
317323
_stringBuffer.write( _random[1] );
318-
324+
325+
isAcii = _isAscii( _random[0] ) && _isAscii( _random[1] );
319326
}
320-
321-
327+
328+
322329
while ( true ){
323330
byte b = read();
324331
if ( b == 0 )
325332
break;
326333
_stringBuffer.write( b );
334+
isAcii = isAcii && _isAscii( b );
327335
}
328-
336+
329337
String out = null;
330-
try {
331-
out = _stringBuffer.asString( "UTF-8" );
338+
if ( isAcii ){
339+
out = _stringBuffer.asString();
332340
}
333-
catch ( UnsupportedOperationException e ){
334-
throw new RuntimeException( "impossible" , e );
341+
else {
342+
try {
343+
out = _stringBuffer.asString( "UTF-8" );
344+
}
345+
catch ( UnsupportedOperationException e ){
346+
throw new RuntimeException( "impossible" , e );
347+
}
335348
}
336349
_stringBuffer.reset();
337350
return out;

0 commit comments

Comments
 (0)