@@ -320,15 +320,17 @@ protected int writeCharacters(final String str, final boolean checkNullTerminati
320
320
byte [] dst = curBuffer .array ();
321
321
int arrayOffset = curBuffer .arrayOffset ();
322
322
if (remaining >= str .length () + 1 ) {
323
- // Write ASCII characters directly to the array until we hit a non-ASCII character
323
+ // Write ASCII characters directly to the array until we hit a non-ASCII character.
324
324
sp = writeOnArrayAscii (str , dst , arrayOffset + curBufferPos , checkNullTermination );
325
325
curBufferPos += sp ;
326
+ // If the whole string was written as ASCII, append the null terminator.
326
327
if (sp == stringLength ) {
327
328
dst [arrayOffset + curBufferPos ++] = 0 ;
328
329
position += sp + 1 ;
329
330
curBuffer .position (curBufferPos );
330
331
return sp + 1 ;
331
332
}
333
+ // Otherwise, update the position to reflect the partial write.
332
334
position += sp ;
333
335
curBuffer .position (curBufferPos );
334
336
}
@@ -378,6 +380,7 @@ private int writeOnBuffers(final String str,
378
380
position ++;
379
381
} else if (c < 0x800 ) {
380
382
if (remaining < 2 ) {
383
+ // Not enough space: use write() to handle buffer boundary
381
384
write ((byte ) (0xc0 + (c >> 6 )));
382
385
write ((byte ) (0x80 + (c & 0x3f )));
383
386
@@ -391,7 +394,7 @@ private int writeOnBuffers(final String str,
391
394
position += 2 ;
392
395
}
393
396
} else {
394
- // Handle multibyte characters (may involve surrogate pairs)
397
+ // Handle multibyte characters (may involve surrogate pairs).
395
398
c = Character .codePointAt (str , sp );
396
399
/*
397
400
Malformed surrogate pairs are encoded as-is (3 byte code unit) without substituting any code point.
0 commit comments