private Buffer fileToBytes(File file) throws IOException { Buffer result = new Buffer(); result.writeAll(Okio.source(file)); return result; }
public Buffer readBuffer(File file) throws IOException { try (Source source = Okio.source(file)) { Buffer buffer = new Buffer(); buffer.writeAll(source); return buffer; } }
/** * Marks the current position in the stream as one to potentially return back to. Returns the * offset of this position. Call {@link #reset(long)} with this position to return to it later. It * is an error to call {@link #reset(long)} after consuming more than {@code readLimit} bytes from * {@linkplain #source() the source}. */ public long mark(long readLimit) throws IOException { if (readLimit < 0L) { throw new IllegalArgumentException("readLimit < 0: " + readLimit); } if (closed) { throw new IllegalStateException("closed"); } // Mark the current position in the buffered source. long userOffset = offset - userBuffer.size(); // If this is a new mark promote userBuffer data into the markBuffer. if (mark == -1L) { markBuffer.writeAll(userBuffer); mark = userOffset; offset = userOffset; } // Grow the limit if necessary. long newMarkBufferLimit = userOffset + readLimit; if (newMarkBufferLimit < 0) newMarkBufferLimit = Long.MAX_VALUE; // Long overflow! limit = Math.max(limit, newMarkBufferLimit); return userOffset; }
readBuffer.writeAll(receiveBuffer); if (wasEmpty) { Http2Stream.this.notifyAll();
private List<Integer> moveBytesBetweenBuffers(String... contents) throws IOException { StringBuilder expected = new StringBuilder(); Buffer buffer = new Buffer(); for (String s : contents) { Buffer source = new Buffer(); source.writeUtf8(s); buffer.writeAll(source); expected.append(s); } List<Integer> segmentSizes = segmentSizes(buffer); assertEquals(expected.toString(), buffer.readUtf8(expected.length())); return segmentSizes; }
readBuffer.writeAll(receiveBuffer); if (wasEmpty) { Http2Stream.this.notifyAll();
try (GzipSource gzippedResponseBody = new GzipSource(buffer.clone())) { buffer = new Buffer(); buffer.writeAll(gzippedResponseBody);
@Test public void writeAllMultipleSegments() throws Exception { Buffer source = new Buffer().writeUtf8(TestUtil.repeat('a', SEGMENT_SIZE * 3)); Buffer sink = new Buffer(); assertEquals(SEGMENT_SIZE * 3, sink.writeAll(source)); assertEquals(0, source.size()); assertEquals(TestUtil.repeat('a', SEGMENT_SIZE * 3), sink.readUtf8()); }
try (GzipSource gzippedResponseBody = new GzipSource(buffer.clone())) { buffer = new Buffer(); buffer.writeAll(gzippedResponseBody);
@Override public String readString(Charset charset) throws IOException { if (charset == null) throw new IllegalArgumentException("charset == null"); buffer.writeAll(source); return buffer.readString(charset); }
@Override public String readUtf8() throws IOException { buffer.writeAll(source); return buffer.readUtf8(); }
@Override public ByteString readByteString() throws IOException { buffer.writeAll(source); return buffer.readByteString(); }
@Override public byte[] readByteArray() throws IOException { buffer.writeAll(source); return buffer.readByteArray(); }
@Override public void readFully(Buffer sink, long byteCount) throws IOException { try { require(byteCount); } catch (EOFException e) { // The underlying source is exhausted. Copy the bytes we got before rethrowing. sink.writeAll(buffer); throw e; } buffer.readFully(sink, byteCount); }
/** * Reads a {@link ResponseBody} as a string. This is useful for logging error bodies. Does not * consume the {@code errorBody}. * <p>Warning: Error bodies can be very large because they can come from unexpected sources. * Only call this method if you are sure you can buffer the entire body into memory. */ public static String errorMessage(ResponseBody errorBody) throws IOException { if (errorBody.contentLength() == 0) { return ""; } Buffer buffer = new Buffer(); buffer.writeAll(errorBody.source().peek()); if (!isPlaintext(buffer)) { return "Error body is not plain text."; } return ResponseBody.create(errorBody.contentType(), buffer.size(), buffer).string(); }
@Override public MockResponse dispatch(RecordedRequest request) throws InterruptedException { okio.Buffer buffer = new okio.Buffer(); try { buffer.writeAll(Okio.source(new File("src/test/resources/test_response.mp3"))); } catch (IOException ioException) { throw new RuntimeException(ioException); } return new MockResponse().setBody(buffer); } });
@Override public MockResponse dispatch(RecordedRequest request) throws InterruptedException { okio.Buffer buffer = new okio.Buffer(); try { buffer.writeAll(Okio.source(new File("src/test/resources/2018-10-31T15_28_22.155Z.tar"))); } catch (IOException ioException) { throw new RuntimeException(ioException); } return new MockResponse().setBody(buffer); } });
readBuffer.writeAll(receiveBuffer); if (wasEmpty) { Http2Stream.this.notifyAll();
readBuffer.writeAll(receiveBuffer); if (wasEmpty) { FramedStream.this.notifyAll();
body.writeAll(stream.getSource()); body.close();