Struct bytebuffer::ByteBuffer

source ·
pub struct ByteBuffer { /* private fields */ }
Expand description

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

Implementations

Construct a new, empty, ByteBuffer

Construct a new ByteBuffer filled with the data array.

Constructs a new ByteBuffer from an existing vector. This function takes ownership of the vector

Return the buffer size

Clear the buffer and reinitialize the reading and writing cursors

Reinitialize the reading and writing cursor

Reinitialize the bit reading and bit 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 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]

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]

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

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

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

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

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

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

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

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)

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)

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

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.

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

Same as read_u8() but for signed values

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

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

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

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

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

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

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.

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.

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.

Dump the byte buffer to a string.

Return the position of the reading cursor

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

Return the writing cursor position

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

Return the raw byte buffer bytes.

Return the raw byte buffer as a Vec.

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)

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)

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
           ^

Append 1 bit value to the buffer. The bit is appended 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

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
Like read, except that it reads into a slice of buffers. Read more
🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. 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
🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
🔬This is a nightly-only experimental API. (read_buf)
Read the exact number of bytes required to fill cursor. 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
Creates an adapter which will chain this stream with another. Read more
Creates an adapter which will read at most limit bytes from it. Read more
Write a buffer into this writer, returning how many bytes were written. Read more
Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
Like write, except that it writes from a slice of buffers. Read more
🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
Attempts to write an entire buffer into this writer. Read more
🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
Writes a formatted string into this writer, returning any error encountered. Read more
Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Reads an unsigned 8 bit integer from the underlying reader. Read more
Reads a signed 8 bit integer from the underlying reader. Read more
Reads an unsigned 16 bit integer from the underlying reader. Read more
Reads a signed 16 bit integer from the underlying reader. Read more
Reads an unsigned 24 bit integer from the underlying reader. Read more
Reads a signed 24 bit integer from the underlying reader. Read more
Reads an unsigned 32 bit integer from the underlying reader. Read more
Reads a signed 32 bit integer from the underlying reader. Read more
Reads an unsigned 48 bit integer from the underlying reader. Read more
Reads a signed 48 bit integer from the underlying reader. Read more
Reads an unsigned 64 bit integer from the underlying reader. Read more
Reads a signed 64 bit integer from the underlying reader. Read more
Reads an unsigned 128 bit integer from the underlying reader. Read more
Reads a signed 128 bit integer from the underlying reader. Read more
Reads an unsigned n-bytes integer from the underlying reader. Read more
Reads a signed n-bytes integer from the underlying reader. Read more
Reads an unsigned n-bytes integer from the underlying reader.
Reads a signed n-bytes integer from the underlying reader.
Reads a IEEE754 single-precision (4 bytes) floating point number from the underlying reader. Read more
Reads a IEEE754 double-precision (8 bytes) floating point number from the underlying reader. Read more
Reads a sequence of unsigned 16 bit integers from the underlying reader. Read more
Reads a sequence of unsigned 32 bit integers from the underlying reader. Read more
Reads a sequence of unsigned 64 bit integers from the underlying reader. Read more
Reads a sequence of unsigned 128 bit integers from the underlying reader. Read more
Reads a sequence of signed 8 bit integers from the underlying reader. Read more
Reads a sequence of signed 16 bit integers from the underlying reader. Read more
Reads a sequence of signed 32 bit integers from the underlying reader. Read more
Reads a sequence of signed 64 bit integers from the underlying reader. Read more
Reads a sequence of signed 128 bit integers from the underlying reader. Read more
Reads a sequence of IEEE754 single-precision (4 bytes) floating point numbers from the underlying reader. Read more
👎Deprecated since 1.2.0: please use read_f32_into instead
DEPRECATED. Read more
Reads a sequence of IEEE754 double-precision (8 bytes) floating point numbers from the underlying reader. Read more
👎Deprecated since 1.2.0: please use read_f64_into instead
DEPRECATED. Read more
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Writes an unsigned 8 bit integer to the underlying writer. Read more
Writes a signed 8 bit integer to the underlying writer. Read more
Writes an unsigned 16 bit integer to the underlying writer. Read more
Writes a signed 16 bit integer to the underlying writer. Read more
Writes an unsigned 24 bit integer to the underlying writer. Read more
Writes a signed 24 bit integer to the underlying writer. Read more
Writes an unsigned 32 bit integer to the underlying writer. Read more
Writes a signed 32 bit integer to the underlying writer. Read more
Writes an unsigned 48 bit integer to the underlying writer. Read more
Writes a signed 48 bit integer to the underlying writer. Read more
Writes an unsigned 64 bit integer to the underlying writer. Read more
Writes a signed 64 bit integer to the underlying writer. Read more
Writes an unsigned 128 bit integer to the underlying writer.
Writes a signed 128 bit integer to the underlying writer.
Writes an unsigned n-bytes integer to the underlying writer. Read more
Writes a signed n-bytes integer to the underlying writer. Read more
Writes an unsigned n-bytes integer to the underlying writer. Read more
Writes a signed n-bytes integer to the underlying writer. Read more
Writes a IEEE754 single-precision (4 bytes) floating point number to the underlying writer. Read more
Writes a IEEE754 double-precision (8 bytes) floating point number to the underlying writer. Read more