Struct bytebuffer::ByteReader

source ·
pub struct ByteReader<'a> { /* private fields */ }
Expand description

A byte buffer object specifically turned to easily read and write binary values

Implementations§

source§

impl<'a> ByteReader<'a>

source

pub fn from_bytes(bytes: &[u8]) -> ByteReader<'_>

Construct a new ByteReader filled with the data array.

source

pub fn len(&self) -> usize

Return the buffer size

source

pub fn is_empty(&self) -> bool

source

pub fn reset_cursors(&mut self)

Reinitialize the reading cursor

source

pub fn reset_bits_cursors(&mut self)

Reinitialize the bit reading cursor

source

pub fn set_endian(&mut self, endian: Endian)

Set the byte order of the buffer

Note: By default the buffer uses big endian order

source

pub fn endian(&self) -> Endian

Returns the current byte order of the buffer

source

pub fn read_bytes(&mut self, size: usize) -> Result<Vec<u8>>

Read a defined amount of raw bytes, or return an IO error if not enough bytes are available. Note: This method resets the read and write cursor for bitwise reading.

source

pub fn read_u8(&mut self) -> Result<u8>

Read one byte, or return an IO error if not enough bytes are available. Note: This method resets the read and write cursor for bitwise reading.

#Example

let data = vec![0x1];
let mut buffer = ByteReader::from_bytes(&data);
let value = buffer.read_u8().unwrap(); //Value contains 1
source

pub fn read_i8(&mut self) -> Result<i8>

Same as read_u8() but for signed values

source

pub fn read_u16(&mut self) -> Result<u16>

Read a 2-bytes long value, or return an IO error if not enough bytes are available. Note: This method resets the read and write cursor for bitwise reading.

#Example

let data = vec![0x0, 0x1];
let mut buffer = ByteReader::from_bytes(&data);
let value = buffer.read_u16().unwrap(); //Value contains 1
source

pub fn read_i16(&mut self) -> Result<i16>

Same as read_u16() but for signed values Note: This method resets the read and write cursor for bitwise reading.

source

pub fn read_u32(&mut self) -> Result<u32>

Read a four-bytes long value, or return an IO error if not enough bytes are available. Note: This method resets the read and write cursor for bitwise reading.

#Example

let data = vec![0x0, 0x0, 0x0, 0x1];
let mut buffer = ByteReader::from_bytes(&data);
let value = buffer.read_u32().unwrap(); // Value contains 1
source

pub fn read_i32(&mut self) -> Result<i32>

Same as read_u32() but for signed values Note: This method resets the read and write cursor for bitwise reading.

source

pub fn read_u64(&mut self) -> Result<u64>

Read an eight bytes long value, or return an IO error if not enough bytes are available. Note: This method resets the read and write cursor for bitwise reading.

#Example

let data = vec![0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1];
let mut buffer = ByteReader::from_bytes(&data);
let value = buffer.read_u64().unwrap(); //Value contains 1
source

pub fn read_i64(&mut self) -> Result<i64>

Same as read_u64() but for signed values Note: This method resets the read and write cursor for bitwise reading.

source

pub fn read_f32(&mut self) -> Result<f32>

Read a 32 bits floating point value, or return an IO error if not enough bytes are available. Note: This method resets the read and write cursor for bitwise reading.

source

pub fn read_f64(&mut self) -> Result<f64>

Read a 64 bits floating point value, or return an IO error if not enough bytes are available. Note: This method resets the read and write cursor for bitwise reading.

source

pub fn read_string(&mut self) -> Result<String>

Read a string.

Note: First it reads a 32 bits value representing the size, then ‘size’ raw bytes that must be encoded as UTF8. Note: This method resets the read and write cursor for bitwise reading.

source

pub fn to_hex_dump(&self) -> String

Dump the byte buffer to a string.

source

pub fn get_rpos(&self) -> usize

Return the position of the reading cursor

source

pub fn set_rpos(&mut self, rpos: usize)

Set the reading cursor position. Note: Sets the reading cursor to min(newPosition, self.len()) to prevent overflow

source

pub fn as_bytes(&self) -> &[u8]

Return the raw byte buffer bytes.

source

pub fn read_bit(&mut self) -> Result<bool>

Read 1 bit. Return true if the bit is set to 1, otherwhise, return false.

Note: Bits are read from left to right

#Example

let data = vec![128];
let mut buffer = ByteReader::from_bytes(&data); // 10000000b
let value1 = buffer.read_bit().unwrap(); //value1 contains true (eg: bit is 1)
let value2 = buffer.read_bit().unwrap(); //value2 contains false (eg: bit is 0)
source

pub fn read_bits(&mut self, n: u8) -> Result<u64>

Read n bits. an return the corresponding value an u64.

Note: We cannot read more than 64 bits

Note: Bits are read from left to right

#Example

let data = vec![128];
let mut buffer = ByteReader::from_bytes(&data); // 10000000b
let value = buffer.read_bits(3).unwrap(); // value contains 4 (eg: 100b)
source

pub fn flush_bits(&mut self)

Discard all the pending bits available for reading and place the corresponding cursor to the next byte.

Note: If no bits are currently read, this function does nothing.

#Example

10010010 | 00000001
^
10010010 | 00000001 // read_bit called
 ^
10010010 | 00000001 // flush_bit() called
           ^

Trait Implementations§

source§

impl<'a> Clone for ByteReader<'a>

source§

fn clone(&self) -> ByteReader<'a>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'a> Debug for ByteReader<'a>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a> From<&'a [u8]> for ByteReader<'a>

source§

fn from(val: &'a [u8]) -> Self

Converts to this type from the input type.
source§

impl<'a> Hash for ByteReader<'a>

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<'a> PartialEq for ByteReader<'a>

source§

fn eq(&self, other: &ByteReader<'a>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> Read for ByteReader<'a>

source§

fn read(&mut self, buf: &mut [u8]) -> Result<usize>

Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
1.36.0 · source§

fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>

Like read, except that it reads into a slice of buffers. Read more
source§

fn is_read_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
1.0.0 · source§

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>

Read all bytes until EOF in this source, placing them into buf. Read more
1.0.0 · source§

fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>

Read all bytes until EOF in this source, appending them to buf. Read more
1.6.0 · source§

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>

Read the exact number of bytes required to fill buf. Read more
source§

fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
source§

fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Read the exact number of bytes required to fill cursor. Read more
1.0.0 · source§

fn by_ref(&mut self) -> &mut Selfwhere Self: Sized,

Creates a “by reference” adaptor for this instance of Read. Read more
1.0.0 · source§

fn bytes(self) -> Bytes<Self>where Self: Sized,

Transforms this Read instance to an Iterator over its bytes. Read more
1.0.0 · source§

fn chain<R>(self, next: R) -> Chain<Self, R>where R: Read, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
1.0.0 · source§

fn take(self, limit: u64) -> Take<Self>where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more
source§

impl<'a> Eq for ByteReader<'a>

source§

impl<'a> StructuralEq for ByteReader<'a>

source§

impl<'a> StructuralPartialEq for ByteReader<'a>

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for ByteReader<'a>

§

impl<'a> Send for ByteReader<'a>

§

impl<'a> Sync for ByteReader<'a>

§

impl<'a> Unpin for ByteReader<'a>

§

impl<'a> UnwindSafe for ByteReader<'a>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<R> ReadBytesExt for Rwhere R: Read + ?Sized,

source§

fn read_u8(&mut self) -> Result<u8, Error>

Reads an unsigned 8 bit integer from the underlying reader. Read more
source§

fn read_i8(&mut self) -> Result<i8, Error>

Reads a signed 8 bit integer from the underlying reader. Read more
source§

fn read_u16<T>(&mut self) -> Result<u16, Error>where T: ByteOrder,

Reads an unsigned 16 bit integer from the underlying reader. Read more
source§

fn read_i16<T>(&mut self) -> Result<i16, Error>where T: ByteOrder,

Reads a signed 16 bit integer from the underlying reader. Read more
source§

fn read_u24<T>(&mut self) -> Result<u32, Error>where T: ByteOrder,

Reads an unsigned 24 bit integer from the underlying reader. Read more
source§

fn read_i24<T>(&mut self) -> Result<i32, Error>where T: ByteOrder,

Reads a signed 24 bit integer from the underlying reader. Read more
source§

fn read_u32<T>(&mut self) -> Result<u32, Error>where T: ByteOrder,

Reads an unsigned 32 bit integer from the underlying reader. Read more
source§

fn read_i32<T>(&mut self) -> Result<i32, Error>where T: ByteOrder,

Reads a signed 32 bit integer from the underlying reader. Read more
source§

fn read_u48<T>(&mut self) -> Result<u64, Error>where T: ByteOrder,

Reads an unsigned 48 bit integer from the underlying reader. Read more
source§

fn read_i48<T>(&mut self) -> Result<i64, Error>where T: ByteOrder,

Reads a signed 48 bit integer from the underlying reader. Read more
source§

fn read_u64<T>(&mut self) -> Result<u64, Error>where T: ByteOrder,

Reads an unsigned 64 bit integer from the underlying reader. Read more
source§

fn read_i64<T>(&mut self) -> Result<i64, Error>where T: ByteOrder,

Reads a signed 64 bit integer from the underlying reader. Read more
source§

fn read_u128<T>(&mut self) -> Result<u128, Error>where T: ByteOrder,

Reads an unsigned 128 bit integer from the underlying reader. Read more
source§

fn read_i128<T>(&mut self) -> Result<i128, Error>where T: ByteOrder,

Reads a signed 128 bit integer from the underlying reader. Read more
source§

fn read_uint<T>(&mut self, nbytes: usize) -> Result<u64, Error>where T: ByteOrder,

Reads an unsigned n-bytes integer from the underlying reader. Read more
source§

fn read_int<T>(&mut self, nbytes: usize) -> Result<i64, Error>where T: ByteOrder,

Reads a signed n-bytes integer from the underlying reader. Read more
source§

fn read_uint128<T>(&mut self, nbytes: usize) -> Result<u128, Error>where T: ByteOrder,

Reads an unsigned n-bytes integer from the underlying reader.
source§

fn read_int128<T>(&mut self, nbytes: usize) -> Result<i128, Error>where T: ByteOrder,

Reads a signed n-bytes integer from the underlying reader.
source§

fn read_f32<T>(&mut self) -> Result<f32, Error>where T: ByteOrder,

Reads a IEEE754 single-precision (4 bytes) floating point number from the underlying reader. Read more
source§

fn read_f64<T>(&mut self) -> Result<f64, Error>where T: ByteOrder,

Reads a IEEE754 double-precision (8 bytes) floating point number from the underlying reader. Read more
source§

fn read_u16_into<T>(&mut self, dst: &mut [u16]) -> Result<(), Error>where T: ByteOrder,

Reads a sequence of unsigned 16 bit integers from the underlying reader. Read more
source§

fn read_u32_into<T>(&mut self, dst: &mut [u32]) -> Result<(), Error>where T: ByteOrder,

Reads a sequence of unsigned 32 bit integers from the underlying reader. Read more
source§

fn read_u64_into<T>(&mut self, dst: &mut [u64]) -> Result<(), Error>where T: ByteOrder,

Reads a sequence of unsigned 64 bit integers from the underlying reader. Read more
source§

fn read_u128_into<T>(&mut self, dst: &mut [u128]) -> Result<(), Error>where T: ByteOrder,

Reads a sequence of unsigned 128 bit integers from the underlying reader. Read more
source§

fn read_i8_into(&mut self, dst: &mut [i8]) -> Result<(), Error>

Reads a sequence of signed 8 bit integers from the underlying reader. Read more
source§

fn read_i16_into<T>(&mut self, dst: &mut [i16]) -> Result<(), Error>where T: ByteOrder,

Reads a sequence of signed 16 bit integers from the underlying reader. Read more
source§

fn read_i32_into<T>(&mut self, dst: &mut [i32]) -> Result<(), Error>where T: ByteOrder,

Reads a sequence of signed 32 bit integers from the underlying reader. Read more
source§

fn read_i64_into<T>(&mut self, dst: &mut [i64]) -> Result<(), Error>where T: ByteOrder,

Reads a sequence of signed 64 bit integers from the underlying reader. Read more
source§

fn read_i128_into<T>(&mut self, dst: &mut [i128]) -> Result<(), Error>where T: ByteOrder,

Reads a sequence of signed 128 bit integers from the underlying reader. Read more
source§

fn read_f32_into<T>(&mut self, dst: &mut [f32]) -> Result<(), Error>where T: ByteOrder,

Reads a sequence of IEEE754 single-precision (4 bytes) floating point numbers from the underlying reader. Read more
source§

fn read_f32_into_unchecked<T>(&mut self, dst: &mut [f32]) -> Result<(), Error>where T: ByteOrder,

👎Deprecated since 1.2.0: please use read_f32_into instead
DEPRECATED. Read more
source§

fn read_f64_into<T>(&mut self, dst: &mut [f64]) -> Result<(), Error>where T: ByteOrder,

Reads a sequence of IEEE754 double-precision (8 bytes) floating point numbers from the underlying reader. Read more
source§

fn read_f64_into_unchecked<T>(&mut self, dst: &mut [f64]) -> Result<(), Error>where T: ByteOrder,

👎Deprecated since 1.2.0: please use read_f64_into instead
DEPRECATED. Read more
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.