pub struct ByteStream { /* private fields */ }Expand description
A position-tracked binary stream reader.
ByteStream wraps binary data and provides sequential reading operations
while tracking the current position. It supports creating slices (views)
into the data for parsing nested structures without copying.
§Design
The stream uses Arc<[u8]> internally, allowing cheap cloning and slicing
without data duplication. Position tracking is relative to the slice,
with abs_pos() providing the absolute position in the original data.
§Example
use gpg_inspector_lib::ByteStream;
let data = vec![0x01, 0x02, 0x03, 0x04];
let mut stream = ByteStream::new(data);
assert_eq!(stream.octet().unwrap(), 0x01);
assert_eq!(stream.pos(), 1);
assert_eq!(stream.remaining(), 3);Implementations§
Source§impl ByteStream
impl ByteStream
Sourcepub fn new(bytes: Vec<u8>) -> Self
pub fn new(bytes: Vec<u8>) -> Self
Creates a new stream from a vector of bytes.
The stream will own the data and start reading from position 0.
Sourcepub fn from_arc(bytes: Arc<[u8]>) -> Self
pub fn from_arc(bytes: Arc<[u8]>) -> Self
Creates a new stream from an Arc<[u8]>.
Useful when sharing the same data across multiple streams or
when the data is already in an Arc.
Sourcepub fn slice(&self, start: usize, end: usize) -> Self
pub fn slice(&self, start: usize, end: usize) -> Self
Creates a slice (view) into this stream’s data.
The slice shares the underlying data but has its own position tracker
starting at 0. The start and end parameters are relative to the
current stream’s bounds.
This is useful for parsing nested packet structures where you want to limit reads to a specific byte range.
Sourcepub fn pos(&self) -> usize
pub fn pos(&self) -> usize
Returns the current position within this stream (relative to start).
Sourcepub fn abs_pos(&self) -> usize
pub fn abs_pos(&self) -> usize
Returns the absolute position in the original data.
For slices, this accounts for the slice’s offset from the beginning of the original data.
Sourcepub fn octet(&mut self) -> Result<u8>
pub fn octet(&mut self) -> Result<u8>
Reads a single byte and advances the position.
§Errors
Returns Error::UnexpectedEnd if no bytes remain.
Sourcepub fn peek(&self) -> Option<u8>
pub fn peek(&self) -> Option<u8>
Returns the next byte without advancing the position.
Returns None if no bytes remain.
Sourcepub fn uint16(&mut self) -> Result<u16>
pub fn uint16(&mut self) -> Result<u16>
Reads a big-endian 16-bit unsigned integer.
§Errors
Returns Error::UnexpectedEnd if fewer than 2 bytes remain.
Sourcepub fn uint32(&mut self) -> Result<u32>
pub fn uint32(&mut self) -> Result<u32>
Reads a big-endian 32-bit unsigned integer.
§Errors
Returns Error::UnexpectedEnd if fewer than 4 bytes remain.
Sourcepub fn bytes(&mut self, count: usize) -> Result<Vec<u8>>
pub fn bytes(&mut self, count: usize) -> Result<Vec<u8>>
Reads count bytes and returns them as a vector.
§Errors
Returns Error::UnexpectedEnd if fewer than count bytes remain.
Sourcepub fn hex(&mut self, count: usize) -> Result<String>
pub fn hex(&mut self, count: usize) -> Result<String>
Reads count bytes and returns them as an uppercase hex string.
§Errors
Returns Error::UnexpectedEnd if fewer than count bytes remain.
Sourcepub fn utf8(&mut self, count: usize) -> Result<String>
pub fn utf8(&mut self, count: usize) -> Result<String>
Reads count bytes and interprets them as UTF-8.
Invalid UTF-8 sequences are replaced with the Unicode replacement character.
§Errors
Returns Error::UnexpectedEnd if fewer than count bytes remain.
Sourcepub fn rest(&mut self) -> Vec<u8> ⓘ
pub fn rest(&mut self) -> Vec<u8> ⓘ
Reads all remaining bytes and returns them as a vector.
After this call, remaining() will return 0.
Sourcepub fn rest_as_hex(&mut self) -> String
pub fn rest_as_hex(&mut self) -> String
Reads all remaining bytes and returns them as an uppercase hex string.
Sourcepub fn multi_precision_integer(&mut self) -> Result<(u16, String)>
pub fn multi_precision_integer(&mut self) -> Result<(u16, String)>
Reads an OpenPGP Multi-Precision Integer (MPI).
MPIs are stored as a 16-bit big-endian bit count followed by the integer bytes (big-endian, minimum length for the value).
Returns the bit length and the value as a hex string.
§Errors
Returns Error::UnexpectedEnd if the data is truncated.
Sourcepub fn variable_length(&mut self) -> Result<usize>
pub fn variable_length(&mut self) -> Result<usize>
Reads an OpenPGP variable-length value.
This is the new-format packet length encoding:
- 0-191: one byte, literal value
- 192-254: two bytes,
((first - 192) << 8) + second + 192 - 255: five bytes, 32-bit big-endian length
§Errors
Returns Error::UnexpectedEnd if the data is truncated.
Trait Implementations§
Source§impl Clone for ByteStream
impl Clone for ByteStream
Source§fn clone(&self) -> ByteStream
fn clone(&self) -> ByteStream
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more