A modern I/O API for Java. Okio is a new library that complements java.io and java.nio to make it much easier to access, store, and process your data.
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, String is fundamental. ByteString is 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.
Internally, 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.
SDK for Android
New: Change the segment size to 8 KiB. This has been reported to dramatically improve performance in some applications.
New: md5(), sha1(), and sha256() methods on Buffer. Also add a sha1() method on ByteString for symmetry.
New: HashingSource and HashingSink. These classes are Okio’s equivalent to the JDK’s DigestInputStream and DigestOutputStream. They offer convenient md5(), sha1(), and sha256() factory methods to avoid an impossible NoSuchAlgorithmException.
Fix: Limit snapshot byte strings to requested size.
Fix: Change write timeouts to have a maximum write size. Previously large writes could easly suffer timeouts because the entire write was subject to a single timeout.
Fix: Recover from EBADF failures, which could be triggered by asynchronously closing a stream on older versions of Android.
Fix: Don't share segments if doing so only saves a small copy. This should improve performance for all applications.
Fix: Optimize BufferedSource.indexOfElement() and indexOf(ByteString). Previously this method had a bug that caused it to be very slow on large buffers.