InterceptingSink and InterceptingSource
See original GitHub issueThere 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:
- Created 6 years ago
- Reactions:1
- Comments:12 (7 by maintainers)
Top 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 >
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
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?
Yeah, definitely a sample. And we still need the enlargeBuffer API.