/** Returns {@code s} with control characters and non-ASCII characters replaced with '?'. */ private static String toHumanReadableAscii(String s) { for (int i = 0, length = s.length(), c; i < length; i += Character.charCount(c)) { c = s.codePointAt(i); if (c > '\u001f' && c < '\u007f') continue; Buffer buffer = new Buffer(); buffer.writeUtf8(s, 0, i); buffer.writeUtf8CodePoint('?'); for (int j = i + Character.charCount(c); j < length; j += Character.charCount(c)) { c = s.codePointAt(j); buffer.writeUtf8CodePoint(c > '\u001f' && c < '\u007f' ? c : '?'); } return buffer.readUtf8(); } return s; }
static void percentDecode(Buffer out, String encoded, int pos, int limit, boolean plusIsSpace) { int codePoint; for (int i = pos; i < limit; i += Character.charCount(codePoint)) { codePoint = encoded.codePointAt(i); if (codePoint == '%' && i + 2 < limit) { int d1 = decodeHexDigit(encoded.charAt(i + 1)); int d2 = decodeHexDigit(encoded.charAt(i + 2)); if (d1 != -1 && d2 != -1) { out.writeByte((d1 << 4) + d2); i += 2; continue; } } else if (codePoint == '+' && plusIsSpace) { out.writeByte(' '); continue; } out.writeUtf8CodePoint(codePoint); } }
private static void canonicalizeForPath(Buffer out, String input, int pos, int limit, boolean alreadyEncoded) { Buffer utf8Buffer = null; // Lazily allocated. int codePoint; for (int i = pos; i < limit; i += Character.charCount(codePoint)) { codePoint = input.codePointAt(i); if (alreadyEncoded && (codePoint == '\t' || codePoint == '\n' || codePoint == '\f' || codePoint == '\r')) { // Skip this character. } else if (codePoint < 0x20 || codePoint >= 0x7f || PATH_SEGMENT_ALWAYS_ENCODE_SET.indexOf(codePoint) != -1 || (!alreadyEncoded && (codePoint == '/' || codePoint == '%'))) { // Percent encode this character. if (utf8Buffer == null) { utf8Buffer = new Buffer(); } utf8Buffer.writeUtf8CodePoint(codePoint); while (!utf8Buffer.exhausted()) { int b = utf8Buffer.readByte() & 0xff; out.writeByte('%'); out.writeByte(HEX_DIGITS[(b >> 4) & 0xf]); out.writeByte(HEX_DIGITS[b & 0xf]); } } else { // This character doesn't need encoding. Just copy it over. out.writeUtf8CodePoint(codePoint); } } }
static void percentDecode(Buffer out, String encoded, int pos, int limit) { int codePoint; for (int i = pos; i < limit; i += Character.charCount(codePoint)) { codePoint = encoded.codePointAt(i); if (codePoint == '%' && i + 2 < limit) { int d1 = decodeHexDigit(encoded.charAt(i + 1)); int d2 = decodeHexDigit(encoded.charAt(i + 2)); if (d1 != -1 && d2 != -1) { out.writeByte((d1 << 4) + d2); i += 2; continue; } } out.writeUtf8CodePoint(codePoint); } }
encodedCharBuffer.writeUtf8CodePoint(codePoint); } else { encodedCharBuffer.writeString(input, i, i + Character.charCount(codePoint), charset); out.writeUtf8CodePoint(codePoint);
static void percentDecode(Buffer out, String encoded, int pos, int limit, boolean plusIsSpace) { int codePoint; for (int i = pos; i < limit; i += Character.charCount(codePoint)) { codePoint = encoded.codePointAt(i); if (codePoint == '%' && i + 2 < limit) { int d1 = decodeHexDigit(encoded.charAt(i + 1)); int d2 = decodeHexDigit(encoded.charAt(i + 2)); if (d1 != -1 && d2 != -1) { out.writeByte((d1 << 4) + d2); i += 2; continue; } } else if (codePoint == '+' && plusIsSpace) { out.writeByte(' '); continue; } out.writeUtf8CodePoint(codePoint); } }
private static void canonicalizeForPath(Buffer out, String input, int pos, int limit, boolean alreadyEncoded) { Buffer utf8Buffer = null; // Lazily allocated. int codePoint; for (int i = pos; i < limit; i += Character.charCount(codePoint)) { codePoint = input.codePointAt(i); if (alreadyEncoded && (codePoint == '\t' || codePoint == '\n' || codePoint == '\f' || codePoint == '\r')) { // Skip this character. } else if (codePoint < 0x20 || codePoint >= 0x7f || PATH_SEGMENT_ALWAYS_ENCODE_SET.indexOf(codePoint) != -1 || (!alreadyEncoded && (codePoint == '/' || codePoint == '%'))) { // Percent encode this character. if (utf8Buffer == null) { utf8Buffer = new Buffer(); } utf8Buffer.writeUtf8CodePoint(codePoint); while (!utf8Buffer.exhausted()) { int b = utf8Buffer.readByte() & 0xff; out.writeByte('%'); out.writeByte(HEX_DIGITS[(b >> 4) & 0xf]); out.writeByte(HEX_DIGITS[b & 0xf]); } } else { // This character doesn't need encoding. Just copy it over. out.writeUtf8CodePoint(codePoint); } } }
utf8Buffer = new Buffer(); utf8Buffer.writeUtf8CodePoint(codePoint); while (!utf8Buffer.exhausted()) { int b = utf8Buffer.readByte() & 0xff; out.writeUtf8CodePoint(codePoint);
@Test public void writeCodePointBeyondUnicodeMaximum() throws Exception { Buffer buffer = new Buffer(); try { buffer.writeUtf8CodePoint(0x110000); fail(); } catch (IllegalArgumentException expected) { } }
encodedCharBuffer.writeUtf8CodePoint(codePoint); } else { encodedCharBuffer.writeString(input, i, i + Character.charCount(codePoint), charset); out.writeUtf8CodePoint(codePoint);
private void assertCodePointEncoded(String hex, int... codePoints) throws Exception { Buffer buffer = new Buffer(); for (int codePoint : codePoints) { buffer.writeUtf8CodePoint(codePoint); } assertEquals(buffer.readByteString(), ByteString.decodeHex(hex)); }
private void assertStringEncoded(String hex, String string) throws Exception { ByteString expectedUtf8 = ByteString.decodeHex(hex); // Confirm our expectations are consistent with the platform. ByteString platformUtf8 = ByteString.of(string.getBytes("UTF-8")); assertEquals(expectedUtf8, platformUtf8); // Confirm our implementation matches those expectations. ByteString actualUtf8 = new Buffer().writeUtf8(string).readByteString(); assertEquals(expectedUtf8, actualUtf8); // Confirm we are consistent when writing one code point at a time. Buffer bufferUtf8 = new Buffer(); for (int i = 0; i < string.length(); ) { int c = string.codePointAt(i); bufferUtf8.writeUtf8CodePoint(c); i += Character.charCount(c); } assertEquals(expectedUtf8, bufferUtf8.readByteString()); // Confirm we are consistent when measuring lengths. assertEquals(expectedUtf8.size(), Utf8.size(string)); assertEquals(expectedUtf8.size(), Utf8.size(string, 0, string.length())); } }
@Override public BufferedSink writeUtf8CodePoint(int codePoint) throws IOException { if (closed) throw new IllegalStateException("closed"); buffer.writeUtf8CodePoint(codePoint); return emitCompleteSegments(); }
/** Returns {@code s} with control characters and non-ASCII characters replaced with '?'. */ private static String toHumanReadableAscii(String s) { for (int i = 0, length = s.length(), c; i < length; i += Character.charCount(c)) { c = s.codePointAt(i); if (c > '\u001f' && c < '\u007f') continue; Buffer buffer = new Buffer(); buffer.writeUtf8(s, 0, i); buffer.writeUtf8CodePoint('?'); for (int j = i + Character.charCount(c); j < length; j += Character.charCount(c)) { c = s.codePointAt(j); buffer.writeUtf8CodePoint(c > '\u001f' && c < '\u007f' ? c : '?'); } return buffer.readUtf8(); } return s; }
/** Returns {@code s} with control characters and non-ASCII characters replaced with '?'. */ private static String toHumanReadableAscii(String s) { for (int i = 0, length = s.length(), c; i < length; i += Character.charCount(c)) { c = s.codePointAt(i); if (c > '\u001f' && c < '\u007f') continue; Buffer buffer = new Buffer(); buffer.writeUtf8(s, 0, i); buffer.writeUtf8CodePoint('?'); for (int j = i + Character.charCount(c); j < length; j += Character.charCount(c)) { c = s.codePointAt(j); buffer.writeUtf8CodePoint(c > '\u001f' && c < '\u007f' ? c : '?'); } return buffer.readUtf8(); } return s; }
/** Returns {@code s} with control characters and non-ASCII characters replaced with '?'. */ public static String toHumanReadableAscii(String s) { for (int i = 0, length = s.length(), c; i < length; i += Character.charCount(c)) { c = s.codePointAt(i); if (c > '\u001f' && c < '\u007f') continue; Buffer buffer = new Buffer(); buffer.writeUtf8(s, 0, i); for (int j = i; j < length; j += Character.charCount(c)) { c = s.codePointAt(j); buffer.writeUtf8CodePoint(c > '\u001f' && c < '\u007f' ? c : '?'); } return buffer.readUtf8(); } return s; }
/** Returns {@code s} with control characters and non-ASCII characters replaced with '?'. */ public static String toHumanReadableAscii(String s) { for (int i = 0, length = s.length(), c; i < length; i += Character.charCount(c)) { c = s.codePointAt(i); if (c > '\u001f' && c < '\u007f') continue; Buffer buffer = new Buffer(); buffer.writeUtf8(s, 0, i); for (int j = i; j < length; j += Character.charCount(c)) { c = s.codePointAt(j); buffer.writeUtf8CodePoint(c > '\u001f' && c < '\u007f' ? c : '?'); } return buffer.readUtf8(); } return s; }
/** Returns {@code s} with control characters and non-ASCII characters replaced with '?'. */ public static String toHumanReadableAscii(String s) { for (int i = 0, length = s.length(), c; i < length; i += Character.charCount(c)) { c = s.codePointAt(i); if (c > '\u001f' && c < '\u007f') continue; Buffer buffer = new Buffer(); buffer.writeUtf8(s, 0, i); for (int j = i; j < length; j += Character.charCount(c)) { c = s.codePointAt(j); buffer.writeUtf8CodePoint(c > '\u001f' && c < '\u007f' ? c : '?'); } return buffer.readUtf8(); } return s; }
/** Returns {@code s} with control characters and non-ASCII characters replaced with '?'. */ public static String toHumanReadableAscii(String s) { for (int i = 0, length = s.length(), c; i < length; i += Character.charCount(c)) { c = s.codePointAt(i); if (c > '\u001f' && c < '\u007f') continue; Buffer buffer = new Buffer(); buffer.writeUtf8(s, 0, i); for (int j = i; j < length; j += Character.charCount(c)) { c = s.codePointAt(j); buffer.writeUtf8CodePoint(c > '\u001f' && c < '\u007f' ? c : '?'); } return buffer.readUtf8(); } return s; }
/** Returns {@code s} with control characters and non-ASCII characters replaced with '?'. */ public static String toHumanReadableAscii(String s) { for (int i = 0, length = s.length(), c; i < length; i += Character.charCount(c)) { c = s.codePointAt(i); if (c > '\u001f' && c < '\u007f') continue; Buffer buffer = new Buffer(); buffer.writeUtf8(s, 0, i); for (int j = i; j < length; j += Character.charCount(c)) { c = s.codePointAt(j); buffer.writeUtf8CodePoint(c > '\u001f' && c < '\u007f' ? c : '?'); } return buffer.readUtf8(); } return s; }