Make FixedLengthSource public API#1823
Conversation
| @JvmOverloads | ||
| fun Source.limit( | ||
| byteCount: Long, | ||
| throwIfSourceIsLonger: Boolean = false, |
There was a problem hiding this comment.
This is sufficient for OkHttp's needs? I thought it needed to know if you exhausted the limit or not.
There was a problem hiding this comment.
I looked into OkHttp's two best candidates...
HTTP/1 framing could use this, but it's overall worse because the framing also does other things like skip the remainder if closed prematurely.
DNS messages are a good candidate and can use the simplest limit API.
| // If we received bytes beyond the limit, don't return them to the caller. | ||
| sink.truncateToSize(sink.size - beyondLimitByteCount) |
There was a problem hiding this comment.
This would only happen from a misbehaving source, right?
There was a problem hiding this comment.
Kinda sorta
In Okio zip file handling we expect a zip file's decompressed size to be consistent with the metadata's promised decompressed size. They could be inconsistent if the zip writer was buggy or malicious.
In such cases we'll detect that the actual size was larger than the expected size and throw.
But we won't return the excess data to the caller.
| return result | ||
| } | ||
|
|
||
| private fun Buffer.truncateToSize(newByteCount: Long) { |
There was a problem hiding this comment.
You can't UnsafeCursor and walk back the counts/tail segments? I guess it'll never happen in a well behaved system so who cares the performance...
There was a problem hiding this comment.
Yeah, exactly. This is Okio itself, we could also just add a function to Buffer, trimToSize(). Not today though
Closes: #1208