@Override public void onResponse(Call call, Response response) throws IOException { try (ResponseBody body = response.body()) { // Consume and discard the response body. body.source().readByteString(); } } });
@Override public void onResponse(Call call, Response response) throws IOException { try (ResponseBody body = response.body()) { // Consume and discard the response body. body.source().readByteString(); } } });
public void run() throws Exception { Request request = new Request.Builder() .url("https://publicobject.com/helloworld.txt") .build(); System.out.println("REQUEST 1 (new connection)"); try (Response response = client.newCall(request).execute()) { // Consume and discard the response body. response.body().source().readByteString(); } System.out.println("REQUEST 2 (pooled connection)"); try (Response response = client.newCall(request).execute()) { // Consume and discard the response body. response.body().source().readByteString(); } }
public ByteString readByteString(File file) throws IOException { try (BufferedSource source = Okio.buffer(Okio.source(file))) { return source.readByteString(); } }
private static ByteString decodeQ(String encodedWord) { /* * Replace _ with =20 */ StringBuilder sb = new StringBuilder(); for (int i = 0; i < encodedWord.length(); i++) { char c = encodedWord.charAt(i); if (c == '_') { sb.append("=20"); } else { sb.append(c); } } byte[] bytes = sb.toString().getBytes(Charset.forName("US-ASCII")); QuotedPrintableInputStream is = new QuotedPrintableInputStream(new ByteArrayInputStream(bytes)); try { return Okio.buffer(Okio.source(is)).readByteString(); } catch (IOException e) { return null; } }
private List<InetAddress> readResponse(String hostname, Response response) throws Exception { if (response.cacheResponse() == null && response.protocol() != Protocol.HTTP_2) { Platform.get().log(Platform.WARN, "Incorrect protocol: " + response.protocol(), null); } try { if (!response.isSuccessful()) { throw new IOException("response: " + response.code() + " " + response.message()); } ResponseBody body = response.body(); if (body.contentLength() > MAX_RESPONSE_SIZE) { throw new IOException("response size exceeds limit (" + MAX_RESPONSE_SIZE + " bytes): " + body.contentLength() + " bytes"); } ByteString responseBytes = body.source().readByteString(); return DnsRecordCodec.decodeAnswers(hostname, responseBytes); } finally { response.close(); } }
/** * Reads a {@code bytes} field value from the stream. The length is read from the * stream prior to the actual data. */ public ByteString readBytes() throws IOException { long byteCount = beforeLengthDelimitedScalar(); source.require(byteCount); // Throws EOFException if insufficient bytes are available. return source.readByteString(byteCount); }
public void readConnectionPreface(Handler handler) throws IOException { if (client) { // The client reads the initial SETTINGS frame. if (!nextFrame(true, handler)) { throw ioException("Required SETTINGS preface not received"); } } else { // The server reads the CONNECTION_PREFACE byte string. ByteString connectionPreface = source.readByteString(CONNECTION_PREFACE.size()); if (logger.isLoggable(FINE)) logger.fine(format("<< CONNECTION %s", connectionPreface.hex())); if (!CONNECTION_PREFACE.equals(connectionPreface)) { throw ioException("Expected a connection header but was %s", connectionPreface.utf8()); } } }
/** Reads a potentially Huffman encoded byte string. */ ByteString readByteString() throws IOException { int firstByte = readByte(); boolean huffmanDecode = (firstByte & 0x80) == 0x80; // 1NNNNNNN int length = readInt(firstByte, PREFIX_7_BITS); if (huffmanDecode) { return ByteString.of(Huffman.get().decode(source.readByteArray(length))); } else { return source.readByteString(length); } } }
private void readGoAway(Handler handler, int length, byte flags, int streamId) throws IOException { if (length < 8) throw ioException("TYPE_GOAWAY length < 8: %s", length); if (streamId != 0) throw ioException("TYPE_GOAWAY streamId != 0"); int lastStreamId = source.readInt(); int errorCodeInt = source.readInt(); int opaqueDataLength = length - 8; ErrorCode errorCode = ErrorCode.fromHttp2(errorCodeInt); if (errorCode == null) { throw ioException("TYPE_GOAWAY unexpected error code: %d", errorCodeInt); } ByteString debugData = EMPTY; if (opaqueDataLength > 0) { // Must read debug data in order to not corrupt the connection. debugData = source.readByteString(opaqueDataLength); } handler.goAway(lastStreamId, errorCode, debugData); }
public void readConnectionPreface(Handler handler) throws IOException { if (client) { // The client reads the initial SETTINGS frame. if (!nextFrame(true, handler)) { throw ioException("Required SETTINGS preface not received"); } } else { // The server reads the CONNECTION_PREFACE byte string. ByteString connectionPreface = source.readByteString(CONNECTION_PREFACE.size()); if (logger.isLoggable(FINE)) logger.fine(format("<< CONNECTION %s", connectionPreface.hex())); if (!CONNECTION_PREFACE.equals(connectionPreface)) { throw ioException("Expected a connection header but was %s", connectionPreface.utf8()); } } }
/** Reads a potentially Huffman encoded byte string. */ ByteString readByteString() throws IOException { int firstByte = readByte(); boolean huffmanDecode = (firstByte & 0x80) == 0x80; // 1NNNNNNN int length = readInt(firstByte, PREFIX_7_BITS); if (huffmanDecode) { return ByteString.of(Huffman.get().decode(source.readByteArray(length))); } else { return source.readByteString(length); } } }
@Test public void multipleSegments() throws Exception { HashingSource hashingSource = HashingSource.sha256(source); BufferedSource bufferedSource = Okio.buffer(hashingSource); source.write(r32k); assertEquals(r32k, bufferedSource.readByteString()); assertEquals(SHA256_r32k, hashingSource.hash()); }
private void readGoAway(Handler handler, int length, byte flags, int streamId) throws IOException { if (length < 8) throw ioException("TYPE_GOAWAY length < 8: %s", length); if (streamId != 0) throw ioException("TYPE_GOAWAY streamId != 0"); int lastStreamId = source.readInt(); int errorCodeInt = source.readInt(); int opaqueDataLength = length - 8; ErrorCode errorCode = ErrorCode.fromHttp2(errorCodeInt); if (errorCode == null) { throw ioException("TYPE_GOAWAY unexpected error code: %d", errorCodeInt); } ByteString debugData = EMPTY; if (opaqueDataLength > 0) { // Must read debug data in order to not corrupt the connection. debugData = source.readByteString(opaqueDataLength); } handler.goAway(lastStreamId, errorCode, debugData); }
@Test public void readByteStringTooShortThrows() throws IOException { sink.writeUtf8("abc"); sink.emit(); try { source.readByteString(4); fail(); } catch (EOFException expected) { } assertEquals("abc", source.readUtf8()); // The read shouldn't consume any data. }
@Test public void removingElementZeroesData() throws IOException { QueueFile queueFile = newQueueFile(true); queueFile.add(values[4]); queueFile.remove(); queueFile.close(); BufferedSource source = Okio.buffer(Okio.source(file)); source.skip(headerLength); source.skip(Element.HEADER_LENGTH); assertThat(source.readByteString(4).hex()).isEqualTo("00000000"); }
@Test public void removingElementDoesNotZeroData() throws IOException { QueueFile queueFile = newQueueFile(false); queueFile.add(values[4]); queueFile.remove(); queueFile.close(); BufferedSource source = Okio.buffer(Okio.source(file)); source.skip(headerLength); source.skip(Element.HEADER_LENGTH); assertThat(source.readByteString(4).hex()).isEqualTo("04030201"); source.close(); }
bufferedSource.readByteString(10); fail(); } catch (IllegalStateException expected) {
@Test public void readByteString() throws IOException { sink.writeUtf8("abcd").writeUtf8(repeat('e', SEGMENT_SIZE)); sink.emit(); assertEquals("abcd" + repeat('e', SEGMENT_SIZE), source.readByteString().utf8()); }
@Test public void readByteStringPartial() throws IOException { sink.writeUtf8("abcd").writeUtf8(repeat('e', SEGMENT_SIZE)); sink.emit(); assertEquals("abc", source.readByteString(3).utf8()); assertEquals("d", source.readUtf8(1)); }