@Override public void writeString(String v) { try { final int MAX_CHARS_PER_WINDOW = 1024; if (v.length() <= MAX_CHARS_PER_WINDOW) { final BytesRef utf8 = new BytesRef(v); writeVInt(utf8.length); writeBytes(utf8.bytes, utf8.offset, utf8.length); } else { writeVInt(UnicodeUtil.calcUTF16toUTF8Length(v, 0, v.length())); final byte [] buf = new byte [UnicodeUtil.MAX_UTF8_BYTES_PER_CHAR * MAX_CHARS_PER_WINDOW]; UTF16toUTF8(v, 0, v.length(), buf, (len) -> { writeBytes(buf, 0, len); }); } } catch (IOException e) { throw new UncheckedIOException(e); } }
@Override public void writeString(String string) throws IOException { int maxLen = UnicodeUtil.maxUTF8Length(string.length()); if (maxLen <= MIN_UTF8_SIZE_TO_ENABLE_DOUBLE_PASS_ENCODING) { // string is small enough that we don't need to save memory by falling back to double-pass approach // this is just an optimized writeString() that re-uses scratchBytes. if (scratchBytes == null) { scratchBytes = new byte[ArrayUtil.oversize(maxLen, Character.BYTES)]; } else { scratchBytes = ArrayUtil.grow(scratchBytes, maxLen); } int len = UnicodeUtil.UTF16toUTF8(string, 0, string.length(), scratchBytes); writeVInt(len); writeBytes(scratchBytes, len); } else { // use a double pass approach to avoid allocating a large intermediate buffer for string encoding int numBytes = UnicodeUtil.calcUTF16toUTF8Length(string, 0, string.length()); writeVInt(numBytes); bytes = ArrayUtil.grow(bytes, length + numBytes); length = UnicodeUtil.UTF16toUTF8(string, 0, string.length(), bytes, length); } }
@Override public void writeString(String v) { try { final int MAX_CHARS_PER_WINDOW = 1024; if (v.length() <= MAX_CHARS_PER_WINDOW) { final BytesRef utf8 = new BytesRef(v); writeVInt(utf8.length); writeBytes(utf8.bytes, utf8.offset, utf8.length); } else { writeVInt(UnicodeUtil.calcUTF16toUTF8Length(v, 0, v.length())); final byte [] buf = new byte [UnicodeUtil.MAX_UTF8_BYTES_PER_CHAR * MAX_CHARS_PER_WINDOW]; UTF16toUTF8(v, 0, v.length(), buf, (len) -> { writeBytes(buf, 0, len); }); } } catch (IOException e) { throw new UncheckedIOException(e); } }
@Override public void writeString(String string) throws IOException { int maxLen = string.length() * UnicodeUtil.MAX_UTF8_BYTES_PER_CHAR; if (maxLen <= MIN_UTF8_SIZE_TO_ENABLE_DOUBLE_PASS_ENCODING) { // string is small enough that we don't need to save memory by falling back to double-pass approach // this is just an optimized writeString() that re-uses scratchBytes. scratchBytes = ArrayUtil.grow(scratchBytes, maxLen); int len = UnicodeUtil.UTF16toUTF8(string, 0, string.length(), scratchBytes); writeVInt(len); writeBytes(scratchBytes, len); } else { // use a double pass approach to avoid allocating a large intermediate buffer for string encoding int numBytes = UnicodeUtil.calcUTF16toUTF8Length(string, 0, string.length()); writeVInt(numBytes); bytes = ArrayUtil.grow(bytes, length + numBytes); length = UnicodeUtil.UTF16toUTF8(string, 0, string.length(), bytes, length); } } }
@Override public void writeString(String string) throws IOException { int maxLen = string.length() * UnicodeUtil.MAX_UTF8_BYTES_PER_CHAR; if (maxLen <= MIN_UTF8_SIZE_TO_ENABLE_DOUBLE_PASS_ENCODING) { // string is small enough that we don't need to save memory by falling back to double-pass approach // this is just an optimized writeString() that re-uses scratchBytes. scratchBytes = ArrayUtil.grow(scratchBytes, maxLen); int len = UnicodeUtil.UTF16toUTF8(string, 0, string.length(), scratchBytes); writeVInt(len); writeBytes(scratchBytes, len); } else { // use a double pass approach to avoid allocating a large intermediate buffer for string encoding int numBytes = UnicodeUtil.calcUTF16toUTF8Length(string, 0, string.length()); writeVInt(numBytes); bytes = ArrayUtil.grow(bytes, length + numBytes); length = UnicodeUtil.UTF16toUTF8(string, 0, string.length(), bytes, length); } } }
@Override public void writeString(String string) throws IOException { int maxLen = UnicodeUtil.maxUTF8Length(string.length()); if (maxLen <= MIN_UTF8_SIZE_TO_ENABLE_DOUBLE_PASS_ENCODING) { // string is small enough that we don't need to save memory by falling back to double-pass approach // this is just an optimized writeString() that re-uses scratchBytes. if (scratchBytes == null) { scratchBytes = new byte[ArrayUtil.oversize(maxLen, Character.BYTES)]; } else { scratchBytes = ArrayUtil.grow(scratchBytes, maxLen); } int len = UnicodeUtil.UTF16toUTF8(string, 0, string.length(), scratchBytes); writeVInt(len); writeBytes(scratchBytes, len); } else { // use a double pass approach to avoid allocating a large intermediate buffer for string encoding int numBytes = UnicodeUtil.calcUTF16toUTF8Length(string, 0, string.length()); writeVInt(numBytes); bytes = ArrayUtil.grow(bytes, length + numBytes); length = UnicodeUtil.UTF16toUTF8(string, 0, string.length(), bytes, length); } }