[][src]Struct bytebuffer::ByteBuffer

pub struct ByteBuffer { /* fields omitted */ }

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

Methods

impl ByteBuffer[src]

Important traits for ByteBuffer
pub fn new() -> ByteBuffer[src]

Construct a new, empty, ByteBuffer

Important traits for ByteBuffer
pub fn from_bytes(bytes: &[u8]) -> ByteBuffer[src]

Construct a new ByteBuffer filled with the data array.

pub fn len(&self) -> usize[src]

Return the buffer size

pub fn is_empty(&self) -> bool[src]

pub fn clear(&mut self)[src]

Clear the buffer and reinitialize the reading and writing cursor

pub fn resize(&mut self, size: usize)[src]

Change the buffer size to size.

Note: You cannot shrink a buffer with this method

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

Set the byte order of the buffer

Note: By default the buffer uses big endian order

pub fn endian(&self) -> Endian[src]

Returns the current byte order of the buffer

pub fn write_bytes(&mut self, bytes: &[u8])[src]

Append a byte array to the buffer. The buffer is automatically extended if needed Note: This method resets the read and write cursor for bitwise reading.

#Example

let mut buffer = ByteBuffer::new();
buffer.write_bytes(&vec![0x1, 0xFF, 0x45]); // buffer contains [0x1, 0xFF, 0x45]

pub fn write_u8(&mut self, val: u8)[src]

Append a byte (8 bits value) to the buffer Note: This method resets the read and write cursor for bitwise reading.

#Example

let mut buffer = ByteBuffer::new();
buffer.write_u8(1) // buffer contains [0x1]

pub fn write_i8(&mut self, val: i8)[src]

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

pub fn write_u16(&mut self, val: u16)[src]

Append a word (16 bits value) to the buffer Note: This method resets the read and write cursor for bitwise reading.

#Example

let mut buffer = ByteBuffer::new();
buffer.write_u16(1) // buffer contains [0x00, 0x1] if little endian

pub fn write_i16(&mut self, val: i16)[src]

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

pub fn write_u32(&mut self, val: u32)[src]

Append a double word (32 bits value) to the buffer Note: This method resets the read and write cursor for bitwise reading.

#Example

let mut buffer = ByteBuffer::new();
buffer.write_u32(1) // buffer contains [0x00, 0x00, 0x00, 0x1] if little endian

pub fn write_i32(&mut self, val: i32)[src]

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

pub fn write_u64(&mut self, val: u64)[src]

Append a quaddruple word (64 bits value) to the buffer Note: This method resets the read and write cursor for bitwise reading.

#Example

let mut buffer = ByteBuffer::new();
buffer.write_u64(1) // buffer contains [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1] if little endian

pub fn write_i64(&mut self, val: i64)[src]

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

pub fn write_f32(&mut self, val: f32)[src]

Append a 32 bits floating point number to the buffer. Note: This method resets the read and write cursor for bitwise reading.

#Example

let mut buffer = ByteBuffer::new();
buffer.write_f32(0.1)

pub fn write_f64(&mut self, val: f64)[src]

Append a 64 bits floating point number to the buffer. Note: This method resets the read and write cursor for bitwise reading.

#Example

let mut buffer = ByteBuffer::new();
buffer.write_f64(0.1)

pub fn write_string(&mut self, val: &str)[src]

Append a string to the buffer. Note: This method resets the read and write cursor for bitwise reading.

Format The format is (u32)size + size * (u8)characters

#Example

let mut buffer = ByteBuffer::new();
buffer.write_string("Hello")

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

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.

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

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 mut buffer = ByteBuffer::from_bytes(&vec![0x1]);
let value = buffer.read_u8().unwrap(); //Value contains 1

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

Same as read_u8() but for signed values

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

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 mut buffer = ByteBuffer::from_bytes(&vec![0x0, 0x1]);
let value = buffer.read_u16().unwrap(); //Value contains 1

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

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

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

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 mut buffer = ByteBuffer::from_bytes(&vec![0x0, 0x0, 0x0, 0x1]);
let value = buffer.read_u32().unwrap(); // Value contains 1

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

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

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

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 mut buffer = ByteBuffer::from_bytes(&vec![0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1]);
let value = buffer.read_u64().unwrap(); //Value contains 1

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

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

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

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.

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

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.

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

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.

pub fn to_string(&self) -> String[src]

Dump the byte buffer to a string.

pub fn get_rpos(&self) -> usize[src]

Return the position of the reading cursor

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

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

pub fn get_wpos(&self) -> usize[src]

Return the writing cursor position

pub fn set_wpos(&mut self, wpos: usize)[src]

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

pub fn to_bytes(&self) -> Vec<u8>[src]

Return the raw byte buffer.

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

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 mut buffer = ByteBuffer::from_bytes(&vec![128]); // 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)

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

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 mut buffer = ByteBuffer::from_bytes(&vec![128]); // 10000000b
let value = buffer.read_bits(3).unwrap(); // value contains 4 (eg: 100b)

pub fn flush_bit(&mut self)[src]

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

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

#Example

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

pub fn write_bit(&mut self, bit: bool)[src]

Append 1 bit value to the buffer. The bit is appended like this :

...| XXXXXXXX | 10000000 |....

pub fn write_bits(&mut self, value: u64, n: u8)[src]

Write the given value as a sequence of n bits

#Example

let mut buffer = ByteBuffer::new();
buffer.write_bits(4, 3); // append 100b

Trait Implementations

impl Default for ByteBuffer[src]

impl Debug for ByteBuffer[src]

impl Read for ByteBuffer[src]

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

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

unsafe fn initializer(&self) -> Initializer[src]

🔬 This is a nightly-only experimental API. (read_initializer)

Determines if this Reader can work with buffers of uninitialized memory. Read more

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

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

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

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

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

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

fn by_ref(&mut self) -> &mut Self1.0.0[src]

Creates a "by reference" adaptor for this instance of Read. Read more

fn bytes(self) -> Bytes<Self>1.0.0[src]

Transforms this Read instance to an [Iterator] over its bytes. Read more

fn chain<R>(self, next: R) -> Chain<Self, R> where
    R: Read
1.0.0[src]

Creates an adaptor which will chain this stream with another. Read more

fn take(self, limit: u64) -> Take<Self>1.0.0[src]

Creates an adaptor which will read at most limit bytes from it. Read more

impl Write for ByteBuffer[src]

fn write_vectored(&mut self, bufs: &[IoSlice]) -> Result<usize, Error>1.36.0[src]

Like write, except that it writes from a slice of buffers. Read more

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

Attempts to write an entire buffer into this writer. Read more

fn write_fmt(&mut self, fmt: Arguments) -> Result<(), Error>1.0.0[src]

Writes a formatted string into this writer, returning any error encountered. Read more

fn by_ref(&mut self) -> &mut Self1.0.0[src]

Creates a "by reference" adaptor for this instance of Write. Read more

Auto Trait Implementations

Blanket Implementations

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<R> ReadBytesExt for R where
    R: Read + ?Sized
[src]

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

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

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

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

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

Reads an unsigned 16 bit integer from the underlying reader.

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

Reads a signed 16 bit integer from the underlying reader.

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

Reads an unsigned 32 bit integer from the underlying reader.

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

Reads a signed 32 bit integer from the underlying reader.

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

Reads an unsigned 64 bit integer from the underlying reader.

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

Reads a signed 64 bit integer from the underlying reader.

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

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

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

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

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

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

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

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

impl<W> WriteBytesExt for W where
    W: Write + ?Sized
[src]

fn write_u8(&mut self, n: u8) -> Result<(), Error>[src]

Writes an unsigned 8 bit integer to the underlying writer. Read more

fn write_i8(&mut self, n: i8) -> Result<(), Error>[src]

Writes a signed 8 bit integer to the underlying writer. Read more

fn write_u16<T>(&mut self, n: u16) -> Result<(), Error> where
    T: ByteOrder
[src]

Writes an unsigned 16 bit integer to the underlying writer.

fn write_i16<T>(&mut self, n: i16) -> Result<(), Error> where
    T: ByteOrder
[src]

Writes a signed 16 bit integer to the underlying writer.

fn write_u32<T>(&mut self, n: u32) -> Result<(), Error> where
    T: ByteOrder
[src]

Writes an unsigned 32 bit integer to the underlying writer.

fn write_i32<T>(&mut self, n: i32) -> Result<(), Error> where
    T: ByteOrder
[src]

Writes a signed 32 bit integer to the underlying writer.

fn write_u64<T>(&mut self, n: u64) -> Result<(), Error> where
    T: ByteOrder
[src]

Writes an unsigned 64 bit integer to the underlying writer.

fn write_i64<T>(&mut self, n: i64) -> Result<(), Error> where
    T: ByteOrder
[src]

Writes a signed 64 bit integer to the underlying writer.

fn write_f32<T>(&mut self, n: f32) -> Result<(), Error> where
    T: ByteOrder
[src]

Writes a IEEE754 single-precision (4 bytes) floating point number to the underlying writer. Read more

fn write_f64<T>(&mut self, n: f64) -> Result<(), Error> where
    T: ByteOrder
[src]

Writes a IEEE754 double-precision (8 bytes) floating point number to the underlying writer. Read more