xref: /aosp_15_r20/external/okio/docs/index.md (revision f9742813c14b702d71392179818a9e591da8620c)
1*f9742813SAndroid Build Coastguard WorkerOkio
2*f9742813SAndroid Build Coastguard Worker====
3*f9742813SAndroid Build Coastguard Worker
4*f9742813SAndroid Build Coastguard WorkerOkio is a library that complements `java.io` and `java.nio` to make it much
5*f9742813SAndroid Build Coastguard Workereasier to access, store, and process your data. It started as a component of
6*f9742813SAndroid Build Coastguard Worker[OkHttp][1], the capable HTTP client included in Android. It's well-exercised
7*f9742813SAndroid Build Coastguard Workerand ready to solve new problems.
8*f9742813SAndroid Build Coastguard Worker
9*f9742813SAndroid Build Coastguard WorkerByteStrings and Buffers
10*f9742813SAndroid Build Coastguard Worker-----------------------
11*f9742813SAndroid Build Coastguard Worker
12*f9742813SAndroid Build Coastguard WorkerOkio is built around two types that pack a lot of capability into a
13*f9742813SAndroid Build Coastguard Workerstraightforward API:
14*f9742813SAndroid Build Coastguard Worker
15*f9742813SAndroid Build Coastguard Worker * [**ByteString**][3] is an immutable sequence of bytes. For character data, `String`
16*f9742813SAndroid Build Coastguard Worker   is fundamental. `ByteString` is String's long-lost brother, making it easy to
17*f9742813SAndroid Build Coastguard Worker   treat binary data as a value. This class is ergonomic: it knows how to encode
18*f9742813SAndroid Build Coastguard Worker   and decode itself as hex, base64, and UTF-8.
19*f9742813SAndroid Build Coastguard Worker
20*f9742813SAndroid Build Coastguard Worker * [**Buffer**][4] is a mutable sequence of bytes. Like `ArrayList`, you don't need
21*f9742813SAndroid Build Coastguard Worker   to size your buffer in advance. You read and write buffers as a queue: write
22*f9742813SAndroid Build Coastguard Worker   data to the end and read it from the front. There's no obligation to manage
23*f9742813SAndroid Build Coastguard Worker   positions, limits, or capacities.
24*f9742813SAndroid Build Coastguard Worker
25*f9742813SAndroid Build Coastguard WorkerInternally, `ByteString` and `Buffer` do some clever things to save CPU and
26*f9742813SAndroid Build Coastguard Workermemory. If you encode a UTF-8 string as a `ByteString`, it caches a reference to
27*f9742813SAndroid Build Coastguard Workerthat string so that if you decode it later, there's no work to do.
28*f9742813SAndroid Build Coastguard Worker
29*f9742813SAndroid Build Coastguard Worker`Buffer` is implemented as a linked list of segments. When you move data from
30*f9742813SAndroid Build Coastguard Workerone buffer to another, it _reassigns ownership_ of the segments rather than
31*f9742813SAndroid Build Coastguard Workercopying the data across. This approach is particularly helpful for multithreaded
32*f9742813SAndroid Build Coastguard Workerprograms: a thread that talks to the network can exchange data with a worker
33*f9742813SAndroid Build Coastguard Workerthread without any copying or ceremony.
34*f9742813SAndroid Build Coastguard Worker
35*f9742813SAndroid Build Coastguard WorkerSources and Sinks
36*f9742813SAndroid Build Coastguard Worker-----------------
37*f9742813SAndroid Build Coastguard Worker
38*f9742813SAndroid Build Coastguard WorkerAn elegant part of the `java.io` design is how streams can be layered for
39*f9742813SAndroid Build Coastguard Workertransformations like encryption and compression. Okio includes its own stream
40*f9742813SAndroid Build Coastguard Workertypes called [`Source`][5] and [`Sink`][6] that work like `InputStream` and
41*f9742813SAndroid Build Coastguard Worker`OutputStream`, but with some key differences:
42*f9742813SAndroid Build Coastguard Worker
43*f9742813SAndroid Build Coastguard Worker * **Timeouts.** The streams provide access to the timeouts of the underlying
44*f9742813SAndroid Build Coastguard Worker   I/O mechanism. Unlike the `java.io` socket streams, both `read()` and
45*f9742813SAndroid Build Coastguard Worker   `write()` calls honor timeouts.
46*f9742813SAndroid Build Coastguard Worker
47*f9742813SAndroid Build Coastguard Worker * **Easy to implement.** `Source` declares three methods: `read()`, `close()`,
48*f9742813SAndroid Build Coastguard Worker   and `timeout()`. There are no hazards like `available()` or single-byte reads
49*f9742813SAndroid Build Coastguard Worker   that cause correctness and performance surprises.
50*f9742813SAndroid Build Coastguard Worker
51*f9742813SAndroid Build Coastguard Worker * **Easy to use.** Although _implementations_ of `Source` and `Sink` have only
52*f9742813SAndroid Build Coastguard Worker   three methods to write, _callers_ are given a rich API with the
53*f9742813SAndroid Build Coastguard Worker   [`BufferedSource`][7] and [`BufferedSink`][8] interfaces. These interfaces give you
54*f9742813SAndroid Build Coastguard Worker   everything you need in one place.
55*f9742813SAndroid Build Coastguard Worker
56*f9742813SAndroid Build Coastguard Worker * **No artificial distinction between byte streams and char streams.** It's all
57*f9742813SAndroid Build Coastguard Worker   data. Read and write it as bytes, UTF-8 strings, big-endian 32-bit integers,
58*f9742813SAndroid Build Coastguard Worker   little-endian shorts; whatever you want. No more `InputStreamReader`!
59*f9742813SAndroid Build Coastguard Worker
60*f9742813SAndroid Build Coastguard Worker * **Easy to test.** The `Buffer` class implements both `BufferedSource` and
61*f9742813SAndroid Build Coastguard Worker   `BufferedSink` so your test code is simple and clear.
62*f9742813SAndroid Build Coastguard Worker
63*f9742813SAndroid Build Coastguard WorkerSources and sinks interoperate with `InputStream` and `OutputStream`. You can
64*f9742813SAndroid Build Coastguard Workerview any `Source` as an `InputStream`, and you can view any `InputStream` as a
65*f9742813SAndroid Build Coastguard Worker`Source`. Similarly for `Sink` and `OutputStream`.
66*f9742813SAndroid Build Coastguard Worker
67*f9742813SAndroid Build Coastguard Worker
68*f9742813SAndroid Build Coastguard WorkerPresentations
69*f9742813SAndroid Build Coastguard Worker-------------
70*f9742813SAndroid Build Coastguard Worker
71*f9742813SAndroid Build Coastguard Worker[A Few “Ok” Libraries][ok_libraries_talk] ([slides][ok_libraries_slides]): An introduction to Okio
72*f9742813SAndroid Build Coastguard Workerand three libraries written with it.
73*f9742813SAndroid Build Coastguard Worker
74*f9742813SAndroid Build Coastguard Worker[Decoding the Secrets of Binary Data][encoding_talk] ([slides][encoding_slides]): How data encoding
75*f9742813SAndroid Build Coastguard Workerworks and how Okio does it.
76*f9742813SAndroid Build Coastguard Worker
77*f9742813SAndroid Build Coastguard Worker[Ok Multiplatform!][ok_multiplatform_talk] ([slides][ok_multiplatform_slides]): How we changed
78*f9742813SAndroid Build Coastguard WorkerOkio’s implementation language from Java to Kotlin.
79*f9742813SAndroid Build Coastguard Worker
80*f9742813SAndroid Build Coastguard Worker[Nerding Out On Okio][apis_talk]: The story of the Okio APIs, their design and tradeoffs, as well
81*f9742813SAndroid Build Coastguard Workeras implementation notes with animated marbles diagrams.
82*f9742813SAndroid Build Coastguard Worker
83*f9742813SAndroid Build Coastguard Worker
84*f9742813SAndroid Build Coastguard WorkerRequirements
85*f9742813SAndroid Build Coastguard Worker------------
86*f9742813SAndroid Build Coastguard Worker
87*f9742813SAndroid Build Coastguard WorkerOkio 2.x supports Android 4.0.3+ (API level 15+) and Java 7+.
88*f9742813SAndroid Build Coastguard Worker
89*f9742813SAndroid Build Coastguard WorkerOkio 3.x supports Android 4.0.3+ (API level 15+) and Java 8+.
90*f9742813SAndroid Build Coastguard Worker
91*f9742813SAndroid Build Coastguard WorkerOkio depends on the [Kotlin standard library][kotlin]. It is a small library with strong
92*f9742813SAndroid Build Coastguard Workerbackward-compatibility.
93*f9742813SAndroid Build Coastguard Worker
94*f9742813SAndroid Build Coastguard Worker
95*f9742813SAndroid Build Coastguard WorkerReleases
96*f9742813SAndroid Build Coastguard Worker--------
97*f9742813SAndroid Build Coastguard Worker
98*f9742813SAndroid Build Coastguard WorkerOur [change log][changelog] has release history.
99*f9742813SAndroid Build Coastguard Worker
100*f9742813SAndroid Build Coastguard Worker```kotlin
101*f9742813SAndroid Build Coastguard Workerimplementation("com.squareup.okio:okio:3.7.0")
102*f9742813SAndroid Build Coastguard Worker```
103*f9742813SAndroid Build Coastguard Worker
104*f9742813SAndroid Build Coastguard Worker<details>
105*f9742813SAndroid Build Coastguard Worker   <summary>Snapshot builds are also available</summary>
106*f9742813SAndroid Build Coastguard Worker
107*f9742813SAndroid Build Coastguard Worker```kotlin
108*f9742813SAndroid Build Coastguard Workerrepositories {
109*f9742813SAndroid Build Coastguard Worker    maven("https://s01.oss.sonatype.org/content/repositories/snapshots/")
110*f9742813SAndroid Build Coastguard Worker}
111*f9742813SAndroid Build Coastguard Worker
112*f9742813SAndroid Build Coastguard Workerdependencies {
113*f9742813SAndroid Build Coastguard Worker   implementation("com.squareup.okio:okio:3.7.0")
114*f9742813SAndroid Build Coastguard Worker}
115*f9742813SAndroid Build Coastguard Worker```
116*f9742813SAndroid Build Coastguard Worker
117*f9742813SAndroid Build Coastguard Worker</details>
118*f9742813SAndroid Build Coastguard Worker
119*f9742813SAndroid Build Coastguard Worker
120*f9742813SAndroid Build Coastguard WorkerLicense
121*f9742813SAndroid Build Coastguard Worker--------
122*f9742813SAndroid Build Coastguard Worker
123*f9742813SAndroid Build Coastguard Worker    Copyright 2013 Square, Inc.
124*f9742813SAndroid Build Coastguard Worker
125*f9742813SAndroid Build Coastguard Worker    Licensed under the Apache License, Version 2.0 (the "License");
126*f9742813SAndroid Build Coastguard Worker    you may not use this file except in compliance with the License.
127*f9742813SAndroid Build Coastguard Worker    You may obtain a copy of the License at
128*f9742813SAndroid Build Coastguard Worker
129*f9742813SAndroid Build Coastguard Worker       http://www.apache.org/licenses/LICENSE-2.0
130*f9742813SAndroid Build Coastguard Worker
131*f9742813SAndroid Build Coastguard Worker    Unless required by applicable law or agreed to in writing, software
132*f9742813SAndroid Build Coastguard Worker    distributed under the License is distributed on an "AS IS" BASIS,
133*f9742813SAndroid Build Coastguard Worker    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134*f9742813SAndroid Build Coastguard Worker    See the License for the specific language governing permissions and
135*f9742813SAndroid Build Coastguard Worker    limitations under the License.
136*f9742813SAndroid Build Coastguard Worker
137*f9742813SAndroid Build Coastguard Worker [1]: https://github.com/square/okhttp
138*f9742813SAndroid Build Coastguard Worker [3]: https://square.github.io/okio/3.x/okio/okio/okio/-byte-string/index.html
139*f9742813SAndroid Build Coastguard Worker [4]: https://square.github.io/okio/3.x/okio/okio/okio/-buffer/index.html
140*f9742813SAndroid Build Coastguard Worker [5]: https://square.github.io/okio/3.x/okio/okio/okio/-source/index.html
141*f9742813SAndroid Build Coastguard Worker [6]: https://square.github.io/okio/3.x/okio/okio/okio/-sink/index.html
142*f9742813SAndroid Build Coastguard Worker [7]: https://square.github.io/okio/3.x/okio/okio/okio/-buffered-source/index.html
143*f9742813SAndroid Build Coastguard Worker [8]: https://square.github.io/okio/3.x/okio/okio/okio/-buffered-sink/index.html
144*f9742813SAndroid Build Coastguard Worker [changelog]: http://square.github.io/okio/changelog/
145*f9742813SAndroid Build Coastguard Worker [javadoc]: https://square.github.io/okio/2.x/okio/okio/index.html
146*f9742813SAndroid Build Coastguard Worker [kotlin]: https://kotlinlang.org/
147*f9742813SAndroid Build Coastguard Worker [ok_libraries_talk]: https://www.youtube.com/watch?v=WvyScM_S88c
148*f9742813SAndroid Build Coastguard Worker [ok_libraries_slides]: https://speakerdeck.com/jakewharton/a-few-ok-libraries-droidcon-mtl-2015
149*f9742813SAndroid Build Coastguard Worker [encoding_talk]: https://www.youtube.com/watch?v=T_p22jMZSrk
150*f9742813SAndroid Build Coastguard Worker [encoding_slides]: https://speakerdeck.com/swankjesse/decoding-the-secrets-of-binary-data-droidcon-nyc-2016
151*f9742813SAndroid Build Coastguard Worker [ok_multiplatform_talk]: https://www.youtube.com/watch?v=Q8B4eDirgk0
152*f9742813SAndroid Build Coastguard Worker [ok_multiplatform_slides]: https://speakerdeck.com/swankjesse/ok-multiplatform
153*f9742813SAndroid Build Coastguard Worker [apis_talk]: https://www.youtube.com/watch?v=Du7YXPAV1M8
154