/** * Peeks up to {@code byteCount} bytes from the response body and returns them as a new response * body. If fewer than {@code byteCount} bytes are in the response body, the full response body is * returned. If more than {@code byteCount} bytes are in the response body, the returned value * will be truncated to {@code byteCount} bytes. * * <p>It is an error to call this method after the body has been consumed. * * <p><strong>Warning:</strong> this method loads the requested bytes into memory. Most * applications should set a modest limit on {@code byteCount}, such as 1 MiB. */ public ResponseBody peekBody(long byteCount) throws IOException { BufferedSource peeked = body.source().peek(); Buffer buffer = new Buffer(); peeked.request(byteCount); buffer.write(peeked, Math.min(byteCount, peeked.getBuffer().size())); return ResponseBody.create(body.contentType(), buffer.size(), buffer); }
@Override public Pipe pipe() { Buffer buffer = new Buffer(); Pipe result = new Pipe(); result.sink = buffer; result.source = Okio.buffer((Source) buffer).peek(); return result; }
/** Copy-constructor makes a deep copy for peeking. */ JsonUtf8Reader(JsonUtf8Reader copyFrom) { super(copyFrom); BufferedSource sourcePeek = copyFrom.source.peek(); this.source = sourcePeek; this.buffer = sourcePeek.getBuffer(); this.peeked = copyFrom.peeked; this.peekedLong = copyFrom.peekedLong; this.peekedNumberLength = copyFrom.peekedNumberLength; this.peekedString = copyFrom.peekedString; // Make sure our buffer has as many bytes as the source's buffer. This is necessary because // JsonUtf8Reader assumes any data it has peeked (like the peekedNumberLength) are buffered. try { sourcePeek.require(copyFrom.buffer.size()); } catch (IOException e) { throw new AssertionError(); } }
/** * Peeks up to {@code byteCount} bytes from the response body and returns them as a new response * body. If fewer than {@code byteCount} bytes are in the response body, the full response body is * returned. If more than {@code byteCount} bytes are in the response body, the returned value * will be truncated to {@code byteCount} bytes. * * <p>It is an error to call this method after the body has been consumed. * * <p><strong>Warning:</strong> this method loads the requested bytes into memory. Most * applications should set a modest limit on {@code byteCount}, such as 1 MiB. */ public ResponseBody peekBody(long byteCount) throws IOException { BufferedSource peeked = body.source().peek(); Buffer buffer = new Buffer(); peeked.request(byteCount); buffer.write(peeked, Math.min(byteCount, peeked.getBuffer().size())); return ResponseBody.create(body.contentType(), buffer.size(), buffer); }
} else { if (calculateSize) { BitmapFactory.decodeStream(bufferedSource.peek().inputStream(), null, options); calculateInSampleSize(request.targetWidth, request.targetHeight, checkNotNull(options, "options == null"), request);
@Test public void peekMultiple() throws IOException { sink.writeUtf8("abcdefghi"); sink.emit(); assertEquals("abc", source.readUtf8(3)); BufferedSource peek1 = source.peek(); BufferedSource peek2 = source.peek(); assertEquals("def", peek1.readUtf8(3)); assertEquals("def", peek2.readUtf8(3)); assertEquals("ghi", peek2.readUtf8(3)); assertFalse(peek2.request(1)); assertEquals("ghi", peek1.readUtf8(3)); assertFalse(peek1.request(1)); assertEquals("def", source.readUtf8(3)); }
@Test public void peekInvalid() throws IOException { sink.writeUtf8("abcdefghi"); sink.emit(); assertEquals("abc", source.readUtf8(3)); BufferedSource peek = source.peek(); assertEquals("def", peek.readUtf8(3)); assertEquals("ghi", peek.readUtf8(3)); assertFalse(peek.request(1)); assertEquals("def", source.readUtf8(3)); try { peek.readUtf8(); fail(); } catch (IllegalStateException e) { assertEquals("Peek source is invalid because upstream source was used", e.getMessage()); } }
@Test public void peekSegmentThenInvalid() throws IOException { sink.writeUtf8("abc"); sink.writeUtf8(repeat('d', 2 * SEGMENT_SIZE)); sink.emit(); assertEquals("abc", source.readUtf8(3)); // Peek a little data and skip the rest of the upstream source BufferedSource peek = source.peek(); assertEquals("ddd", peek.readUtf8(3)); source.readAll(Okio.blackhole()); // Skip the rest of the buffered data peek.skip(SEGMENT_SIZE - 3); try { peek.readByte(); fail(); } catch (IllegalStateException e) { assertEquals("Peek source is invalid because upstream source was used", e.getMessage()); } }
@Test public void peek() throws IOException { sink.writeUtf8("abcdefghi"); sink.emit(); assertEquals("abc", source.readUtf8(3)); BufferedSource peek = source.peek(); assertEquals("def", peek.readUtf8(3)); assertEquals("ghi", peek.readUtf8(3)); assertFalse(peek.request(1)); assertEquals("def", source.readUtf8(3)); }
@Test public void peekLarge() throws IOException { sink.writeUtf8("abcdef"); sink.writeUtf8(repeat('g', 2 * SEGMENT_SIZE)); sink.writeUtf8("hij"); sink.emit(); assertEquals("abc", source.readUtf8(3)); BufferedSource peek = source.peek(); assertEquals("def", peek.readUtf8(3)); peek.skip(2 * SEGMENT_SIZE); assertEquals("hij", peek.readUtf8(3)); assertFalse(peek.request(1)); assertEquals("def", source.readUtf8(3)); source.skip(2 * SEGMENT_SIZE); assertEquals("hij", source.readUtf8(3)); }
/** Copy-constructor makes a deep copy for peeking. */ JsonUtf8Reader(JsonUtf8Reader copyFrom) { super(copyFrom); BufferedSource sourcePeek = copyFrom.source.peek(); this.source = sourcePeek; this.buffer = sourcePeek.getBuffer(); this.peeked = copyFrom.peeked; this.peekedLong = copyFrom.peekedLong; this.peekedNumberLength = copyFrom.peekedNumberLength; this.peekedString = copyFrom.peekedString; // Make sure our buffer has as many bytes as the source's buffer. This is necessary because // JsonUtf8Reader assumes any data it has peeked (like the peekedNumberLength) are buffered. try { sourcePeek.require(copyFrom.buffer.size()); } catch (IOException e) { throw new AssertionError(); } }
/** * 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(); }