lz4.StreamingDecompressor
StreamingDecompressor enables pull-based, chunk-by-chunk decompression. Compressed byte slices are fed as they arrive from a socket or file stream, and decompressed bytes are drained into caller-provided buffers.
pub const StreamingDecompressor = struct {
allocator: std.mem.Allocator,
options: DecompressionOptions,
// ...
};Methods
init
pub fn init(allocator: std.mem.Allocator, opts: DecompressionOptions) StreamingDecompressorInitializes an incremental decompressor with the specified options.
deinit
pub fn deinit(self: *StreamingDecompressor) voidFrees internal accumulation buffers.
reset
pub fn reset(self: *StreamingDecompressor) voidClears buffers and resets state for decoding a new stream while retaining allocated capacity.
feed
pub fn feed(self: *StreamingDecompressor, chunk: []const u8) !voidAppends chunk of compressed data to the internal input buffer.
take
pub fn take(self: *StreamingDecompressor, dst: []u8) !usizeExtracts available uncompressed bytes into dst. Returns the number of bytes written. If more decompressed data is available than can fit in dst, subsequent calls to take will continue draining the buffer until it returns 0.
isFinished
pub fn isFinished(self: *const StreamingDecompressor) boolReturns true when all compressed frames have been completely decoded and all uncompressed data has been drained via take.
Example Usage
const std = @import("std");
const lz4 = @import("lz4");
pub fn processStream(allocator: std.mem.Allocator, socket: std.net.Stream) !void {
var decompressor = lz4.StreamingDecompressor.init(allocator, .{});
defer decompressor.deinit();
var network_buf: [4096]u8 = undefined;
var output_buf: [4096]u8 = undefined;
while (true) {
const bytes_read = try socket.read(&network_buf);
if (bytes_read == 0) break;
try decompressor.feed(network_buf[0..bytes_read]);
while (true) {
const count = try decompressor.take(&output_buf);
if (count == 0) break;
// Handle uncompressed bytes in output_buf[0..count]
}
}
}