Skip to content
FutureBlitz

    Install

    Import std.compress.stream

    std.compress.stream

    Compressing and decompressing streams as they're written and read.

    std.compress over std.io's streams: a CoderWriter writes what's written to it through a Coder, like a GzipCompressor, to another Writer, and a CoderReader reads what another Reader gives through one, like a GzipDecompressor, so a file or a socket compresses and decompresses as it goes, a part at a time.

    std/compress/stream.fb

    Types

    CoderWriter class

    Public Class CoderWriter Implements Writer

    A Writer whose bytes go through a Coder to another Writer: Write adds them to the Coder and writes what comes of them, and Finish writes the rest, which ends what the Coder makes, as CoderWriter(file, GzipCompressor()) ends a gzip member. A Coder holds on to what it hasn't coded, so Flush flushes only the other Writer, and the Coder's rest comes with Finish. Each throws the IOError the Coder or the Writer does.

    Examples

    Local memory = MemoryStream()
    Local writer = CoderWriter(memory, GzipCompressor())
    Try writer.WriteText("written a part at a time")
    Try writer.Finish()
    Expect(String.FromUtf8(Try Gzip.Decompress(memory.ToArray())) == "written a part at a time")

    std/compress/stream.fb, line 33

    • Public New(inner: Writer, through: Coder) constructor #

      A CoderWriter that writes to inner what through makes of the bytes it's given.

    • Public Method Write(from: Buffer<UInt8>) Throws IOError method #

      Puts the bytes through the Coder, and writes what comes of them so far to the other Writer.

    • Public Method Flush() Throws IOError method #

      Flushes the other Writer, but not the Coder, whose rest comes with Finish.

    • Public Method Finish() Throws IOError method #

      Writes what the Coder has left, and flushes the other Writer. The Coder starts again, so what's written next is coded on its own, as a gzip's next member is.

    CoderReader class

    Public Class CoderReader Implements Reader

    A Reader whose bytes are what another Reader's come to through a Coder, as CoderReader(file, GzipDecompressor()) reads a .gz file's: it reads the other Reader a chunk at a time, and once that ends, gives what the Coder has left, and then ends. Read throws the IOError the Coder or the Reader does, like a Decompressor's for bytes that aren't its format, or that end early, and a Read after that throws again, or ends.

    Examples

    Local gz = Gzip.Compress("read back".Bytes)
    Local reader = CoderReader(MemoryStream(gz), GzipDecompressor())
    Expect(Try ReadAllText(reader) == "read back")

    std/compress/stream.fb, line 77

    • Public New(inner: Reader, through: Coder) constructor #

      A CoderReader that reads what through makes of what inner gives.

    • Public Method Read(into: Buffer<UInt8>): Int64 Throws IOError method #

      Reads what the other Reader's bytes come to through the Coder into into, and gives how many it read, or 0 once both have ended. Throws the IOError the Coder or the other Reader does.

    Report an issue with this page or with FutureBlitz on GitHub.