okio icon indicating copy to clipboard operation
okio copied to clipboard

buildByteString

Open swankjesse opened this issue 6 years ago • 4 comments

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()
}

swankjesse avatar May 11 '19 16:05 swankjesse

There are a couple choice with a function like this.

Location

  1. Top level function buildByteString {...} - single location
  2. ByteString companion function ByteString.build {...} - expect/actual and all that fun but scoped

Conversion

  1. buffer.readByteString() - results in copy
  2. buffer.snapshot() - SegmentedByteString and 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().

bnorm avatar Oct 07 '19 18:10 bnorm

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?

swankjesse avatar Oct 07 '19 22:10 swankjesse

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.

bnorm avatar Oct 08 '19 14:10 bnorm

Exactly right right place for the enhancements!

swankjesse avatar Oct 08 '19 18:10 swankjesse