Struct bytebuffer_new::ByteBuffer [−][src]
pub struct ByteBuffer { /* fields omitted */ }
A byte buffer object specifically turned to easily read and write binary values
Methods
impl ByteBuffer
[src]
impl ByteBuffer
ⓘImportant traits for ByteBufferpub fn new() -> ByteBuffer
[src]
pub fn new() -> ByteBuffer
Construct a new, empty, ByteBuffer
ⓘImportant traits for ByteBufferpub fn from_bytes(bytes: &[u8]) -> ByteBuffer
[src]
pub fn from_bytes(bytes: &[u8]) -> ByteBuffer
Construct a new ByteBuffer filled with the data array.
pub fn len(&self) -> usize
[src]
pub fn len(&self) -> usize
Return the buffer size
pub fn clear(&mut self)
[src]
pub fn clear(&mut self)
Clear the buffer and reinitialize the reading and writing cursor
pub fn resize(&mut self, size: usize)
[src]
pub fn resize(&mut self, size: usize)
Change the buffer size to size.
Note: You cannot shrink a buffer with this method
pub fn set_endian(&mut self, endian: Endian)
[src]
pub fn set_endian(&mut self, endian: Endian)
Set the byte order of the buffer
Note: By default the buffer uses big endian order
pub fn endian(&self) -> Endian
[src]
pub fn endian(&self) -> Endian
Returns the current byte order of the buffer
pub fn write_bytes(&mut self, bytes: &[u8])
[src]
pub 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]
pub fn write_u8(&mut self, val: u8)
[src]
pub 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]
pub fn write_i8(&mut self, val: i8)
[src]
pub fn write_i8(&mut self, val: i8)
Same as write_u8()
but for signed values
pub fn write_u16(&mut self, val: u16)
[src]
pub 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
pub fn write_i16(&mut self, val: i16)
[src]
pub fn write_i16(&mut self, val: i16)
Same as write_u16()
but for signed values
pub fn write_u32(&mut self, val: u32)
[src]
pub 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
pub fn write_i32(&mut self, val: i32)
[src]
pub fn write_i32(&mut self, val: i32)
Same as write_u32()
but for signed values
pub fn write_u64(&mut self, val: u64)
[src]
pub 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
pub fn write_i64(&mut self, val: i64)
[src]
pub fn write_i64(&mut self, val: i64)
Same as write_u64()
but for signed values
pub fn write_f32(&mut self, val: f32)
[src]
pub 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)
pub fn write_f64(&mut self, val: f64)
[src]
pub 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)
pub fn write_string(&mut self, val: &str)
[src]
pub 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")
pub fn read_bytes(&mut self, size: usize) -> Vec<u8>
[src]
pub 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
pub fn read_u8(&mut self) -> u8
[src]
pub 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
pub fn read_i8(&mut self) -> i8
[src]
pub fn read_i8(&mut self) -> i8
Same as read_u8()
but for signed values
pub fn read_u16(&mut self) -> u16
[src]
pub 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
pub fn read_i16(&mut self) -> i16
[src]
pub fn read_i16(&mut self) -> i16
Same as read_u16()
but for signed values
pub fn read_u32(&mut self) -> u32
[src]
pub 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
pub fn read_i32(&mut self) -> i32
[src]
pub fn read_i32(&mut self) -> i32
Same as read_u32()
but for signed values
pub fn read_u64(&mut self) -> u64
[src]
pub 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
pub fn read_i64(&mut self) -> i64
[src]
pub fn read_i64(&mut self) -> i64
Same as read_u64()
but for signed values
pub fn read_f32(&mut self) -> f32
[src]
pub fn read_f32(&mut self) -> f32
Read a 32 bits floating point value. The program crash if not enough bytes are available
pub fn read_f64(&mut self) -> f64
[src]
pub fn read_f64(&mut self) -> f64
Read a 64 bits floating point value. The program crash if not enough bytes are available
pub fn read_string(&mut self) -> String
[src]
pub 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.
pub fn to_string(&self) -> String
[src]
pub fn to_string(&self) -> String
Dump the byte buffer to a string.
pub fn get_rpos(&self) -> usize
[src]
pub fn get_rpos(&self) -> usize
Return the position of the reading cursor
pub fn set_rpos(&mut self, rpos: usize)
[src]
pub 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
pub fn get_wpos(&self) -> usize
[src]
pub fn get_wpos(&self) -> usize
Return the writing cursor position
pub fn set_wpos(&mut self, wpos: usize)
[src]
pub 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
pub fn to_bytes(&self) -> Vec<u8>
[src]
pub fn to_bytes(&self) -> Vec<u8>
Return the raw byte buffer.
pub fn read_bit(&mut self) -> bool
[src]
pub 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)
pub fn read_bits(&mut self, n: u8) -> u64
[src]
pub 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)
pub fn flush_bit(&mut self)
[src]
pub 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
^
pub fn write_bit(&mut self, bit: bool)
[src]
pub fn write_bit(&mut self, bit: bool)
Append 1 bit value to the buffer. The bit is happened like this :
...| XXXXXXXX | 10000000 |....
pub fn write_bits(&mut self, value: u64, n: u8)
[src]
pub 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]
impl Read for ByteBuffer
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
[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
unsafe fn initializer(&self) -> Initializer
[src]
unsafe fn initializer(&self) -> Initializer
read_initializer
)Determines if this Read
er 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]
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
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
1.0.0[src]
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
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
1.6.0[src]
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
Read the exact number of bytes required to fill buf
. Read more
fn by_ref(&mut self) -> &mut Self
1.0.0[src]
fn by_ref(&mut self) -> &mut Self
Creates a "by reference" adaptor for this instance of Read
. Read more
fn bytes(self) -> Bytes<Self>
1.0.0[src]
fn bytes(self) -> Bytes<Self>
Transforms this Read
instance to an [Iterator
] over its bytes. Read more
fn chars(self) -> Chars<Self>
[src]
fn chars(self) -> Chars<Self>
: Use str::from_utf8 instead: https://doc.rust-lang.org/nightly/std/str/struct.Utf8Error.html#examples
🔬 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[src]
fn chain<R>(self, next: R) -> Chain<Self, R> where
R: Read,
Creates an adaptor which will chain this stream with another. Read more
fn take(self, limit: u64) -> Take<Self>
1.0.0[src]
fn take(self, limit: u64) -> Take<Self>
Creates an adaptor which will read at most limit
bytes from it. Read more
impl Write for ByteBuffer
[src]
impl Write for ByteBuffer
fn write(&mut self, buf: &[u8]) -> Result<usize>
[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<()>
[src]
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[src]
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Attempts to write an entire buffer into this write. Read more
fn write_fmt(&mut self, fmt: Arguments) -> Result<(), Error>
1.0.0[src]
fn write_fmt(&mut self, fmt: Arguments) -> Result<(), Error>
Writes a formatted string into this writer, returning any error encountered. Read more
fn by_ref(&mut self) -> &mut Self
1.0.0[src]
fn by_ref(&mut self) -> &mut Self
Creates a "by reference" adaptor for this instance of Write
. Read more
impl Debug for ByteBuffer
[src]
impl Debug for ByteBuffer
Auto Trait Implementations
impl Send for ByteBuffer
impl Send for ByteBuffer
impl Sync for ByteBuffer
impl Sync for ByteBuffer