com.fasterxml.jackson.core.io.CharacterEscapes Java Examples

The following examples show how to use com.fasterxml.jackson.core.io.CharacterEscapes. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JsonEncoder.java    From metafacture-core with Apache License 2.0 6 votes vote down vote up
/**
 * By default JSON output does only have escaping where it is strictly
 * necessary. This is recommended in the most cases. Nevertheless it can
 * be sometimes useful to have some more escaping.
 *
 * @param escapeCharacters an array which defines which characters should be
 *                         escaped and how it will be done. See
 *                         {@link CharacterEscapes}. In most cases this should
 *                         be null. Use like this:
 *                         <pre>{@code int[] esc = CharacterEscapes.standardAsciiEscapesForJSON();
 *                            // and force escaping of a few others:
 *                            esc['\''] = CharacterEscapes.ESCAPE_STANDARD;
 *                         JsonEncoder.useEscapeJavaScript(esc);
 *                         }</pre>
 */
public void setJavaScriptEscapeChars(final int[] escapeCharacters) {

    final CharacterEscapes ce = new CharacterEscapes() {

        private static final long serialVersionUID = 1L;

        @Override
        public int[] getEscapeCodesForAscii() {
            if (escapeCharacters == null) {
                return CharacterEscapes.standardAsciiEscapesForJSON();
            }
            return escapeCharacters;
        }

        @Override
        public SerializableString getEscapeSequence(final int ch) {
            final String jsEscaped = escapeChar((char) ch);
            return new SerializedString(jsEscaped);
        }

    };

    jsonGenerator.setCharacterEscapes(ce);
}
 
Example #2
Source File: JsonGeneratorImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public JsonGenerator setCharacterEscapes(CharacterEscapes esc)
{
    _characterEscapes = esc;
    if (esc == null) { // revert to standard escapes
        _outputEscapes = sOutputEscapes;
    } else {
        _outputEscapes = esc.getEscapeCodesForAscii();
    }
    return this;
}
 
Example #3
Source File: DescribeSchemaHandler.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public int[] getEscapeCodesForAscii() {
  // add standard set of escaping characters
  int[] esc = CharacterEscapes.standardAsciiEscapesForJSON();
  // don't escape backslash (not to corrupt windows path)
  esc['\\'] = CharacterEscapes.ESCAPE_NONE;
  return esc;
}
 
Example #4
Source File: MessageMarshaller.java    From curiostack with MIT License 5 votes vote down vote up
@Override
public int[] getEscapeCodesForAscii() {
  int[] escapes = CharacterEscapes.standardAsciiEscapesForJSON();
  // From
  // https://github.com/google/gson/blob/bac26b8e429150d4cbf807e8692f207b7ce7d40d/gson/src/main/java/com/google/gson/stream/JsonWriter.java#L158
  escapes['<'] = CharacterEscapes.ESCAPE_CUSTOM;
  escapes['>'] = CharacterEscapes.ESCAPE_CUSTOM;
  escapes['&'] = CharacterEscapes.ESCAPE_STANDARD;
  escapes['='] = CharacterEscapes.ESCAPE_STANDARD;
  escapes['\''] = CharacterEscapes.ESCAPE_STANDARD;
  return escapes;
}
 
Example #5
Source File: ObjectMapperFactory.java    From etf-webapp with European Union Public License 1.2 5 votes vote down vote up
public HTMLCharacterEscapes() {
    asciiEscapes = CharacterEscapes.standardAsciiEscapesForJSON();
    asciiEscapes['<'] = CharacterEscapes.ESCAPE_CUSTOM;
    asciiEscapes['>'] = CharacterEscapes.ESCAPE_CUSTOM;
    asciiEscapes['&'] = CharacterEscapes.ESCAPE_CUSTOM;
    asciiEscapes['"'] = CharacterEscapes.ESCAPE_CUSTOM;
    asciiEscapes['\''] = CharacterEscapes.ESCAPE_CUSTOM;
}
 
Example #6
Source File: WriterBasedJsonGenerator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void _writeSegmentASCII(int end, final int maxNonEscaped)
    throws IOException, JsonGenerationException
{
    final int[] escCodes = _outputEscapes;
    final int escLimit = Math.min(escCodes.length, maxNonEscaped+1);

    int ptr = 0;
    int escCode = 0;
    int start = ptr;

    output_loop:
    while (ptr < end) {
        // Fast loop for chars not needing escaping
        char c;
        while (true) {
            c = _outputBuffer[ptr];
            if (c < escLimit) {
                escCode = escCodes[c];
                if (escCode != 0) {
                    break;
                }
            } else if (c > maxNonEscaped) {
                escCode = CharacterEscapes.ESCAPE_STANDARD;
                break;
            }
            if (++ptr >= end) {
                break;
            }
        }
        int flushLen = (ptr - start);
        if (flushLen > 0) {
            _writer.write(_outputBuffer, start, flushLen);
            if (ptr >= end) {
                break output_loop;
            }
        }
        ++ptr;
        start = _prependOrWriteCharacterEscape(_outputBuffer, ptr, end, c, escCode);
    }
}
 
Example #7
Source File: WriterBasedJsonGenerator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void _writeStringASCII(final int len, final int maxNonEscaped)
    throws IOException, JsonGenerationException
{
    // And then we'll need to verify need for escaping etc:
    int end = _outputTail + len;
    final int[] escCodes = _outputEscapes;
    final int escLimit = Math.min(escCodes.length, maxNonEscaped+1);
    int escCode = 0;
    
    output_loop:
    while (_outputTail < end) {
        char c;
        // Fast loop for chars not needing escaping
        escape_loop:
        while (true) {
            c = _outputBuffer[_outputTail];
            if (c < escLimit) {
                escCode = escCodes[c];
                if (escCode != 0) {
                    break escape_loop;
                }
            } else if (c > maxNonEscaped) {
                escCode = CharacterEscapes.ESCAPE_STANDARD;
                break escape_loop;
            }
            if (++_outputTail >= end) {
                break output_loop;
            }
        }
        int flushLen = (_outputTail - _outputHead);
        if (flushLen > 0) {
            _writer.write(_outputBuffer, _outputHead, flushLen);
        }
        ++_outputTail;
        _prependOrWriteCharacterEscape(c, escCode);
    }
}
 
Example #8
Source File: JsonGeneratorImpl.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
@Override
public JsonGenerator setCharacterEscapes(CharacterEscapes esc)
{
    _characterEscapes = esc;
    if (esc == null) { // revert to standard escapes
        _outputEscapes = sOutputEscapes;
    } else {
        _outputEscapes = esc.getEscapeCodesForAscii();
    }
    return this;
}
 
Example #9
Source File: ObjectWriter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public GeneratorSettings(PrettyPrinter pp, FormatSchema sch,
        CharacterEscapes esc, SerializableString rootSep) {
    prettyPrinter = pp;
    schema = sch;
    characterEscapes = esc;
    rootValueSeparator = rootSep;
}
 
Example #10
Source File: CharEscapeUtil.java    From bboss-elasticsearch with Apache License 2.0 5 votes vote down vote up
private void _writeStringASCII(final int len, final int maxNonEscaped)
		throws IOException, JsonGenerationException
{
	// And then we'll need to verify need for escaping etc:
	int end = _outputTail + len;
	final int[] escCodes = _outputEscapes;
	final int escLimit = Math.min(escCodes.length, maxNonEscaped+1);
	int escCode = 0;

	output_loop:
	while (_outputTail < end) {
		char c;
		// Fast loop for chars not needing escaping
		escape_loop:
		while (true) {
			c = _outputBuffer[_outputTail];
			if (c < escLimit) {
				escCode = escCodes[c];
				if (escCode != 0) {
					break escape_loop;
				}
			} else if (c > maxNonEscaped) {
				escCode = CharacterEscapes.ESCAPE_STANDARD;
				break escape_loop;
			}
			if (++_outputTail >= end) {
				break output_loop;
			}
		}
		int flushLen = (_outputTail - _outputHead);
		if (flushLen > 0) {
			_writer.write(_outputBuffer, _outputHead, flushLen);
		}
		++_outputTail;
		_prependOrWriteCharacterEscape(c, escCode);
	}
}
 
Example #11
Source File: CharEscapeUtil.java    From bboss-elasticsearch with Apache License 2.0 5 votes vote down vote up
private void _writeSegmentASCII(int end, final int maxNonEscaped)
		throws IOException, JsonGenerationException
{
	final int[] escCodes = _outputEscapes;
	final int escLimit = Math.min(escCodes.length, maxNonEscaped+1);

	int ptr = 0;
	int escCode = 0;
	int start = ptr;

	output_loop:
	while (ptr < end) {
		// Fast loop for chars not needing escaping
		char c;
		while (true) {
			c = _outputBuffer[ptr];
			if (c < escLimit) {
				escCode = escCodes[c];
				if (escCode != 0) {
					break;
				}
			} else if (c > maxNonEscaped) {
				escCode = CharacterEscapes.ESCAPE_STANDARD;
				break;
			}
			if (++ptr >= end) {
				break;
			}
		}
		int flushLen = (ptr - start);
		if (flushLen > 0) {
			_writer.write(_outputBuffer, start, flushLen);
			if (ptr >= end) {
				break output_loop;
			}
		}
		++ptr;
		start = _prependOrWriteCharacterEscape(_outputBuffer, ptr, end, c, escCode);
	}
}
 
Example #12
Source File: JsonGeneratorImpl.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Method for accessing custom escapes factory uses for {@link JsonGenerator}s
 * it creates.
 */
@Override
public CharacterEscapes getCharacterEscapes() {
    return _characterEscapes;
}
 
Example #13
Source File: JsonGeneratorDelegate.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
@Override
public CharacterEscapes getCharacterEscapes() {  return delegate.getCharacterEscapes(); }
 
Example #14
Source File: JsonGeneratorDelegate.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
@Override
public JsonGenerator setCharacterEscapes(CharacterEscapes esc) { delegate.setCharacterEscapes(esc);
    return this; }
 
Example #15
Source File: JacksonService.java    From agrest with Apache License 2.0 4 votes vote down vote up
public JacksonService() {

		// fun Jackson API with circular dependencies ... so we create a mapper
		// first, and grab implicitly created factory from it
		this.sharedMapper = new ObjectMapper();
		this.sharedFactory = sharedMapper.getFactory();

		final SerializableString LINE_SEPARATOR = new SerializedString("\\u2028");
		final SerializableString PARAGRAPH_SEPARATOR = new SerializedString("\\u2029");

		this.sharedFactory.setCharacterEscapes(new CharacterEscapes() {

			private static final long serialVersionUID = 3995801066651016289L;

			@Override
			public int[] getEscapeCodesForAscii() {
				return standardAsciiEscapesForJSON();
			}

			@Override
			public SerializableString getEscapeSequence(int ch) {
				// see ECMA-262 Section 7.3;
				// in most cases our client is browser,
				// and JSON is parsed into JS;
				// therefore these two whitespace characters,
				// which are perfectly valid in JSON but invalid in JS strings,
				// need to be escaped...
				switch (ch) {
				case '\u2028':
					return LINE_SEPARATOR;
				case '\u2029':
					return PARAGRAPH_SEPARATOR;
				default:
					return null;
				}
			}
		});

		// make sure mapper does not attempt closing streams it does not
		// manage... why is this even a default in jackson?
		sharedFactory.disable(Feature.AUTO_CLOSE_TARGET);

		// do not flush every time. why would we want to do that?
		// this is having a HUGE impact on extrest serializers (5x speedup)
		sharedMapper.disable(SerializationFeature.FLUSH_AFTER_WRITE_VALUE);
	}
 
Example #16
Source File: JSON.java    From jackson-jr with Apache License 2.0 4 votes vote down vote up
@Override
public CharacterEscapes getCharacterEscapes() {
    return null;
}
 
Example #17
Source File: JsonGeneratorDelegate.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public JsonGenerator setCharacterEscapes(CharacterEscapes esc) { delegate.setCharacterEscapes(esc);
    return this; }
 
Example #18
Source File: JsonGeneratorDelegate.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public CharacterEscapes getCharacterEscapes() {  return delegate.getCharacterEscapes(); }
 
Example #19
Source File: WriterBasedJsonGenerator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Method called to append escape sequence for given character, at the
 * end of standard output buffer; or if not possible, write out directly.
 */
private void _appendCharacterEscape(char ch, int escCode)
    throws IOException, JsonGenerationException
{
    if (escCode >= 0) { // \\N (2 char)
        if ((_outputTail + 2) > _outputEnd) {
            _flushBuffer();
        }
        _outputBuffer[_outputTail++] = '\\';
        _outputBuffer[_outputTail++] = (char) escCode;
        return;
    }
    if (escCode != CharacterEscapes.ESCAPE_CUSTOM) { // std, \\uXXXX
        if ((_outputTail + 5) >= _outputEnd) {
            _flushBuffer();
        }
        int ptr = _outputTail;
        char[] buf = _outputBuffer;
        buf[ptr++] = '\\';
        buf[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;
            buf[ptr++] = HEX_CHARS[hi >> 4];
            buf[ptr++] = HEX_CHARS[hi & 0xF];
            ch &= 0xFF;
        } else {
            buf[ptr++] = '0';
            buf[ptr++] = '0';
        }
        buf[ptr++] = HEX_CHARS[ch >> 4];
        buf[ptr++] = HEX_CHARS[ch & 0xF];
        _outputTail = ptr;
        return;
    }
    String escape;
    if (_currentEscape == null) {
        escape = _characterEscapes.getEscapeSequence(ch).getValue();
    } else {
        escape = _currentEscape.getValue();
        _currentEscape = null;
    }
    int len = escape.length();
    if ((_outputTail + len) > _outputEnd) {
        _flushBuffer();
        if (len > _outputEnd) { // very very long escape; unlikely but theoretically possible
            _writer.write(escape);
            return;
        }
    }
    escape.getChars(0, len, _outputBuffer, _outputTail);
    _outputTail += len;
}
 
Example #20
Source File: JsonGeneratorImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Method for accessing custom escapes factory uses for {@link JsonGenerator}s
 * it creates.
 */
@Override
public CharacterEscapes getCharacterEscapes() {
    return _characterEscapes;
}
 
Example #21
Source File: WriterBasedJsonGenerator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void _writeStringCustom(char[] text, int offset, int len)
    throws IOException, JsonGenerationException
{
    len += offset; // -> len marks the end from now on
    final int[] escCodes = _outputEscapes;
    final int maxNonEscaped = (_maximumNonEscapedChar < 1) ? 0xFFFF : _maximumNonEscapedChar;
    final int escLimit = Math.min(escCodes.length, maxNonEscaped+1);
    final CharacterEscapes customEscapes = _characterEscapes;

    int escCode = 0;
    
    while (offset < len) {
        int start = offset;
        char c;
        
        while (true) {
            c = text[offset];
            if (c < escLimit) {
                escCode = escCodes[c];
                if (escCode != 0) {
                    break;
                }
            } else if (c > maxNonEscaped) {
                escCode = CharacterEscapes.ESCAPE_STANDARD;
                break;
            } else {
                if ((_currentEscape = customEscapes.getEscapeSequence(c)) != null) {
                    escCode = CharacterEscapes.ESCAPE_CUSTOM;
                    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.
        ++offset;
        _appendCharacterEscape(c, escCode);
    }
}
 
Example #22
Source File: WriterBasedJsonGenerator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void _writeSegmentCustom(int end)
    throws IOException, JsonGenerationException
{
    final int[] escCodes = _outputEscapes;
    final int maxNonEscaped = (_maximumNonEscapedChar < 1) ? 0xFFFF : _maximumNonEscapedChar;
    final int escLimit = Math.min(escCodes.length, maxNonEscaped+1);
    final CharacterEscapes customEscapes = _characterEscapes;

    int ptr = 0;
    int escCode = 0;
    int start = ptr;

    output_loop:
    while (ptr < end) {
        // Fast loop for chars not needing escaping
        char c;
        while (true) {
            c = _outputBuffer[ptr];
            if (c < escLimit) {
                escCode = escCodes[c];
                if (escCode != 0) {
                    break;
                }
            } else if (c > maxNonEscaped) {
                escCode = CharacterEscapes.ESCAPE_STANDARD;
                break;
            } else {
                if ((_currentEscape = customEscapes.getEscapeSequence(c)) != null) {
                    escCode = CharacterEscapes.ESCAPE_CUSTOM;
                    break;
                }
            }
            if (++ptr >= end) {
                break;
            }
        }
        int flushLen = (ptr - start);
        if (flushLen > 0) {
            _writer.write(_outputBuffer, start, flushLen);
            if (ptr >= end) {
                break output_loop;
            }
        }
        ++ptr;
        start = _prependOrWriteCharacterEscape(_outputBuffer, ptr, end, c, escCode);
    }
}
 
Example #23
Source File: WriterBasedJsonGenerator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void _writeStringCustom(final int len)
    throws IOException, JsonGenerationException
{
    // And then we'll need to verify need for escaping etc:
    int end = _outputTail + len;
    final int[] escCodes = _outputEscapes;
    final int maxNonEscaped = (_maximumNonEscapedChar < 1) ? 0xFFFF : _maximumNonEscapedChar;
    final int escLimit = Math.min(escCodes.length, maxNonEscaped+1);
    int escCode = 0;
    final CharacterEscapes customEscapes = _characterEscapes;

    output_loop:
    while (_outputTail < end) {
        char c;
        // Fast loop for chars not needing escaping
        escape_loop:
        while (true) {
            c = _outputBuffer[_outputTail];
            if (c < escLimit) {
                escCode = escCodes[c];
                if (escCode != 0) {
                    break escape_loop;
                }
            } else if (c > maxNonEscaped) {
                escCode = CharacterEscapes.ESCAPE_STANDARD;
                break escape_loop;
            } else {
                if ((_currentEscape = customEscapes.getEscapeSequence(c)) != null) {
                    escCode = CharacterEscapes.ESCAPE_CUSTOM;
                    break escape_loop;
                }
            }
            if (++_outputTail >= end) {
                break output_loop;
            }
        }
        int flushLen = (_outputTail - _outputHead);
        if (flushLen > 0) {
            _writer.write(_outputBuffer, _outputHead, flushLen);
        }
        ++_outputTail;
        _prependOrWriteCharacterEscape(c, escCode);
    }
}
 
Example #24
Source File: WriterBasedJsonGenerator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void _writeStringASCII(char[] text, int offset, int len,
        final int maxNonEscaped)
    throws IOException, JsonGenerationException
{
    len += offset; // -> len marks the end from now on
    final int[] escCodes = _outputEscapes;
    final int escLimit = Math.min(escCodes.length, maxNonEscaped+1);

    int escCode = 0;
    
    while (offset < len) {
        int start = offset;
        char c;
        
        while (true) {
            c = text[offset];
            if (c < escLimit) {
                escCode = escCodes[c];
                if (escCode != 0) {
                    break;
                }
            } else if (c > maxNonEscaped) {
                escCode = CharacterEscapes.ESCAPE_STANDARD;
                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.
        ++offset;
        _appendCharacterEscape(c, escCode);
    }
}
 
Example #25
Source File: ObjectWriter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public GeneratorSettings with(CharacterEscapes esc) {
    return (characterEscapes == esc) ? this
            : new GeneratorSettings(prettyPrinter, schema, esc, rootValueSeparator);
}
 
Example #26
Source File: ObjectWriter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @since 2.3
 */
public ObjectWriter with(CharacterEscapes escapes) {
    return _new(_generatorSettings.with(escapes), _prefetch);
}
 
Example #27
Source File: CharEscapeUtil.java    From bboss-elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Method called to append escape sequence for given character, at the
 * end of standard output buffer; or if not possible, write out directly.
 */
private void _appendCharacterEscape(char ch, int escCode)
		throws IOException, JsonGenerationException
{
	if (escCode >= 0) { // \\N (2 char)
		if ((_outputTail + 2) > _outputEnd) {
			_flushBuffer();
		}
		_outputBuffer[_outputTail++] = '\\';
		_outputBuffer[_outputTail++] = (char) escCode;
		return;
	}
	if (escCode != CharacterEscapes.ESCAPE_CUSTOM) { // std, \\uXXXX
		if ((_outputTail + 5) >= _outputEnd) {
			_flushBuffer();
		}
		int ptr = _outputTail;
		char[] buf = _outputBuffer;
		buf[ptr++] = '\\';
		buf[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;
			buf[ptr++] = HEX_CHARS[hi >> 4];
			buf[ptr++] = HEX_CHARS[hi & 0xF];
			ch &= 0xFF;
		} else {
			buf[ptr++] = '0';
			buf[ptr++] = '0';
		}
		buf[ptr++] = HEX_CHARS[ch >> 4];
		buf[ptr++] = HEX_CHARS[ch & 0xF];
		_outputTail = ptr;
		return;
	}
	String escape;
	if (_currentEscape == null) {
		escape = _characterEscapes.getEscapeSequence(ch).getValue();
	} else {
		escape = _currentEscape.getValue();
		_currentEscape = null;
	}
	int len = escape.length();
	if ((_outputTail + len) > _outputEnd) {
		_flushBuffer();
		if (len > _outputEnd) { // very very long escape; unlikely but theoretically possible
			_writer.write(escape);
			return;
		}
	}
	escape.getChars(0, len, _outputBuffer, _outputTail);
	_outputTail += len;
}
 
Example #28
Source File: CharEscapeUtil.java    From bboss-elasticsearch with Apache License 2.0 4 votes vote down vote up
private void _writeStringCustom(char[] text, int offset, int len)
		throws IOException, JsonGenerationException
{
	len += offset; // -> len marks the end from now on
	final int[] escCodes = _outputEscapes;
	final int maxNonEscaped = (_maximumNonEscapedChar < 1) ? 0xFFFF : _maximumNonEscapedChar;
	final int escLimit = Math.min(escCodes.length, maxNonEscaped+1);
	final CharacterEscapes customEscapes = _characterEscapes;

	int escCode = 0;

	while (offset < len) {
		int start = offset;
		char c;

		while (true) {
			c = text[offset];
			if (c < escLimit) {
				escCode = escCodes[c];
				if (escCode != 0) {
					break;
				}
			} else if (c > maxNonEscaped) {
				escCode = CharacterEscapes.ESCAPE_STANDARD;
				break;
			} else {
				if ((_currentEscape = customEscapes.getEscapeSequence(c)) != null) {
					escCode = CharacterEscapes.ESCAPE_CUSTOM;
					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.
		++offset;
		_appendCharacterEscape(c, escCode);
	}
}
 
Example #29
Source File: CharEscapeUtil.java    From bboss-elasticsearch with Apache License 2.0 4 votes vote down vote up
private void _writeStringCustom(final int len)
		throws IOException, JsonGenerationException
{
	// And then we'll need to verify need for escaping etc:
	int end = _outputTail + len;
	final int[] escCodes = _outputEscapes;
	final int maxNonEscaped = (_maximumNonEscapedChar < 1) ? 0xFFFF : _maximumNonEscapedChar;
	final int escLimit = Math.min(escCodes.length, maxNonEscaped+1);
	int escCode = 0;
	final CharacterEscapes customEscapes = _characterEscapes;

	output_loop:
	while (_outputTail < end) {
		char c;
		// Fast loop for chars not needing escaping
		escape_loop:
		while (true) {
			c = _outputBuffer[_outputTail];
			if (c < escLimit) {
				escCode = escCodes[c];
				if (escCode != 0) {
					break escape_loop;
				}
			} else if (c > maxNonEscaped) {
				escCode = CharacterEscapes.ESCAPE_STANDARD;
				break escape_loop;
			} else {
				if ((_currentEscape = customEscapes.getEscapeSequence(c)) != null) {
					escCode = CharacterEscapes.ESCAPE_CUSTOM;
					break escape_loop;
				}
			}
			if (++_outputTail >= end) {
				break output_loop;
			}
		}
		int flushLen = (_outputTail - _outputHead);
		if (flushLen > 0) {
			_writer.write(_outputBuffer, _outputHead, flushLen);
		}
		++_outputTail;
		_prependOrWriteCharacterEscape(c, escCode);
	}
}
 
Example #30
Source File: CharEscapeUtil.java    From bboss-elasticsearch with Apache License 2.0 4 votes vote down vote up
private void _writeSegmentCustom(int end)
		throws IOException, JsonGenerationException
{
	final int[] escCodes = _outputEscapes;
	final int maxNonEscaped = (_maximumNonEscapedChar < 1) ? 0xFFFF : _maximumNonEscapedChar;
	final int escLimit = Math.min(escCodes.length, maxNonEscaped+1);
	final CharacterEscapes customEscapes = _characterEscapes;

	int ptr = 0;
	int escCode = 0;
	int start = ptr;

	output_loop:
	while (ptr < end) {
		// Fast loop for chars not needing escaping
		char c;
		while (true) {
			c = _outputBuffer[ptr];
			if (c < escLimit) {
				escCode = escCodes[c];
				if (escCode != 0) {
					break;
				}
			} else if (c > maxNonEscaped) {
				escCode = CharacterEscapes.ESCAPE_STANDARD;
				break;
			} else {
				if ((_currentEscape = customEscapes.getEscapeSequence(c)) != null) {
					escCode = CharacterEscapes.ESCAPE_CUSTOM;
					break;
				}
			}
			if (++ptr >= end) {
				break;
			}
		}
		int flushLen = (ptr - start);
		if (flushLen > 0) {
			_writer.write(_outputBuffer, start, flushLen);
			if (ptr >= end) {
				break output_loop;
			}
		}
		++ptr;
		start = _prependOrWriteCharacterEscape(_outputBuffer, ptr, end, c, escCode);
	}
}