| ... | ... | @@ -247,6 +247,27 @@ pub fn Reader( |
| 247 | 247 | return bytes; |
| 248 | 248 | } |
| 249 | 249 | |
| 250 | /// Reads bytes into the bounded array, until |
| 251 | /// the bounded array is full, or the stream ends. |
| 252 | pub fn readIntoBoundedBytes( |
| 253 | self: Self, |
| 254 | comptime num_bytes: usize, |
| 255 | bounded: *std.BoundedArray(u8, num_bytes), |
| 256 | ) !void { |
| 257 | while (bounded.len < num_bytes) { |
| 258 | const bytes_read = try self.read(bounded.unusedCapacitySlice()); |
| 259 | if (bytes_read == 0) return; |
| 260 | bounded.len += bytes_read; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | /// Reads at most `num_bytes` and returns as a bounded array. |
| 265 | pub fn readBoundedBytes(self: Self, comptime num_bytes: usize) !std.BoundedArray(u8, num_bytes) { |
| 266 | var result = std.BoundedArray(u8, num_bytes){}; |
| 267 | try self.readIntoBoundedBytes(num_bytes, &result); |
| 268 | return result; |
| 269 | } |
| 270 | |
| 250 | 271 | /// Reads a native-endian integer |
| 251 | 272 | pub fn readIntNative(self: Self, comptime T: type) !T { |
| 252 | 273 | const bytes = try self.readBytesNoEof((@typeInfo(T).Int.bits + 7) / 8); |