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]

Important traits for ByteBuffer

Construct a new, empty, ByteBuffer

Important traits for ByteBuffer

Construct a new ByteBuffer filled with the data array.

Return the buffer size

Clear the buffer and reinitialize the reading and writing cursor

Change the buffer size to size.

Note: You cannot shrink a buffer with this method

Set the byte order of the buffer

Note: By default the buffer uses big endian order

Returns the current byte order of the buffer

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]

Append a byte (8 bits value) to the buffer

#Example

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

Same as write_u8() but for signed values

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

Same as write_u16() but for signed values

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

Same as write_u32() but for signed values

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

Same as write_u64() but for signed values

Append a 32 bits floating point number to the buffer.

#Example

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

Append a 64 bits floating point number to the buffer.

#Example

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

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")

Read a defined amount of raw bytes. The program crash if not enough bytes are available

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

Same as read_u8() but for signed values

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

Same as read_u16() but for signed values

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

Same as read_u32() but for signed values

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

Same as read_u64() but for signed values

Read a 32 bits floating point value. The program crash if not enough bytes are available

Read a 64 bits floating point value. The program crash if not enough bytes are available

Read a string.

Note : First it reads a 32 bits value representing the size, the read 'size' raw bytes.

Dump the byte buffer to a string.

Return the position of the reading cursor

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

Return the writing cursor position

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

Return the raw byte buffer.

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)

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)

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
           ^

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

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

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]

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

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

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

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

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

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

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

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

Deprecated since 1.27.0

: 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

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

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

impl Write for ByteBuffer
[src]

Write a buffer into this object, returning how many bytes were written. Read more

Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more

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

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

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

impl Debug for ByteBuffer
[src]

Formats the value using the given formatter. Read more

Auto Trait Implementations

impl Send for ByteBuffer

impl Sync for ByteBuffer