A streaming Brotli (RFC 7932) decoder in pure MoonBit: arbitrary input chunks, bounded output slices, memory bounded by the window.
moon add gaato/brotli///|
fn decode_all(chunks : Array[Bytes], sink : (BytesView) -> Unit) -> Unit raise {
let decoder = @brotli.Decoder()
let out = FixedArray::make(64 * 1024, b'\x00')
for i, chunk in chunks {
let mut input = chunk[:]
for ;; {
let status = decoder.step_into(input, out, end=i == chunks.length() - 1)
input = input[decoder.last_consumed():]
sink(Bytes::from_array(out[:decoder.last_produced()]))
match status {
Done => return
NeedMoreInput => break
NeedMoreOutput => ()
}
}
}
}///|
fn example(compressed : Bytes) -> Bytes raise @brotli.BrotliError {
@brotli.decompress(compressed, max_output=16 * 1024 * 1024)
}pub struct Decoder {
// private fields
}fn Decoder::step(self : Decoder, input : BytesView, output : MutArrayView[Byte], end? : Bool) -> Status raise BrotliErrorfn Decoder::step_into(self : Decoder, input : BytesView, output : FixedArray[Byte], end? : Bool) -> Status raise BrotliErrorfn decompress(input : BytesView, max_output? : Int, max_window_bits? : Int) -> Bytes raise BrotliErrorInstall
Download zipA streaming Brotli (RFC 7932) decoder in pure MoonBit: arbitrary input chunks, bounded output slices, memory bounded by the window.