// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Compresses what is written to a `std.Io.Writer`, passing the Brotli //! stream on to another `std.Io.Writer`. //! //! ``` //! var c: brotli.CompressWriter = .init(gpa, output_writer, &buffer, .{}); //! defer c.deinit(); //! try c.writer.writeAll(data); //! try c.finish(); //! try output_writer.flush(); //! ``` //! //! `finish` ends the stream; without it, what is written is not a complete //! Brotli stream. Flushing `writer` compresses everything written so far //! and carries the stream to a byte boundary, so that whoever is reading the //! output can decompress all of it; that costs a few bytes each time, so //! flush when the reader needs it rather than out of habit. Neither flushes //! the output writer. //! //! The `std.Io.Writer` interface has only `error.WriteFailed` to say that //! something went wrong. When the fault is the compressor's rather than the //! output writer's, `err` says what it was. //! //! The struct holds the interface that refers back to it, so it must not be //! moved once `writer` is in use. const std = @import("std"); const Allocator = std.mem.Allocator; const Io = std.Io; const Compressor = @import("Compressor.zig"); const CompressWriter = @This(); output: *Io.Writer, compressor: Compressor, /// Where the uncompressed data is written. writer: Io.Writer, /// Why the last write failed, if it was the compressor's fault rather than /// the output writer's. err: ?Compressor.Error = null, const vtable: Io.Writer.VTable = .{ .drain = drain, .flush = flush, }; /// `buffer` becomes the buffer of `writer`; it may be empty. The compressor /// gathers its input into blocks of its own, so a large buffer here saves /// only calls. pub fn init(gpa: Allocator, output: *Io.Writer, buffer: []u8, options: Compressor.Options) CompressWriter { return .{ .output = output, .compressor = .init(gpa, options), .writer = .{ .vtable = &vtable, .buffer = buffer, .end = 0 }, }; } pub fn deinit(c: *CompressWriter) void { c.compressor.deinit(); c.* = undefined; } /// Compresses whatever is still buffered and ends the stream. Everything is /// then written to the output writer, which is left for the caller to /// flush. pub fn finish(c: *CompressWriter) (Compressor.Error || Io.Writer.Error)!void { try c.run(c.writer.buffered(), .finish); c.writer.end = 0; } fn drain(w: *Io.Writer, data: []const []const u8, splat: usize) Io.Writer.Error!usize { const c: *CompressWriter = @alignCast(@fieldParentPtr("writer", w)); try c.feed(w.buffered(), .process); w.end = 0; var n: usize = 0; for (data[0 .. data.len - 1]) |bytes| { try c.feed(bytes, .process); n += bytes.len; } const last = data[data.len - 1]; for (0..splat) |_| { try c.feed(last, .process); n += last.len; } return n; } fn flush(w: *Io.Writer) Io.Writer.Error!void { const c: *CompressWriter = @alignCast(@fieldParentPtr("writer", w)); try c.feed(w.buffered(), .flush); w.end = 0; } /// `run`, with the compressor's own errors kept in `err`. fn feed(c: *CompressWriter, bytes: []const u8, op: Compressor.Operation) Io.Writer.Error!void { c.run(bytes, op) catch |e| switch (e) { error.WriteFailed => return error.WriteFailed, else => |ce| { c.err = ce; return error.WriteFailed; }, }; } /// Compresses all of `bytes` and carries out `op`, writing what comes out to /// the output writer. fn run(c: *CompressWriter, bytes: []const u8, op: Compressor.Operation) (Compressor.Error || Io.Writer.Error)!void { var in = bytes; while (true) { const r = try c.compressor.compressToWriter(in, c.output, .unlimited, op); in = in[r.read..]; switch (r.status) { .need_output => {}, .need_input, .done => return, } } }