question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BufferedSource.peek()

See original GitHub issue

We’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:closed
  • Created 5 years ago
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
swankjessecommented, Sep 10, 2018

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 the RuntimeJsonAdapterFactory.

0reactions
swankjessecommented, Sep 13, 2018

Gotcha. It won’t catch all programming mistakes. But it should still prevent data corruption because we’ll hault before we return unexpected data.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found