Skip to content

Make FixedLengthSource public API#1823

Merged
swankjesse merged 2 commits into
masterfrom
jwilson.0719.source_limit
Jul 20, 2026
Merged

Make FixedLengthSource public API#1823
swankjesse merged 2 commits into
masterfrom
jwilson.0719.source_limit

Conversation

@swankjesse

Copy link
Copy Markdown
Collaborator

Closes: #1208

@JvmOverloads
fun Source.limit(
byteCount: Long,
throwIfSourceIsLonger: Boolean = false,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is sufficient for OkHttp's needs? I thought it needed to know if you exhausted the limit or not.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +140 to +141
// If we received bytes beyond the limit, don't return them to the caller.
sink.truncateToSize(sink.size - beyondLimitByteCount)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would only happen from a misbehaving source, right?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, exactly. This is Okio itself, we could also just add a function to Buffer, trimToSize(). Not today though

@swankjesse
swankjesse merged commit 304f508 into master Jul 20, 2026
14 checks passed
@swankjesse
swankjesse deleted the jwilson.0719.source_limit branch July 20, 2026 02:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Make FixedLengthSource public API

2 participants