readVec
Writes bytes from the internally tracked stream position to data.
Returns the number of bytes written, which will be at minimum 0 and
at most the sum of each data slice length. The number of bytes read,
including zero, does not indicate end of stream.
The reader's internal logical seek position moves forward in accordance with the number of bytes returned from this function.
Function parameters
Parameters
- r:*Reader
- data:[][]u8
Type definitions in this namespace
Types
Functions in this namespace
Functions
- fixed
- Constructs a `Reader` such that it will read from `buffer` and then end.
- streamExact
- "Pump" exactly `n` bytes from the reader to the writer.
- streamExact64
- "Pump" exactly `n` bytes from the reader to the writer.
- streamExactPreserve
- "Pump" exactly `n` bytes from the reader to the writer.
- streamRemaining
- "Pump" data from the reader to the writer, handling `error.EndOfStream` as
- discardRemaining
- Consumes the stream until the end, ignoring all the data, returning the
- allocRemaining
- Transfers all bytes from the current position to the end of the stream, up
- appendRemaining
- Transfers all bytes from the current position to the end of the stream, up
- readVec
- Writes bytes from the internally tracked stream position to `data`.
- defaultReadVec
- Writes to `Reader.buffer` or `data`, whichever has larger capacity.
- peek
- Returns the next `len` bytes from the stream, filling the buffer as
- peekGreedy
- Returns all the next buffered bytes, after filling the buffer to ensure it
- toss
- Skips the next `n` bytes from the stream, advancing the seek position.
- tossBuffered
- Equivalent to `toss(r.bufferedLen())`.
- take
- Equivalent to `peek` followed by `toss`.
- takeArray
- Returns the next `n` bytes from the stream as an array, filling the buffer
- peekArray
- Returns the next `n` bytes from the stream as an array, filling the buffer
- discardAll
- Skips the next `n` bytes from the stream, advancing the seek position.
- discardShort
- Skips the next `n` bytes from the stream, advancing the seek position.
- readSliceAll
- Fill `buffer` with the next `buffer.len` bytes from the stream, advancing
- readSliceShort
- Fill `buffer` with the next `buffer.len` bytes from the stream, advancing
- readSliceEndian
- Fill `buffer` with the next `buffer.len` bytes from the stream, advancing
- readSliceEndianAlloc
- The function is inline to avoid the dead code in case `endian` is
- readAlloc
- Shortcut for calling `readSliceAll` with a buffer provided by `allocator`.
- takeSentinel
- Returns a slice of the next bytes of buffered data from the stream until
- peekSentinel
- Returns a slice of the next bytes of buffered data from the stream until
- takeDelimiterInclusive
- Returns a slice of the next bytes of buffered data from the stream until
- peekDelimiterInclusive
- Returns a slice of the next bytes of buffered data from the stream until
- takeDelimiterExclusive
- Returns a slice of the next bytes of buffered data from the stream until
- takeDelimiter
- Returns a slice of the next bytes of buffered data from the stream until
- peekDelimiterExclusive
- Returns a slice of the next bytes of buffered data from the stream until
- streamDelimiter
- Appends to `w` contents by reading from the stream until `delimiter` is
- streamDelimiterEnding
- Appends to `w` contents by reading from the stream until `delimiter` is found.
- streamDelimiterLimit
- Appends to `w` contents by reading from the stream until `delimiter` is found.
- discardDelimiterInclusive
- Reads from the stream until specified byte is found, discarding all data,
- discardDelimiterExclusive
- Reads from the stream until specified byte is found, discarding all data,
- discardDelimiterLimit
- Reads from the stream until specified byte is found, discarding all data,
- fill
- Fills the buffer such that it contains at least `n` bytes, without
- fillMore
- Without advancing the seek position, does exactly one underlying read, filling the buffer as
- peekByte
- Returns the next byte from the stream or returns `error.EndOfStream`.
- takeByte
- Reads 1 byte from the stream or returns `error.EndOfStream`.
- takeByteSigned
- Same as `takeByte` except the returned byte is signed.
- takeInt
- Asserts the buffer was initialized with a capacity at least `@bitSizeOf(T) / 8`.
- peekInt
- Asserts the buffer was initialized with a capacity at least `@bitSizeOf(T) / 8`.
- takeVarInt
- Asserts the buffer was initialized with a capacity at least `n`.
- takeStructPointer
- Obtains an unaligned pointer to the beginning of the stream, reinterpreted
- peekStructPointer
- Obtains an unaligned pointer to the beginning of the stream, reinterpreted
- takeStruct
- Asserts the buffer was initialized with a capacity at least `@sizeOf(T)`.
- peekStruct
- Asserts the buffer was initialized with a capacity at least `@sizeOf(T)`.
- takeEnum
- Reads an integer with the same size as the given enum's tag type.
- takeEnumNonexhaustive
- Reads an integer with the same size as the given nonexhaustive enum's tag type.
- takeLeb128
- Read a single LEB128 value as type T, or `error.Overflow` if the value cannot fit.
- rebase
- Ensures `capacity` data can be buffered without rebasing.
- Hashed
- Provides a `Reader` implementation by passing data from an underlying
Error sets in this namespace
Error Sets
= .{ .vtable = &.{ .stream = failingStream, .discard = failingDiscard, }, .buffer = &.{}, .seek = 0, .end = 0, }
Values
Source
Implementation
pub fn readVec(r: *Reader, data: [][]u8) Error!usize {
var seek = r.seek;
for (data, 0..) |buf, i| {
const contents = r.buffer[seek..r.end];
const copy_len = @min(contents.len, buf.len);
@memcpy(buf[0..copy_len], contents[0..copy_len]);
seek += copy_len;
if (buf.len - copy_len == 0) continue;
// All of `buffer` has been copied to `data`.
const n = seek - r.seek;
r.seek = seek;
data[i] = buf[copy_len..];
defer data[i] = buf;
return n + (r.vtable.readVec(r, data[i..]) catch |err| switch (err) {
error.EndOfStream => if (n == 0) return error.EndOfStream else 0,
error.ReadFailed => return error.ReadFailed,
});
}
const n = seek - r.seek;
r.seek = seek;
return n;
}