Reader(int headerTableSizeSetting, int maxDynamicTableByteCount, Source source) { this.headerTableSizeSetting = headerTableSizeSetting; this.maxDynamicTableByteCount = maxDynamicTableByteCount; this.source = Okio.buffer(source); }
@Override public BufferedSource source() { return Okio.buffer(new ForwardingSource(delegate.source()) { @Override public long read(Buffer sink, long byteCount) throws IOException { try { return super.read(sink, byteCount); } catch (IOException e) { thrownException = e; throw e; } } }); }
@Override public BufferedSource source() { if (bufferedSource == null) { bufferedSource = Okio.buffer(source(responseBody.source())); } return bufferedSource; }
private BufferedSink newJournalWriter() throws FileNotFoundException { Sink fileSink = fileSystem.appendingSink(journalFile); Sink faultHidingSink = new FaultHidingSink(fileSink) { @Override protected void onException(IOException e) { assert (Thread.holdsLock(DiskLruCache.this)); hasJournalErrors = true; } }; return Okio.buffer(faultHidingSink); }
CacheResponseBody(final DiskLruCache.Snapshot snapshot, String contentType, String contentLength) { this.snapshot = snapshot; this.contentType = contentType; this.contentLength = contentLength; Source source = snapshot.getSource(ENTRY_BODY); bodySource = Okio.buffer(new ForwardingSource(source) { @Override public void close() throws IOException { snapshot.close(); super.close(); } }); }
public BufferedSink createSink() { return Okio.buffer(pipe.sink()); }
public Builder socket(Socket socket) throws IOException { SocketAddress remoteSocketAddress = socket.getRemoteSocketAddress(); String connectionName = remoteSocketAddress instanceof InetSocketAddress ? ((InetSocketAddress) remoteSocketAddress).getHostName() : remoteSocketAddress.toString(); return socket(socket, connectionName, Okio.buffer(Okio.source(socket)), Okio.buffer(Okio.sink(socket))); }
@Override public boolean hasNext() { if (nextUrl != null) return true; canRemove = false; // Prevent delegate.remove() on the wrong item! while (delegate.hasNext()) { try (DiskLruCache.Snapshot snapshot = delegate.next()) { BufferedSource metadata = Okio.buffer(snapshot.getSource(ENTRY_METADATA)); nextUrl = metadata.readUtf8LineStrict(); return true; } catch (IOException ignored) { // We couldn't read the metadata for this snapshot; possibly because the host filesystem // has disappeared! Skip it. } } return false; }
@Override public OutputStream getBody() throws IOException { Sink body = okCacheRequest.body(); if (body == null) { return null; } return Okio.buffer(body).outputStream(); } };
StreamedRequestBody(long expectedContentLength) { initOutputStream(Okio.buffer(pipe.sink()), expectedContentLength); }
/** * Creates an OkHttp Response.Body containing the supplied information. */ private static ResponseBody createOkBody(final URLConnection urlConnection) throws IOException { if (!urlConnection.getDoInput()) { return null; } final BufferedSource body = Okio.buffer(Okio.source(urlConnection.getInputStream())); return new ResponseBody() { @Override public MediaType contentType() { String contentTypeHeader = urlConnection.getContentType(); return contentTypeHeader == null ? null : MediaType.parse(contentTypeHeader); } @Override public long contentLength() { String s = urlConnection.getHeaderField("Content-Length"); return stringToLong(s); } @Override public BufferedSource source() { return body; } }; }
/** * Respond to CONNECT requests until a SWITCH_TO_SSL_AT_END response is * dispatched. */ private void createTunnel() throws IOException, InterruptedException { BufferedSource source = Okio.buffer(Okio.source(raw)); BufferedSink sink = Okio.buffer(Okio.sink(raw)); while (true) { SocketPolicy socketPolicy = dispatcher.peek().getSocketPolicy(); if (!processOneRequest(raw, source, sink)) { throw new IllegalStateException("Tunnel without any CONNECT!"); } if (socketPolicy == UPGRADE_TO_SSL_AT_END) return; } }
/** * Creates an OkHttp Response.Body containing the supplied information. */ private static ResponseBody createOkBody(final Headers okHeaders, final CacheResponse cacheResponse) throws IOException { final BufferedSource body = Okio.buffer(Okio.source(cacheResponse.getBody())); return new ResponseBody() { @Override public MediaType contentType() { String contentTypeHeader = okHeaders.get("Content-Type"); return contentTypeHeader == null ? null : MediaType.parse(contentTypeHeader); } @Override public long contentLength() { return HttpHeaders.contentLength(okHeaders); } @Override public BufferedSource source() { return body; } }; }
@Override public void writeTo(BufferedSink sink) throws IOException { BufferedSink gzipSink = Okio.buffer(new GzipSink(sink)); body.writeTo(gzipSink); gzipSink.close(); } };
@Override public ResponseBody openResponseBody(Response response) throws IOException { streamAllocation.eventListener.responseBodyStart(streamAllocation.call); String contentType = response.header("Content-Type"); if (!HttpHeaders.hasBody(response)) { Source source = newFixedLengthSource(0); return new RealResponseBody(contentType, 0, Okio.buffer(source)); } if ("chunked".equalsIgnoreCase(response.header("Transfer-Encoding"))) { Source source = newChunkedSource(response.request().url()); return new RealResponseBody(contentType, -1L, Okio.buffer(source)); } long contentLength = HttpHeaders.contentLength(response); if (contentLength != -1) { Source source = newFixedLengthSource(contentLength); return new RealResponseBody(contentType, contentLength, Okio.buffer(source)); } return new RealResponseBody(contentType, -1L, Okio.buffer(newUnknownLengthSource())); }
private void readTheList() throws IOException { byte[] publicSuffixListBytes; byte[] publicSuffixExceptionListBytes; InputStream resource = PublicSuffixDatabase.class.getResourceAsStream(PUBLIC_SUFFIX_RESOURCE); if (resource == null) return; try (BufferedSource bufferedSource = Okio.buffer(new GzipSource(Okio.source(resource)))) { int totalBytes = bufferedSource.readInt(); publicSuffixListBytes = new byte[totalBytes]; bufferedSource.readFully(publicSuffixListBytes); int totalExceptionBytes = bufferedSource.readInt(); publicSuffixExceptionListBytes = new byte[totalExceptionBytes]; bufferedSource.readFully(publicSuffixExceptionListBytes); } synchronized (this) { this.publicSuffixListBytes = publicSuffixListBytes; this.publicSuffixExceptionListBytes = publicSuffixExceptionListBytes; } readCompleteLatch.countDown(); }
public void run() { synchronized (DiskLruCache.this) { if (!initialized | closed) { return; // Nothing to do } try { trimToSize(); } catch (IOException ignored) { mostRecentTrimFailed = true; } try { if (journalRebuildRequired()) { rebuildJournal(); redundantOpCount = 0; } } catch (IOException e) { mostRecentRebuildFailed = true; journalWriter = Okio.buffer(Okio.blackhole()); } } } };
@Override public ResponseBody openResponseBody(Response response) throws IOException { streamAllocation.eventListener.responseBodyStart(streamAllocation.call); String contentType = response.header("Content-Type"); long contentLength = HttpHeaders.contentLength(response); Source source = new StreamFinishingSource(stream.getSource()); return new RealResponseBody(contentType, contentLength, Okio.buffer(source)); }
@Test public void closeWithExceptionWhenClosing() throws IOException { MockSink mockSink = new MockSink(); mockSink.scheduleThrow(1, new IOException()); BufferedSink bufferedSink = Okio.buffer(mockSink); bufferedSink.writeByte('a'); try { bufferedSink.close(); fail(); } catch (IOException expected) { } mockSink.assertLog("write([text=a], 1)", "close()"); }
@Test public void bufferedSinkFlush() throws IOException { Buffer sink = new Buffer(); BufferedSink bufferedSink = Okio.buffer((Sink) sink); bufferedSink.writeByte('a'); assertEquals(0, sink.size()); bufferedSink.flush(); assertEquals(0, bufferedSink.getBuffer().size()); assertEquals(1, sink.size()); }