Base64 decoder for async streams?

My use case is:

I'm using tokio. I'm receiving Base64-encoded bytes via an async stream. I want to decode the bytes on the fly, and send the decoded bytes to their destination (a tokio::fs::File).

The decoder in the base64 crate supports Read inputs, but not AsyncRead. I couldn't find any crates that do support AsyncRead.

I think I can work around this by using spawn_blocking():

  1. Send the base64-encoded bytes to a tokio channel
  2. Use spawn_blocking() to run a synchronous task that decodes them, and sends the decoded bytes to a second tokio channel.
  3. Have another tokio task that receives the decode bytes and sends them to their final destination.

This is my fallback, but it's a bit clunky, to say the least.

Really what I need is a "stateful" base64 decoder, one to which I can submit the next batch of input bytes, and get back the next batch of decoded bytes (if any). I looked at the crates base64, base64ct, and base64-stream, but none of them provide a stateful decoder (base64ct supports "incremental decoding", but you still have to supply all the encoded data up front).

Does anyone know of a stateful Base64 decoder, or a way of decoding an async stream on the fly?

Hmm, it suddenly occurs to me that any Base64 decoder is "stateful", if you divide the input into chunks of multiples of 4 bytes first. Maybe that's the way...

3 Likes

Yes this worked nicely :slight_smile:

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.