WebSocketWriter(boolean isClient, BufferedSink sink, Random random) { if (sink == null) throw new NullPointerException("sink == null"); if (random == null) throw new NullPointerException("random == null"); this.isClient = isClient; this.sink = sink; this.sinkBuffer = sink.buffer(); this.random = random; // Masks are only a concern for client writers. maskKey = isClient ? new byte[4] : null; maskCursor = isClient ? new Buffer.UnsafeCursor() : null; }
@Override public void writeTo(BufferedSink sink) throws IOException { buffer.copyTo(sink.buffer(), 0, buffer.size()); } }
WebSocketWriter(boolean isClient, BufferedSink sink, Random random) { if (sink == null) throw new NullPointerException("sink == null"); if (random == null) throw new NullPointerException("random == null"); this.isClient = isClient; this.sink = sink; this.sinkBuffer = sink.buffer(); this.random = random; // Masks are only a concern for client writers. maskKey = isClient ? new byte[4] : null; maskCursor = isClient ? new Buffer.UnsafeCursor() : null; }
/** * Either writes this request to {@code sink} or measures its content length. We have one method * do double-duty to make sure the counting and content are consistent, particularly when it comes * to awkward operations like measuring the encoded length of header strings, or the * length-in-digits of an encoded integer. */ private long writeOrCountBytes(@Nullable BufferedSink sink, boolean countBytes) { long byteCount = 0L; Buffer buffer; if (countBytes) { buffer = new Buffer(); } else { buffer = sink.buffer(); } for (int i = 0, size = encodedNames.size(); i < size; i++) { if (i > 0) buffer.writeByte('&'); buffer.writeUtf8(encodedNames.get(i)); buffer.writeByte('='); buffer.writeUtf8(encodedValues.get(i)); } if (countBytes) { byteCount = buffer.size(); buffer.clear(); } return byteCount; }
static void copyResponseBody(Response response, Sink sink) throws IOException { final int bufferSize = 8 * 1024; BufferedSource responseBodySource = response.body().source(); BufferedSink cacheResponseBody = Okio.buffer(sink); while (responseBodySource.read(cacheResponseBody.buffer(), bufferSize) > 0) { cacheResponseBody.emit(); } closeQuietly(responseBodySource); }
long read; while ((read = source.read(sink.buffer(), SEGMENT_SIZE)) != -1) { total += read; sink.flush();
/** * Either writes this request to {@code sink} or measures its content length. We have one method * do double-duty to make sure the counting and content are consistent, particularly when it comes * to awkward operations like measuring the encoded length of header strings, or the * length-in-digits of an encoded integer. */ private long writeOrCountBytes(@Nullable BufferedSink sink, boolean countBytes) { long byteCount = 0L; Buffer buffer; if (countBytes) { buffer = new Buffer(); } else { buffer = sink.buffer(); } for (int i = 0, size = encodedNames.size(); i < size; i++) { if (i > 0) buffer.writeByte('&'); buffer.writeUtf8(encodedNames.get(i)); buffer.writeByte('='); buffer.writeUtf8(encodedValues.get(i)); } if (countBytes) { byteCount = buffer.size(); buffer.clear(); } return byteCount; }
void copyFrom(Buffer buffer, long offset, long bytesCount) { if (failed) return; try { BufferedSink outSink = (BufferedSink) delegate(); buffer.copyTo(outSink.buffer(), offset, bytesCount); outSink.emitCompleteSegments(); } catch (Exception e) { failed = true; onException(e); } }
@Override public long read(Buffer sink, long byteCount) throws IOException { long bytesRead; try { bytesRead = source.read(sink, byteCount); } catch (IOException e) { if (!cacheRequestClosed) { cacheRequestClosed = true; cacheRequest.abort(); // Failed to write a complete cache response. } throw e; } if (bytesRead == -1) { if (!cacheRequestClosed) { cacheRequestClosed = true; cacheBody.close(); // The cache response is complete! } return -1; } sink.copyTo(cacheBody.buffer(), sink.size() - bytesRead, bytesRead); cacheBody.emitCompleteSegments(); return bytesRead; }
if (!source.getBuffer().exhausted() || !sink.buffer().exhausted()) { throw new IOException("TLS tunnel buffered too many bytes!");
@Override public long read(Buffer sink, long byteCount) throws IOException { long bytesRead; try { bytesRead = source.read(sink, byteCount); } catch (IOException e) { if (!cacheRequestClosed) { cacheRequestClosed = true; cacheRequest.abort(); // Failed to write a complete cache response. } throw e; } if (bytesRead == -1) { if (!cacheRequestClosed) { cacheRequestClosed = true; cacheBody.close(); // The cache response is complete! } return -1; } sink.copyTo(cacheBody.buffer(), sink.size() - bytesRead, bytesRead); cacheBody.emitCompleteSegments(); return bytesRead; }
if (!source.getBuffer().exhausted() || !sink.buffer().exhausted()) { throw new IOException("TLS tunnel buffered too many bytes!");
WebSocketWriter(boolean isClient, BufferedSink sink, Random random) { if (sink == null) throw new NullPointerException("sink == null"); if (random == null) throw new NullPointerException("random == null"); this.isClient = isClient; this.sink = sink; this.sinkBuffer = sink.buffer(); this.random = random; // Masks are only a concern for client writers. maskKey = isClient ? new byte[4] : null; maskCursor = isClient ? new Buffer.UnsafeCursor() : null; }
@Override public void writeTo(BufferedSink sink) throws IOException { buffer.copyTo(sink.buffer(), 0, buffer.size()); } };
@Override public void writeTo(BufferedSink sink) throws IOException { buffer.copyTo(sink.buffer(), 0, buffer.size()); } }
@Override public void writeTo(BufferedSink sink) throws IOException { buffer.copyTo(sink.buffer(), 0, buffer.size()); } }
@Override public void writeTo(BufferedSink sink) throws IOException { buffer.copyTo(sink.buffer(), 0, buffer.size()); } }
public static Request interceptRequest(@NotNull Request request, @NotNull String parameter) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Sink sink = Okio.sink(baos); BufferedSink bufferedSink = Okio.buffer(sink); /** * Write old params * */ request.body().writeTo(bufferedSink); /** * write to buffer additional params * */ bufferedSink.writeString(parameter, Charset.defaultCharset()); RequestBody newRequestBody = RequestBody.create( request.body().contentType(), bufferedSink.buffer().readUtf8() ); return request.newBuilder().post(newRequestBody).build(); }
private void writeHeader() { // Write the Gzip header directly into the buffer for the sink to avoid handling IOException. Buffer buffer = this.sink.buffer(); buffer.writeShort(0x1f8b); // Two-byte Gzip ID. buffer.writeByte(0x08); // 8 == Deflate compression method. buffer.writeByte(0x00); // No flags. buffer.writeInt(0x00); // No modification time. buffer.writeByte(0x00); // No extra flags. buffer.writeByte(0x00); // No OS. }
@Override public long read(Buffer sink, long byteCount) throws IOException { long bytesRead; try { bytesRead = source.read(sink, byteCount); } catch (IOException e) { if (!cacheRequestClosed) { cacheRequestClosed = true; cacheRequest.abort(); // Failed to write a complete cache response. } throw e; } if (bytesRead == -1) { if (!cacheRequestClosed) { cacheRequestClosed = true; cacheBody.close(); // The cache response is complete! } return -1; } sink.copyTo(cacheBody.buffer(), sink.size() - bytesRead, bytesRead); cacheBody.emitCompleteSegments(); return bytesRead; }