Skip to content

erickok/okio-extensions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 

Repository files navigation

okio-extensions

A set of Okio extensions to work with various data formats.

    allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }
    
    dependencies {
            compile 'com.github.erickok:okio-extensions:v2.1'
    }

Written in Kotlin, works on plain Java (JVM) and Android.

Base64Source and Base64Sink

Stream Base64-encoded bytes using Okio's own Base64 implementation.

    val base64Source = Buffer().writeUtf8("b2tpbyBvaCBtecK/wqE=")

    val decodedSource = Base64Source(base64Source)

    val decodedString = Buffer().also { it.writeAll(decodedSource) }.readUtf8()
    assertThat(decodedString).isEqualTo("okio oh my¿¡")
    val utf8Sink = Buffer().writeUtf8("okio oh my¿¡")

    val base64Buffer = Buffer()
    val base64Sink = Base64Sink(base64Buffer)

    base64Sink.write(utf8Sink, Long.MAX_VALUE)

    assertThat(base64Buffer.readUtf8()).isEqualTo("b2tpbyBvaCBtecK/wqE=")

Base64Source supports partial and chunked reading, such as for decoding http chunked transfer mode responses in memory-efficient manner.

Think Apache Commons' Base64InputStream and Base64OutputStream but for Okio Sources and Sinks.

CipherSource and CipherSink

Encrypt and decrypt data in a streaming fashion using the Java platform's Cipher API.

    val cipheredSource = ...

    val decodedSource = CipherSource(cipheredSource, decryptCipher)

    val output = Buffer().also { it.writeAll(decodedSource) }
    assertThat(output.readUtf8()).isEqualTo("okio oh my¿¡")
    val cipheredBuffer = Buffer()
    val cipheredSink = CipherSink(cipheredBuffer, encryptCipher)

    val utf8Sink = Buffer().writeUtf8("okio oh my¿¡")
    cipheredSink.write(utf8Sink, Long.MAX_VALUE)

    val deciphered = decodeCipher.doFinal(cipheredBuffer.readByteArray())
    assertThat(String(deciphered)).isEqualTo("okio oh my¿¡")

CipherSource supports chunked deciphering, such as for decrypting http chunked transfer mode responses on the fly.

Think javax.crypto's standard CipherInputStream and CipherOutputStream but for Okio Sources and Sinks.

CipherSource and CipherSink are tested and used in production with AES ciphers, but should work (without warrenty) with any Java-supported Cipher algorithm.

License and credits

Designed and developed by Eric Kok of 2312 development.

Copyright 2018 Eric Kok

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.