This expands upon Reader Delimiters and Missing streamUntilDelimiterOrEof
. More specifically:
This is, in my opinion, the biggest problem of streamUntilDelimiter
since this limits its use to only text and other forms of data that don’t use all 256 possible bytes (Large Binary Files).
Although, I think that streamUntilDelimiterOrEof
should NOT be included in the language, since streamUntilDelimiter
was made to replace all those variations in the first place.
Instead, I think that the delimiter parameter should be optional (?u8
instead of u8
). This way, no new functions will be added and the changes that are made are almost non-breaking.
Consider the following:
// Download the latest zig master
// https://ziglang.org/download/index.json {master.x86_64-linux.tarball}
var f = try std.fs.cwd().createFile("zig-linux-x86_64-master.tar.xz", .{});
defer f.close();
if (false) {
// size = https:ziglang.org/download/index.json {master.x86_64-linux.size}
var buffer = try alloc.alloc(u8, size);
defer alloc.free(buffer);
_ = try dl_req.readAll(buffer);
try f.writeAll(buffer);
} else {
var buffer = try std. ArrayList(u8).init(alloc);
defer buffer.deinit();
while (true) {
const byte = dl_req.reader().readByte() catch |err| switch (err) {
error. EndOfStream => break,
else => |e| return e,
};
try buffer.writer().writeByte(byte);
}
try f.writeAll(buffer.items);
}
The first method assumes that the download provider has provided an accurate size number for the file you are downloading (Thankfully ziglang.org does).
The second method assumes that the download provider does not provide an accurate size number. streamUntilDelimiter
provides 99% of the functionality to read the entire file, only to be blocked by if (byte == delimiter) return;
. Thus, I must reimplement parts of it achieve the functionality I need.
If streamUntilDelimiter
was fn(std.io.Reader, anytype, ?u8, ?usize)
, it would look like:
dl_req.reader().streamUntilDelimiter(buffer.writer(), null, null) catch |err| switch (err) {
error.EndOfStream => {},
else => |e| return e,
};