/** Returns true if any commas were skipped. */ private static boolean skipWhitespaceAndCommas(Buffer buffer) { boolean commaFound = false; while (!buffer.exhausted()) { byte b = buffer.getByte(0); if (b == ',') { buffer.readByte(); // Consume ','. commaFound = true; } else if (b == ' ' || b == '\t') { buffer.readByte(); // Consume space or tab. } else { break; } } return commaFound; }
@CheckReturnValue public static Options of(String... strings) { try { ByteString[] result = new ByteString[strings.length]; Buffer buffer = new Buffer(); for (int i = 0; i < strings.length; i++) { JsonUtf8Writer.string(buffer, strings[i]); buffer.readByte(); // Skip the leading double quote (but leave the trailing one). result[i] = buffer.readByteString(); } return new Options(strings.clone(), okio.Options.of(result)); } catch (IOException e) { throw new AssertionError(e); } } }
private static void skipName(Buffer in) throws EOFException { // 0 - 63 bytes int length = in.readByte(); if (length < 0) { // compressed name pointer, first two bits are 1 // drop second byte of compression offset in.skip(1); } else { while (length > 0) { // skip each part of the domain name in.skip(length); length = in.readByte(); } } } }
private static int skipAll(Buffer buffer, byte b) { int count = 0; while (!buffer.exhausted() && buffer.getByte(0) == b) { count++; buffer.readByte(); } return count; }
/** Returns true if any commas were skipped. */ private static boolean skipWhitespaceAndCommas(Buffer buffer) { boolean commaFound = false; while (!buffer.exhausted()) { byte b = buffer.getByte(0); if (b == ',') { buffer.readByte(); // Consume ','. commaFound = true; } else if (b == ' ' || b == '\t') { buffer.readByte(); // Consume space or tab. } else { break; } } return commaFound; }
/** * Reads a double-quoted string, unescaping quoted pairs like {@code \"} to the 2nd character in * each sequence. Returns the unescaped string, or null if the buffer isn't prefixed with a * double-quoted string. */ private static String readQuotedString(Buffer buffer) { if (buffer.readByte() != '\"') throw new IllegalArgumentException(); Buffer result = new Buffer(); while (true) { long i = buffer.indexOfElement(QUOTED_STRING_DELIMITERS); if (i == -1L) return null; // Unterminated quoted string. if (buffer.getByte(i) == '"') { result.write(buffer, i); buffer.readByte(); // Consume '"'. return result.readUtf8(); } if (buffer.size() == i + 1L) return null; // Dangling escape. result.write(buffer, i); buffer.readByte(); // Consume '\'. result.write(buffer, 1L); // The escaped character. } }
/** * @param toFind a string to search for. Must not contain a newline. */ private boolean skipTo(String toFind) throws IOException { outer: for (; source.request(toFind.length());) { for (int c = 0; c < toFind.length(); c++) { if (buffer.getByte(c) != toFind.charAt(c)) { buffer.readByte(); continue outer; } } return true; } return false; }
private static int skipAll(Buffer buffer, byte b) { int count = 0; while (!buffer.exhausted() && buffer.getByte(0) == b) { count++; buffer.readByte(); } return count; }
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); } } }
int b = encodedCharBuffer.readByte() & 0xff; out.writeByte('%'); out.writeByte(HEX_DIGITS[(b >> 4) & 0xf]);
if (builder == null) builder = new StringBuilder(); builder.append(buffer.readUtf8(index)); buffer.readByte(); // '\' builder.append(readEscapeCharacter()); continue; buffer.readByte(); // Consume the quote character. return result; } else { builder.append(buffer.readUtf8(index)); buffer.readByte(); // Consume the quote character. return builder.toString();
/** * Reads a double-quoted string, unescaping quoted pairs like {@code \"} to the 2nd character in * each sequence. Returns the unescaped string, or null if the buffer isn't prefixed with a * double-quoted string. */ private static String readQuotedString(Buffer buffer) { if (buffer.readByte() != '\"') throw new IllegalArgumentException(); Buffer result = new Buffer(); while (true) { long i = buffer.indexOfElement(QUOTED_STRING_DELIMITERS); if (i == -1L) return null; // Unterminated quoted string. if (buffer.getByte(i) == '"') { result.write(buffer, i); buffer.readByte(); // Consume '"'. return result.readUtf8(); } if (buffer.size() == i + 1L) return null; // Dangling escape. result.write(buffer, i); buffer.readByte(); // Consume '\'. result.write(buffer, 1L); // The escaped character. } }
if (builder == null) builder = new StringBuilder(); builder.append(buffer.readUtf8(index)); buffer.readByte(); // '\' builder.append(readEscapeCharacter()); continue; buffer.readByte(); // Consume the quote character. return result; } else { builder.append(buffer.readUtf8(index)); buffer.readByte(); // Consume the quote character. return builder.toString();
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); } } }
int b = utf8Buffer.readByte() & 0xff; out.writeByte('%'); out.writeByte(HEX_DIGITS[(b >> 4) & 0xf]);
int b = encodedCharBuffer.readByte() & 0xff; out.writeByte('%'); out.writeByte(HEX_DIGITS[(b >> 4) & 0xf]);
buffer.readByte(); // consume ']' or ','. switch (c) { case ']': buffer.readByte(); // Consume '}' or ','. switch (c) { case '}': switch (c) { case '"': buffer.readByte(); // consume the '\"'. return peeked = PEEKED_DOUBLE_QUOTED_NAME; case '\'': buffer.readByte(); // consume the '\''. checkLenient(); return peeked = PEEKED_SINGLE_QUOTED_NAME; case '}': if (peekStack != JsonScope.NONEMPTY_OBJECT) { buffer.readByte(); // consume the '}'. return peeked = PEEKED_END_OBJECT; } else { buffer.readByte(); // Consume ':'. switch (c) { case ':': checkLenient(); if (source.request(1) && buffer.getByte(0) == '>') { buffer.readByte(); // Consume '>'.
@Test public void closeEmitsBufferedBytes() throws IOException { sink.writeByte('a'); sink.close(); assertEquals('a', data.readByte()); }
byte escaped = buffer.readByte(); switch (escaped) { case 'u':