Skip to content

Commit 6facf43

Browse files
committed
Add README examples for Base64Sink and CipherSink.
1 parent c64ed30 commit 6facf43

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@ Stream [Base64](http://www.ietf.org/rfc/rfc2045.txt)-encoded bytes using Okio's
1616
assertThat(decodedString).isEqualTo("okio oh my¿¡")
1717
```
1818

19+
```kotlin
20+
val utf8Sink = Buffer().writeUtf8("okio oh my¿¡")
21+
22+
val base64Buffer = Buffer()
23+
val base64Sink = Base64Sink(base64Buffer)
24+
25+
base64Sink.write(utf8Sink, Long.MAX_VALUE)
26+
27+
assertThat(base64Buffer.readUtf8()).isEqualTo("b2tpbyBvaCBtecK/wqE=")
28+
```
29+
30+
`Base64Source` supports partial and chunked reading, such as for decoding http chunked transfer mode responses in memory-efficient manner.
31+
1932
Think Apache Commons' [Base64InputStream and Base64OutputStream](https://commons.apache.org/proper/commons-codec/apidocs/org/apache/commons/codec/binary/package-summary.html) but for Okio `Source`s and `Sink`s.
2033

2134
## `CipherSource` and `CipherSink`
@@ -25,12 +38,25 @@ Encrypt and decrypt data in a streaming fashion using the Java platform's [Ciphe
2538
```kotlin
2639
val cipheredSource = ...
2740

28-
val decodedSource = CipherSource(cipheredSource, decodeCipher)
41+
val decodedSource = CipherSource(cipheredSource, decryptCipher)
2942

3043
val output = Buffer().also { it.writeAll(decodedSource) }
3144
assertThat(output.readUtf8()).isEqualTo("okio oh my¿¡")
3245
```
3346

47+
```kotlin
48+
val cipheredBuffer = Buffer()
49+
val cipheredSink = CipherSink(cipheredBuffer, encryptCipher)
50+
51+
val utf8Sink = Buffer().writeUtf8("okio oh my¿¡")
52+
cipheredSink.write(utf8Sink, Long.MAX_VALUE)
53+
54+
val deciphered = decodeCipher.doFinal(cipheredBuffer.readByteArray())
55+
assertThat(String(deciphered)).isEqualTo("okio oh my¿¡")
56+
```
57+
58+
`CipherSource` supports chunked deciphering, such as for decrypting http chunked transfer mode responses on the fly.
59+
3460
Think javax.crypto's standard [CipherInputStream and CipherOutputStream](https://docs.oracle.com/javase/7/docs/api/javax/crypto/package-summary.html) but for Okio `Source`s and `Sink`s.
3561

3662
## License and credits

0 commit comments

Comments
 (0)