Menu

Okio 1.3 has segment sharing

Okio’s buffer reads like an InputStream: when you read 50 bytes from the buffer, you implicitly remove those bytes from the buffer. This is a handy optimization and it means we can move data rather than copying it by default.

But sometimes you need non-destructive reads. So in Okio 1.3 we've changed the Buffer.clone() method to do segment sharing. Previously, cloning a 2 megabyte buffer would require 2 megabytes of memory to be allocated and copied. With segment sharing, only the bookkeeping data is copied, which is about 3% of the size of the buffer’s data.

To read a buffer without consuming it, just create a (cheap) copy and read that. And our fundamental optimization is preserved: reading from the clone will move data rather than making even more copies.

Okio 1.3 also adds some nice ergonomic features to ByteString:

  • URL-safe Base64 encoding
  • Handy MD5 and SHA-256 hashes
  • Substrings!

There’s also new APIs in BufferedSource and BufferedSink:

  • Read and write signed decimal values without the string intermediates that you'd need with Long.valueOf(String). We can use this in DiskLruCache’s journal file, among other places.
  • Read and write unsigned hexadecimal values, also without intermediate strings. We’re already using this for chunked encoding in OkHttp.

Okio is an easy-to-use, efficient library for Java I/O that everyone should try. We're very proud of it!

Get it

The changelog has full details of what's new & what's been fixed. Get 1.3.0 from Maven Central:

<dependency>  
  <groupId>com.squareup.okio</groupId>
  <artifactId>okio</artifactId>
  <version>1.3.0</version>
</dependency>