private void _writeString ( char [ ] text , int offset , int len ) throws IOException , JsonGenerationException { /* Let's just find longest spans of non-escapable * content, and for each see if it makes sense * to copy them, or write through */ len += offset ; // -> len marks the end from now on while ( offset < len ) { int start = offset ; while ( true ) { char c = text [ offset ] ; if ( c == ' ' || c == ' ' ) { break ; } if ( ++ offset >= len ) { break ; } } // Short span? Better just copy it to buffer first: int newAmount = offset - start ; if ( newAmount < SHORT_WRITE ) { // Note: let's reserve room for escaped char (up to 6 chars) if ( ( _outputTail + newAmount ) > _outputEnd ) { _flushBuffer ( ) ; } if ( newAmount > 0 ) { System . arraycopy ( text , start , _outputBuffer , _outputTail , newAmount ) ; _outputTail += newAmount ; } } else { // Nope: better just write through _flushBuffer ( ) ; _writer . write ( text , start , newAmount ) ; } // Was this the end? if ( offset >= len ) { // yup break ; } // Nope, need to escape the char. char c = text [ offset ++ ] ; _appendCharacterEscape ( ' ' , c ) ; } }
This method called when the string content is already in a char buffer and need not be copied for processing .