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 the first bytes of {@link #source} are {@code key} followed by a colon or * a newline. */ private boolean isKey(ByteString key) throws IOException { if (source.rangeEquals(0, key)) { byte nextByte = source.getBuffer().getByte(key.size()); return nextByte == ':' || nextByte == '\r' || nextByte == '\n'; } return false; }
/** * Consumes the field name of the specified length and the optional colon and its optional * trailing space. Returns the number of bytes skipped. */ private long skipNameAndDivider(long length) throws IOException { source.skip(length); if (source.getBuffer().getByte(0) == ':') { source.skip(1L); length++; if (source.getBuffer().getByte(0) == ' ') { source.skip(1); length++; } } return length; } }
/** 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; }
private static int skipAll(Buffer buffer, byte b) { int count = 0; while (!buffer.exhausted() && buffer.getByte(0) == b) { count++; buffer.readByte(); } return count; }
/** * @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; }
/** Consumes {@code \r}, {@code \r\n}, or {@code \n} from {@link #source}. */ private void skipCrAndOrLf() throws IOException { if ((source.readByte() & 0xff) == '\r' && source.request(1) && source.getBuffer().getByte(0) == '\n') { source.skip(1); } }
private int peekKeyword() throws IOException { byte c = buffer.getByte(0); String keyword; String keywordUpper; return PEEKED_NONE; c = buffer.getByte(i); if (c != keyword.charAt(i) && c != keywordUpper.charAt(i)) { return PEEKED_NONE; if (source.request(length + 1) && isLiteral(buffer.getByte(length))) { return PEEKED_NONE; // Don't match trues, falsey or nullsoft!
@Test public void getByteOfEmptyBuffer() { Buffer buffer = new Buffer(); try { buffer.getByte(0); fail(); } catch (IndexOutOfBoundsException expected) { } }
/** 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. } }
if (buffer.getByte(index) == '\\') { if (builder == null) builder = new StringBuilder(); builder.append(buffer.readUtf8(index));
private void skipQuotedValue(ByteString runTerminator) throws IOException { while (true) { long index = source.indexOfElement(runTerminator); if (index == -1L) throw syntaxError("Unterminated string"); if (buffer.getByte(index) == '\\') { buffer.skip(index + 1); readEscapeCharacter(); } else { buffer.skip(index + 1); return; } } }
@Test public void byteAt() { Buffer buffer = new Buffer(); buffer.writeUtf8("a"); buffer.writeUtf8(repeat('b', SEGMENT_SIZE)); buffer.writeUtf8("c"); assertEquals('a', buffer.getByte(0)); assertEquals('a', buffer.getByte(0)); // getByte doesn't mutate! assertEquals('c', buffer.getByte(buffer.size() - 1)); assertEquals('b', buffer.getByte(buffer.size() - 2)); assertEquals('b', buffer.getByte(buffer.size() - 3)); }
if (skipWhitespaceAndCommas(header)) return; // Unexpected ','. String parameterValue = !header.exhausted() && header.getByte(0) == '"' ? readQuotedString(header) : readToken(header);
private void skipQuotedValue(ByteString runTerminator) throws IOException { while (true) { long index = source.indexOfElement(runTerminator); if (index == -1L) throw syntaxError("Unterminated string"); if (buffer.getByte(index) == '\\') { buffer.skip(index + 1); readEscapeCharacter(); } else { buffer.skip(index + 1); return; } } }
/** * 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. } }
switch (source.getBuffer().getByte(0)) { case '\r': case '\n':
byte c = buffer.getByte(i); result <<= 4; if (c >= '0' && c <= '9') {