Struct bytebuffer::ByteBuffer
[−]
[src]
pub struct ByteBuffer { /* fields omitted */ }
A byte buffer object specifically turned to easily read and write binary values
Methods
impl ByteBuffer
[src]
fn new() -> ByteBuffer
Construct a new, empty, ByteBuffer
fn from_bytes(bytes: &[u8]) -> ByteBuffer
Construct a new ByteBuffer filled with the data array.
fn len(&self) -> usize
Return the buffer size
fn clear(&mut self)
Clear the buffer and reinitialize the reading and writing cursor
fn resize(&mut self, size: usize)
Change the buffer size to size.
Note: You cannot shrink a buffer with this method
fn write_bytes(&mut self, bytes: &[u8])
Append a byte array to the buffer. The buffer is automatically extended if needed
Example
let mut buffer = ByteBuffer::new(); buffer.write_bytes(&vec![0x1, 0xFF, 0x45]); // buffer contains [0x1, 0xFF, 0x45]
fn write_u8(&mut self, val: u8)
Append a byte (8 bits value) to the buffer
Example
let mut buffer = ByteBuffer::new(); buffer.write_u8(1) // buffer contains [0x1]
fn write_i8(&mut self, val: i8)
Same as write_u8()
but for signed values
fn write_u16(&mut self, val: u16)
Append a word (16 bits value) to the buffer
Example
let mut buffer = ByteBuffer::new(); buffer.write_u16(1) // buffer contains [0x00, 0x1] if little endian
fn write_i16(&mut self, val: i16)
Same as write_u16()
but for signed values
fn write_u32(&mut self, val: u32)
Append a double word (32 bits value) to the buffer
Example
let mut buffer = ByteBuffer::new(); buffer.write_u32(1) // buffer contains [0x00, 0x00, 0x00, 0x1] if little endian
fn write_i32(&mut self, val: i32)
Same as write_u32()
but for signed values
fn write_u64(&mut self, val: u64)
Append a quaddruple word (64 bits value) to the buffer
Example
let mut buffer = ByteBuffer::new(); buffer.write_u64(1) // buffer contains [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1] if little endian
fn write_i64(&mut self, val: i64)
Same as write_u64()
but for signed values
fn write_f32(&mut self, val: f32)
Append a 32 bits floating point number to the buffer.
Example
let mut buffer = ByteBuffer::new(); buffer.write_f32(0.1)
fn write_f64(&mut self, val: f64)
Append a 64 bits floating point number to the buffer.
Example
let mut buffer = ByteBuffer::new(); buffer.write_f64(0.1)
fn write_string(&mut self, val: &str)
Append a string to the buffer.
Format The format is (u32)size + size * (u8)characters
Exapmle
let mut buffer = ByteBuffer::new(); buffer.write_string("Hello")
fn read_bytes(&mut self, size: usize) -> Vec<u8>
Read a defined amount of raw bytes. The program crash if not enough bytes are available
fn read_u8(&mut self) -> u8
Read one byte. The program crash if not enough bytes are available
Example
let mut buffer = ByteBuffer::from_bytes(&vec![0x1]); let value = buffer.read_u8(); //Value contains 1
fn read_i8(&mut self) -> i8
Same as read_u8()
but for signed values
fn read_u16(&mut self) -> u16
Read a 2-bytes long value. The program crash if not enough bytes are available
Example
let mut buffer = ByteBuffer::from_bytes(&vec![0x0, 0x1]); let value = buffer.read_u16(); //Value contains 1
fn read_i16(&mut self) -> i16
Same as read_u16()
but for signed values
fn read_u32(&mut self) -> u32
Read a four-bytes long value. The program crash if not enough bytes are available
Example
let mut buffer = ByteBuffer::from_bytes(&vec![0x0, 0x0, 0x0, 0x1]); let value = buffer.read_u32(); // Value contains 1
fn read_i32(&mut self) -> i32
Same as read_u32()
but for signed values
fn read_u64(&mut self) -> u64
Read an eight bytes long value. The program crash if not enough bytes are available
Example
let mut buffer = ByteBuffer::from_bytes(&vec![0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1]); let value = buffer.read_u64(); //Value contains 1
fn read_i64(&mut self) -> i64
Same as read_u64()
but for signed values
fn read_f32(&mut self) -> f32
Read a 32 bits floating point value. The program crash if not enough bytes are available
fn read_f64(&mut self) -> f64
Read a 64 bits floating point value. The program crash if not enough bytes are available
fn read_string(&mut self) -> String
Read a string.
Note : First it reads a 32 bits value representing the size, the read 'size' raw bytes.
fn to_string(&self) -> String
Dump the byte buffer to a string.
fn get_rpos(&self) -> usize
Return the position of the reading cursor
fn set_rpos(&mut self, rpos: usize)
Set the reading cursor position.
Note : Set the reading cursor to min(newPosition, self.len())
to prevent overflow
fn get_wpos(&self) -> usize
Return the writing cursor position
fn set_wpos(&mut self, wpos: usize)
Set the writing cursor position.
Note : Set the writing cursor to min(newPosition, self.len())
to prevent overflow
fn to_bytes(&self) -> Vec<u8>
Return the raw byte buffer.
fn read_bit(&mut self) -> 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 mut buffer = ByteBuffer::from_bytes(&vec![128]); // 10000000b let value1 = buffer.read_bit(); //value1 contains true (eg: bit is 1) let value2 = buffer.read_bit(); //value2 contains false (eg: bit is 0)
fn read_bits(&mut self, n: u8) -> u64
Read n bits. an return the corresponding value an u64.
Note 1 : We cannot read more than 64 bits
Note 2 Bits are read from left to right
Example
let mut buffer = ByteBuffer::from_bytes(&vec![128]); // 10000000b let value = buffer.read_bits(3); // value contains 4 (eg: 100b)
fn flush_bit(&mut self)
Discard all the pending bits available for reading or writing and place the the corresponding cursor to the next byte.
Note 1 : If no bits are currently read or written, this function does nothing. Note 2 : This function is automatically called for each write or read operations.
Example
10010010 | 00000001
^
10010010 | 00000001 // read_bit called
^
10010010 | 00000001 // flush_bit() called
^
fn write_bit(&mut self, bit: bool)
Append 1 bit value to the buffer. The bit is happened like this :
...| XXXXXXXX | 10000000 |....
fn write_bits(&mut self, value: u64, n: u8)
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 Read for ByteBuffer
[src]
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
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
1.0.0
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
Read all bytes until EOF in this source, placing them into buf
. Read more
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
1.6.0
Read the exact number of bytes required to fill buf
. Read more
fn by_ref(&mut self) -> &mut Self
1.0.0
Creates a "by reference" adaptor for this instance of Read
. Read more
fn bytes(self) -> Bytes<Self>
1.0.0
Transforms this Read
instance to an Iterator
over its bytes. Read more
fn chars(self) -> Chars<Self>
🔬 This is a nightly-only experimental API. (io
)
the semantics of a partial read/write of where errors happen is currently unclear and may change
Transforms this Read
instance to an Iterator
over char
s. Read more
fn chain<R>(self, next: R) -> Chain<Self, R> where
R: Read,
1.0.0
R: Read,
Creates an adaptor which will chain this stream with another. Read more
fn take(self, limit: u64) -> Take<Self>
1.0.0
Creates an adaptor which will read at most limit
bytes from it. Read more
impl Write for ByteBuffer
[src]
fn write(&mut self, buf: &[u8]) -> Result<usize>
Write a buffer into this object, returning how many bytes were written. Read more
fn flush(&mut self) -> Result<()>
Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
1.0.0
Attempts to write an entire buffer into this write. Read more
fn write_fmt(&mut self, fmt: Arguments) -> Result<(), Error>
1.0.0
Writes a formatted string into this writer, returning any error encountered. Read more
fn by_ref(&mut self) -> &mut Self
1.0.0
Creates a "by reference" adaptor for this instance of Write
. Read more