@Override public RequestBody convert(T value) throws IOException { Buffer buffer = new Buffer(); adapter.encode(buffer, value); return RequestBody.create(MEDIA_TYPE, buffer.snapshot()); } }
@Override public void writeTo(BufferedSink sink) throws IOException { sink.write(buffer.snapshot()); } };
@Override public ByteString encodeUtf8(String s) { Buffer buffer = new Buffer(); buffer.writeUtf8(s); return buffer.snapshot(); } };
@Override public ByteString decodeHex(String hex) { Buffer buffer = new Buffer(); buffer.write(ByteString.decodeHex(hex)); return buffer.snapshot(); }
@Test public void snapshotReportsAccurateSize() { Buffer buf = new Buffer().write(new byte[] { 0, 1, 2, 3 }); assertEquals(1, buf.snapshot(1).size()); } }
@Test public void accessByteByByteReverse() throws Exception { Buffer buffer = bufferFactory.newBuffer(); try (UnsafeCursor cursor = buffer.readUnsafe()) { byte[] actual = new byte[(int) buffer.size()]; for (int i = (int) (buffer.size() - 1); i >= 0; i--) { cursor.seek(i); actual[i] = cursor.data[cursor.start]; } assertEquals(ByteString.of(actual), buffer.snapshot()); } }
@Test public void accessByteByByte() throws Exception { Buffer buffer = bufferFactory.newBuffer(); try (UnsafeCursor cursor = buffer.readUnsafe()) { byte[] actual = new byte[(int) buffer.size()]; for (int i = 0; i < buffer.size(); i++) { cursor.seek(i); actual[i] = cursor.data[cursor.start]; } assertEquals(ByteString.of(actual), buffer.snapshot()); } }
@Test public void accessByteByByteAlwaysResettingToZero() throws Exception { Buffer buffer = bufferFactory.newBuffer(); try (UnsafeCursor cursor = buffer.readUnsafe()) { byte[] actual = new byte[(int) buffer.size()]; for (int i = 0; i < buffer.size(); i++) { cursor.seek(i); actual[i] = cursor.data[cursor.start]; cursor.seek(0L); } assertEquals(ByteString.of(actual), buffer.snapshot()); } }
@Override public void writeTo(BufferedSink sink) throws IOException { sink.write(buffer.snapshot()); } };
@Override public void writeTo(BufferedSink sink) throws IOException { sink.write(buffer.snapshot()); } };
@Override public void writeTo(BufferedSink sink) throws IOException { sink.write(buffer.snapshot()); } };
@Override public void writeTo(BufferedSink sink) throws IOException { sink.write(buffer.snapshot()); } };
@Override public void writeTo(BufferedSink sink) throws IOException { sink.write(buffer.snapshot()); } };
@Override public void writeTo(BufferedSink sink) throws IOException { sink.write(buffer.snapshot()); } };
@Override public void writeTo(BufferedSink sink) throws IOException { sink.write(buffer.snapshot()); } };
@Override public void writeTo(BufferedSink sink) throws IOException { sink.write(buffer.snapshot()); } };
@Override public void writeTo(BufferedSink sink) throws IOException { sink.write(buffer.snapshot()); } };
@Override public void writeTo(BufferedSink sink) throws IOException { sink.write(buffer.snapshot()); } };
@Override public void writeTo(BufferedSink sink) throws IOException { sink.write(buffer.snapshot()); } };
/** * Checks for DOCTYPE beginning {@code <!DOCTYPE }. This method doesn't consume the opening <!DOCTYPE * Tag * * @return true, if DOCTYPE opening tag, otherwise false * @throws IOException */ private boolean isDocTypeDefinition() throws IOException { return buffer.size() >= DOCTYPE_OPEN.size() && buffer.snapshot(DOCTYPE_OPEN.size()).toAsciiUppercase().equals(DOCTYPE_OPEN); }