static boolean isWebPFile(BufferedSource source) throws IOException { return source.rangeEquals(0, WEBP_FILE_HEADER_RIFF) && source.rangeEquals(8, WEBP_FILE_HEADER_WEBP); }
/** * 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; }
@Override public T convert(ResponseBody value) throws IOException { BufferedSource source = value.source(); try { // Moshi has no document-level API so the responsibility of BOM skipping falls to whatever // is delegating to it. Since it's a UTF-8-only library as well we only honor the UTF-8 BOM. if (source.rangeEquals(0, UTF8_BOM)) { source.skip(UTF8_BOM.size()); } JsonReader reader = JsonReader.of(source); T result = adapter.fromJson(reader); if (reader.peek() != JsonReader.Token.END_DOCUMENT) { throw new JsonDataException("JSON document was not fully consumed."); } return result; } finally { value.close(); } } }
@Test public void rangeEqualsArgumentValidation() throws IOException { // Negative source offset. assertFalse(source.rangeEquals(-1, ByteString.encodeUtf8("A"))); // Negative bytes offset. assertFalse(source.rangeEquals(0, ByteString.encodeUtf8("A"), -1, 1)); // Bytes offset longer than bytes length. assertFalse(source.rangeEquals(0, ByteString.encodeUtf8("A"), 2, 1)); // Negative byte count. assertFalse(source.rangeEquals(0, ByteString.encodeUtf8("A"), 0, -1)); // Byte count longer than bytes length. assertFalse(source.rangeEquals(0, ByteString.encodeUtf8("A"), 0, 2)); // Bytes offset plus byte count longer than bytes length. assertFalse(source.rangeEquals(0, ByteString.encodeUtf8("A"), 1, 1)); }
@Test public void rangeEquals() throws IOException { sink.writeUtf8("A man, a plan, a canal. Panama."); sink.emit(); assertTrue(source.rangeEquals(7 , ByteString.encodeUtf8("a plan"))); assertTrue(source.rangeEquals(0 , ByteString.encodeUtf8("A man"))); assertTrue(source.rangeEquals(24, ByteString.encodeUtf8("Panama"))); assertFalse(source.rangeEquals(24, ByteString.encodeUtf8("Panama. Panama. Panama."))); }
@Test public void rangeEqualsWithOffsetAndCount() throws IOException { sink.writeUtf8("A man, a plan, a canal. Panama."); sink.emit(); assertTrue(source.rangeEquals(7 , ByteString.encodeUtf8("aaa plannn"), 2, 6)); assertTrue(source.rangeEquals(0 , ByteString.encodeUtf8("AAA mannn"), 2, 5)); assertTrue(source.rangeEquals(24, ByteString.encodeUtf8("PPPanamaaa"), 2, 6)); }
@Test public void rangeEqualsOnlyReadsUntilMismatch() throws IOException { assumeTrue(factory == Factory.ONE_BYTE_AT_A_TIME_BUFFERED_SOURCE); // Other sources read in chunks anyway. sink.writeUtf8("A man, a plan, a canal. Panama."); sink.emit(); assertFalse(source.rangeEquals(0, ByteString.encodeUtf8("A man."))); assertEquals("A man,", source.getBuffer().readUtf8()); }
public static Charset bomAwareCharset(BufferedSource source, Charset charset) throws IOException { if (source.rangeEquals(0, UTF_8_BOM)) { source.skip(UTF_8_BOM.size()); return UTF_8; } if (source.rangeEquals(0, UTF_16_BE_BOM)) { source.skip(UTF_16_BE_BOM.size()); return UTF_16_BE; } if (source.rangeEquals(0, UTF_16_LE_BOM)) { source.skip(UTF_16_LE_BOM.size()); return UTF_16_LE; } if (source.rangeEquals(0, UTF_32_BE_BOM)) { source.skip(UTF_32_BE_BOM.size()); return UTF_32_BE; } if (source.rangeEquals(0, UTF_32_LE_BOM)) { source.skip(UTF_32_LE_BOM.size()); return UTF_32_LE; } return charset; }
/** * 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; }
public static Charset bomAwareCharset(BufferedSource source, Charset charset) throws IOException { if (source.rangeEquals(0, UTF_8_BOM)) { source.skip(UTF_8_BOM.size()); return UTF_8; } if (source.rangeEquals(0, UTF_16_BE_BOM)) { source.skip(UTF_16_BE_BOM.size()); return UTF_16_BE; } if (source.rangeEquals(0, UTF_16_LE_BOM)) { source.skip(UTF_16_LE_BOM.size()); return UTF_16_LE; } if (source.rangeEquals(0, UTF_32_BE_BOM)) { source.skip(UTF_32_BE_BOM.size()); return UTF_32_BE; } if (source.rangeEquals(0, UTF_32_LE_BOM)) { source.skip(UTF_32_LE_BOM.size()); return UTF_32_LE; } return charset; }
@Override public T convert(ResponseBody value) throws IOException { BufferedSource source = value.source(); try { // Moshi has no document-level API so the responsibility of BOM skipping falls to whatever // is delegating to it. Since it's a UTF-8-only library as well we only honor the UTF-8 BOM. if (source.rangeEquals(0, UTF8_BOM)) { source.skip(UTF8_BOM.size()); } JsonReader reader = JsonReader.of(source); T result = adapter.fromJson(reader); if (reader.peek() != JsonReader.Token.END_DOCUMENT) { throw new JsonDataException("JSON document was not fully consumed."); } return result; } finally { value.close(); } } }
if (isDocumentBeginning && source.rangeEquals(0, UTF8_BOM)) { source.skip(3);