okio icon indicating copy to clipboard operation
okio copied to clipboard

Base64 decoding/encoding source/sink

Open MartinDevi opened this issue 6 years ago • 2 comments

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.

MartinDevi avatar May 22 '19 13:05 MartinDevi

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.

swankjesse avatar May 23 '19 00:05 swankjesse

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)

MartinDevi avatar May 23 '19 12:05 MartinDevi