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.

InterceptingSink and InterceptingSource

See original GitHub issue

There seems to be a common pattern used in both GzipSink and HashingSink and another used in GzipSource and HashingSource that gives raw access to the segment bytes to checksum or hashing algorithms. I needed to use a similar pattern to compute a 2’s complement checksum for an IPMI sink but had to create this IpmiChecksumSink in the okio package because of package-private access to the segments. I then created an abstract InterceptingSink which applied the common pattern and had a single abstract method which gave unsafe access to the segment data.

public abstract class InterceptingSink extends ForwardingSink {

  protected InterceptingSink(Sink sink) {
    super(sink);
  }

  @Override public void write(Buffer source, long byteCount) throws IOException {
    checkOffsetAndCount(source.size, 0, byteCount);

    long remaining = byteCount;
    for (Segment head = source.head; remaining > 0; head = head.next) {
      int segmentLength = (int) Math.min(remaining, head.limit - head.pos);
      intercept(head.data, head.pos, segmentLength);
      remaining -= segmentLength;
    }

    // Write those bytes to the sink.
    super.write(source, byteCount);
  }

  protected abstract void intercept(byte[] data, int pos, int len) throws IOException;
}

Is this idea of an InterceptingSink or InterceptingSource something you think would be worth adding to Okio?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
bnormcommented, Jan 5, 2018

I’m all for getting the exposed API correct and not above doing a little proof of concept work. I really appreciate the succinct API Okio exposes and know that only comes with a lot of diligence.

I’m up for working on another concept if you want to provide more details on some of your ideas. For example, this hypothetical UnsafeBuffer, are you thinking that it would be a separate kind of buffer, or that it would extend Buffer? What is the idea behind the release method call?

0reactions
swankjessecommented, Jan 31, 2018

Yeah, definitely a sample. And we still need the enlargeBuffer API.

Read more comments on GitHub >

github_iconTop Results From Across the Web

okio.Buffer$UnsafeCursor.next java code examples | Tabnine
Interceptors$InterceptingSink.write(...) @Override public void write(Buffer source, long byteCount) throws IOException { if (byteCount < 0) throw new ...
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