Okio is a library that complements
java.nio to make it much easier to access, store, and process your data. It started as a component of OkHttp, the capable HTTP client included in Android. It’s well-exercised and ready to solve new problems.
ByteStrings and Buffers
Okio is built around two types that pack a lot of capability into a straightforward API:
- ByteString is an immutable sequence of bytes. For character data,
ByteStringis String’s long-lost brother, making it easy to treat binary data as a value. This class is ergonomic: it knows how to encode and decode itself as hex, base64, and UTF-8.
- Buffer is a mutable sequence of bytes. Like
ArrayList, you don’t need to size your buffer in advance. You read and write buffers as a queue: write data to the end and read it from the front. There’s no obligation to manage positions, limits, or capacities.
Buffer do some clever things to save CPU and memory. If you encode a UTF-8 string as a
ByteString, it caches a reference to that string so that if you decode it later, there’s no work to do.
Buffer is implemented as a linked list of segments. When you move data from one buffer to another, it reassigns ownership of the segments rather than copying the data across. This approach is particularly helpful for multithreaded programs: a thread that talks to the network can exchange data with a worker thread without any copying or ceremony.
Sources and Sinks
An elegant part of the
java.io design is how streams can be layered for transformations like encryption and compression. Okio includes its own stream types called
Sink that work like
OutputStream, but with some key differences:
- Timeouts. The streams provide access to the timeouts of the underlying I/O mechanism. Unlike the
java.iosocket streams, both
write()calls honor timeouts.
- Easy to implement.
Sourcedeclares three methods:
timeout(). There are no hazards like
available()or single-byte reads that cause correctness and performance surprises.
- Easy to use. Although implementations of
Sinkhave only three methods to write, callers are given a rich API with the
BufferedSinkinterfaces. These interfaces give you everything you need in one place.
- No artificial distinction between byte streams and char streams. It’s all data. Read and write it as bytes, UTF-8 strings, big-endian 32-bit integers, little-endian shorts; whatever you want. No more
- Easy to test. The
Bufferclass implements both
BufferedSinkso your test code is simple and clear.
Sources and sinks interoperate with
OutputStream. You can view any
Source as an
InputStream, and you can view any
InputStream as a
Source. Similarly for
Okio 2.x supports Android 4.0.3+ (API level 15+) and Java 7+.
Okio 3.x supports Android 4.0.3+ (API level 15+) and Java 8+.
Okio depends on the Kotlin standard library. It is a small library with strong backward-compatibility.
Mobile App Development Best Practices – 04.10
iOS New and Deprecated APIs in iOS 17 Abstract Class vs. Protocol-Oriented Approach in Swift Comparing the Performance of the...
New and Deprecated APIs in iOS 17
In this video, I would like to share with you some things that were either deprecated or added in iOS...
Promova helps people with dyslexia learn languages
The new Promova feature comes just in time for National Dyslexia Awareness Month and is available on the platform for...
Notify – A simple note application with modern MVVM, Compose and Material3
Notify is a simple note application that is built with Modern Android development tools. This project showcases the Good implementation...
Mobile App Development Best Practices – 03.10
iOS MetaCodable – Supercharge Swift’s Codable implementations with macros meta-programming How to build a Tuist plugin and publish it using...
How to make and use BOM (Bill of Materials) dependencies in Android projects
By using a BOM dependency, you can avoid specifying the versions of each individual library in your app, and let...