private final int _prependOrWriteCharacterEscape(char[] buffer, int ptr, int end,
            char ch, int escCode)
        throws IOException, JsonGenerationException
    {
        if (escCode >= 0) { // \\N (2 char)
            if (ptr > 1 && ptr < end) { // fits, just prepend
                ptr -= 2;
                buffer[ptr] = '\\';
                buffer[ptr+1] = (char) escCode;
            } else { // won't fit, write
                char[] ent = _entityBuffer;
                if (ent == null) {
                    ent = _allocateEntityBuffer();
                }
                ent[1] = (char) escCode;
                _writer.write(ent, 0, 2);
            }
            return ptr;
        }
        //if (escCode != CharacterEscapes.ESCAPE_CUSTOM) { // std, \\uXXXX
            if (ptr > 5 && ptr < end) { // fits, prepend to buffer
                ptr -= 6;
                buffer[ptr++] = '\\';
                buffer[ptr++] = 'u';
                // We know it's a control char, so only the last 2 chars are non-0
                if (ch > 0xFF) { // beyond 8 bytes
                    int hi = (ch >> 8) & 0xFF;
                    buffer[ptr++] = HEX_CHARS[hi >> 4];
                    buffer[ptr++] = HEX_CHARS[hi & 0xF];
                    ch &= 0xFF;
                } else {
                    buffer[ptr++] = '0';
                    buffer[ptr++] = '0';
                }
                buffer[ptr++] = HEX_CHARS[ch >> 4];
                buffer[ptr] = HEX_CHARS[ch & 0xF];
                ptr -= 5;
            } else {
                // won't fit, flush and write
                char[] ent = _entityBuffer;
                if (ent == null) {
                    ent = _allocateEntityBuffer();
                }
                _outputHead = _outputTail;
                if (ch > 0xFF) { // beyond 8 bytes
                    int hi = (ch >> 8) & 0xFF;
                    int lo = ch & 0xFF;
                    ent[10] = HEX_CHARS[hi >> 4];
                    ent[11] = HEX_CHARS[hi & 0xF];
                    ent[12] = HEX_CHARS[lo >> 4];
                    ent[13] = HEX_CHARS[lo & 0xF];
                    _writer.write(ent, 8, 6);
                } else { // We know it's a control char, so only the last 2 chars are non-0
                    ent[6] = HEX_CHARS[ch >> 4];
                    ent[7] = HEX_CHARS[ch & 0xF];
                    _writer.write(ent, 2, 6);
                }
            }
            return ptr;
        //}
    }
private final int _prependOrWriteCharacterEscape ( char [ ] buffer , int ptr , int end , char ch , int escCode ) throws IOException , JsonGenerationException { if ( escCode >= 0 ) { if ( ptr > 1 && ptr < end ) { ptr -= 2 ; buffer [ ptr ] = '\\' ; buffer [ ptr + 1 ] = ( char ) escCode ; } else { char [ ] ent = _entityBuffer ; if ( ent == null ) { ent = _allocateEntityBuffer ( ) ; } ent [ 1 ] = ( char ) escCode ; _writer . write ( ent , 0 , 2 ) ; } return ptr ; } if ( ptr > 5 && ptr < end ) { ptr -= 6 ; buffer [ ptr ++ ] = '\\' ; buffer [ ptr ++ ] = 'u' ; if ( ch > 0xFF ) { int hi = ( ch > > 8 ) & 0xFF ; buffer [ ptr ++ ] = HEX_CHARS [ hi > > 4 ] ; buffer [ ptr ++ ] = HEX_CHARS [ hi & 0xF ] ; ch &= 0xFF ; } else { buffer [ ptr ++ ] = '0' ; buffer [ ptr ++ ] = '0' ; } buffer [ ptr ++ ] = HEX_CHARS [ ch > > 4 ] ; buffer [ ptr ] = HEX_CHARS [ ch & 0xF ] ; ptr -= 5 ; } else { char [ ] ent = _entityBuffer ; if ( ent == null ) { ent = _allocateEntityBuffer ( ) ; } _outputHead = _outputTail ; if ( ch > 0xFF ) { int hi = ( ch > > 8 ) & 0xFF ; int lo = ch & 0xFF ; ent [ 10 ] = HEX_CHARS [ hi > > 4 ] ; ent [ 11 ] = HEX_CHARS [ hi & 0xF ] ; ent [ 12 ] = HEX_CHARS [ lo > > 4 ] ; ent [ 13 ] = HEX_CHARS [ lo & 0xF ] ; _writer . write ( ent , 8 , 6 ) ; } else { ent [ 6 ] = HEX_CHARS [ ch > > 4 ] ; ent [ 7 ] = HEX_CHARS [ ch & 0xF ] ; _writer . write ( ent , 2 , 6 ) ; } } return ptr ; }
Method called to try to either prepend character escape at front of
given buffer; or if not possible, to write it out directly.

@return Pointer to start of prepended entity (if prepended); or 'ptr'
if not.