[−][src]Struct bytebuffer::ByteBuffer
A byte buffer object specifically turned to easily read and write binary values
Methods
impl ByteBuffer
[src][−]
ⓘImportant traits for ByteBufferpub fn new() -> ByteBuffer
[src][−]
Construct a new, empty, ByteBuffer
ⓘImportant traits for ByteBufferpub 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 Write for ByteBuffer
[src][+]
impl Read for ByteBuffer
[src][+]
Auto Trait Implementations
impl Send for ByteBuffer
impl Unpin for ByteBuffer
impl Sync for ByteBuffer
impl RefUnwindSafe for ByteBuffer
impl UnwindSafe for ByteBuffer
Blanket Implementations
impl<T> From<T> for T
[src][+]
impl<T, U> Into<U> for T where
U: From<T>,
[src][+]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src][+]
U: Into<T>,
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src][+]
U: TryFrom<T>,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src][+]
T: ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src][+]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src][+]
T: 'static + ?Sized,
impl<R> ReadBytesExt for R where
R: Read + ?Sized,
[src][+]
R: Read + ?Sized,
impl<W> WriteBytesExt for W where
W: Write + ?Sized,
[src][+]
W: Write + ?Sized,