okio
okio copied to clipboard
buildByteString
This would be handy in ~4 places in OkHttp, possibly more elsewhere.
inline fun buildByteString(block: Buffer.() -> Unit): ByteString {
val buffer = Buffer()
buffer.block()
return buffer.readByteString()
}
There are a couple choice with a function like this.
Location
- Top level function
buildByteString {...}- single location ByteStringcompanion functionByteString.build {...}- expect/actual and all that fun but scoped
Conversion
buffer.readByteString()- results in copybuffer.snapshot()-SegmentedByteStringand everything that comes with it
I'm leaning towards ByteString.build {...} and buffer.readByteString(). Could add ByteString.buildSnapshot {...} if we wanted to use buffer.snapshot().
I think we should have a thoughtful policy on when to do snapshots. Maybe everywhere the byte string is at least size X, where X is 2 segments or something?
Are you talking about enhancing readByteString() to return snapshots when the byte count is above some threshold? Or only for this specific builder function? I would make this enhancement to readByteString() so everyone benefits.
Exactly right right place for the enhancements!