square / okio

A modern I/O library for Android, Java, and Kotlin Multiplatform.
https://square.github.io/okio/
Apache License 2.0
8.81k stars 1.18k forks source link

Base64 decoding/encoding source/sink #613

Open MartinDevi opened 5 years ago

MartinDevi commented 5 years ago
class Base64Decoder(private val reader: Reader): Source
class Base64Encoder(private val writer: Writer): Sink

Has this feature been considered before? I'd happily put some time in to work on it.

In the suggestion above, I use the Reader/Writer classes because since Base64 uses text characters it seems to make more sense.

swankjesse commented 5 years ago

You should avoid situations that require streaming base64. The ByteString encoder/decoder should be enough for most use cases.

That said, I'd love to see a gist that does this. In those awkward cases where you have a lot of base64 this could be handy.

MartinDevi commented 5 years ago

If only I had the luxury of choosing the format of the data that I receive 😄

I gave it a shot for the Base64Decoder. I would've preferred to have this within the Okio library itself though, rather than having to copy the Base64 decoder into my own project.

Base64Decoder.kt

All that's needed is some specific Base64 methods which don't perform string allocations. Should be fairly easy to add.

fun CharArray.decodeBase64(target: ByteArray, offset: Int, length: Int)
fun CharArray.decodeBase64(target: Buffer, offset: Int, length: Int)