@@ -320,15 +320,17 @@ protected int writeCharacters(final String str, final boolean checkNullTerminati
320320 byte [] dst = curBuffer .array ();
321321 int arrayOffset = curBuffer .arrayOffset ();
322322 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.
324324 sp = writeOnArrayAscii (str , dst , arrayOffset + curBufferPos , checkNullTermination );
325325 curBufferPos += sp ;
326+ // If the whole string was written as ASCII, append the null terminator.
326327 if (sp == stringLength ) {
327328 dst [arrayOffset + curBufferPos ++] = 0 ;
328329 position += sp + 1 ;
329330 curBuffer .position (curBufferPos );
330331 return sp + 1 ;
331332 }
333+ // Otherwise, update the position to reflect the partial write.
332334 position += sp ;
333335 curBuffer .position (curBufferPos );
334336 }
@@ -378,6 +380,7 @@ private int writeOnBuffers(final String str,
378380 position ++;
379381 } else if (c < 0x800 ) {
380382 if (remaining < 2 ) {
383+ // Not enough space: use write() to handle buffer boundary
381384 write ((byte ) (0xc0 + (c >> 6 )));
382385 write ((byte ) (0x80 + (c & 0x3f )));
383386
@@ -391,7 +394,7 @@ private int writeOnBuffers(final String str,
391394 position += 2 ;
392395 }
393396 } else {
394- // Handle multibyte characters (may involve surrogate pairs)
397+ // Handle multibyte characters (may involve surrogate pairs).
395398 c = Character .codePointAt (str , sp );
396399 /*
397400 Malformed surrogate pairs are encoded as-is (3 byte code unit) without substituting any code point.
0 commit comments