BufferedSource.peek()
See original GitHub issueWe’ve discussed an idea for a fancy peek API. Here’s an API sketch:
interface BufferedSource {
/**
* Returns a new `BufferedSource` that can read data from this `BufferedSource`
* without consuming it. The returned source becomes invalid once this source
* is next read or closed.
*
* For example, we can use `peek()` to lookahead and read the same data multiple
* times.
*
* ```
* val buffer = Buffer()
* buffer.writeUtf8("abcdefghij")
*
* buffer.readUtf8(3) // returns "abc", buffer contains "defghi"
*
* val peek = buffer.peek()
* peek.readUtf8(3) // returns "def", buffer contains "defghi"
* peek.readUtf8(3) // returns "ghi", buffer contains "defghi"
*
* buffer.readUtf8() // returns "def", buffer contains "ghi"
* ```
*/
BufferedSource peek()
}
Behind the scenes the peeking source would copy data from the upstream buffer into its own private buffer.
Issue Analytics
- State:
- Created 5 years ago
- Comments:11 (8 by maintainers)
Top Results From Across the Web
okio.BufferedSource.peek java code examples - Tabnine
Peeks up to {@code byteCount} bytes from the response body and returns them as a new response * body. If fewer than {@code...
Read more >BufferedSource (Okio 1.17.5 API) - javadoc.io
Returns an input stream that reads from this source. BufferedSource · peek(). Returns a new BufferedSource that can read data from this BufferedSource...
Read more >peek - Apollo GraphQL
Returns the type of the next token without consuming it. © 2022 CopyrightGenerated by dokka.
Read more >okio.BufferedSource Example - Program Talk
BufferedSource body1 = response1.body().source(); ... public ResponseBody peekBody( long byteCount) throws IOException {. BufferedSource source ...
Read more >android - How to fix: "Unresolved reference: buffer" or "Using ...
readUtf8() //issue is that line ... get error: "Using 'buffer(Source): BufferedSource' is an error. moved to ... val body = source.buffer().
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
One other thing I’m eager for – if we do this right we can also potentially support
JsonReader.peek()
that behaves the same way. That’s quite powerful for features like theRuntimeJsonAdapterFactory
.Gotcha. It won’t catch all programming mistakes. But it should still prevent data corruption because we’ll hault before we return unexpected data.