Struct osrs_buffer::ByteBuffer
source · [−]Expand description
The ByteBuffer for reading and writing binary data.
Fields
data: Vec<u8>The bytes of the buffer, stored as a vector of unsigned bytes.
read_pos: usizeThe current position for reading data.
write_pos: usizeThe current position for writing data.
Implementations
Implementations for the ByteBuffer
Initializes a new buffer with a given size.
Examples
use osrs_buffer::ByteBuffer;
let buf = ByteBuffer::new(5);
assert_eq!(buf.data.len(), 5);
Initializes a buffer given an existing buffer.
Examples
use osrs_buffer::ByteBuffer;
let other_buf = ByteBuffer::new(9);
let buf = ByteBuffer::from_buf(other_buf.data);
assert_eq!(buf.data.len(), 9);
Clears the reading and writing position, resetting them to 0.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(1);
buf.clear();
assert_eq!(buf.read_pos, 0);
assert_eq!(buf.write_pos, 0);
Skips over data, increasing the reading and writing position of the buffer by the specified amount.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(5);
buf.skip(3);
assert_eq!(buf.read_pos, 3);
assert_eq!(buf.write_pos, 3);
Writes a given amount of bytes to the buffer, increasing the writing position as a result.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(5);
buf.write_bytes(&[8,9,2]);
assert_eq!(buf.data[0], 8);
assert_eq!(buf.data[1], 9);
assert_eq!(buf.data[2], 2);
assert_eq!(buf.write_pos, 3);
Writes a bool to the buffer, increasing the writing position by 1.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(1);
buf.write_bool(true);
assert_eq!(buf.data[0], 1);
assert_eq!(buf.write_pos, 1);
Writes an unsigned byte to the buffer, increasing the writing position by 1.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(1);
buf.write_u8(42);
assert_eq!(buf.data[0], 42);
assert_eq!(buf.write_pos, 1);
Writes a signed byte to the buffer, increasing the writing position by 1.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(1);
buf.write_i8(-67);
assert_eq!(buf.data[0] as i8, -67);
assert_eq!(buf.write_pos, 1);
Writes the number 128, subtracted by the signed byte to the buffer, increasing the writing position by 1.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(1);
buf.write_i8_sub(99);
assert_eq!(buf.data[0], 29);
assert_eq!(buf.write_pos, 1);
Writes the byte and adds 128 to it, increasing the writing position by 1.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(1);
buf.write_i8_add(42);
assert_eq!(buf.data[0], 170);
assert_eq!(buf.write_pos, 1);
Writes a negated byte to the buffer, increasing the writing position by 1.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(1);
buf.write_i8_neg(55);
assert_eq!(buf.data[0], 201);
assert_eq!(buf.write_pos, 1);
Writes an unsigned short to the buffer, increasing the writing position by 2.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(2);
buf.write_u16(20065);
assert_eq!(buf.data[0], 78);
assert_eq!(buf.data[1], 97);
assert_eq!(buf.write_pos, 2);
Writes an unsigned short smart to the buffer, increasing the writing position by 2.
Examples
Writing a value lesser than or equal to 127 makes it write out a single unsigned byte.
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(2);
buf.write_u16_smart(65);
assert_eq!(buf.data[0], 65);
assert_eq!(buf.data[1], 0);
assert_eq!(buf.write_pos, 1);
Writing a value greater than 127 will make it write out two unsigned bytes.
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(2);
buf.write_u16_smart(986);
assert_eq!(buf.data[0], 131);
assert_eq!(buf.data[1], 218);
assert_eq!(buf.write_pos, 2);
Writes a signed short to the buffer, increasing the writing position by 2.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(2);
buf.write_i16(-14632);
assert_eq!(buf.data[0], 198);
assert_eq!(buf.data[1], 216);
assert_eq!(buf.write_pos, 2);
Writes a signed short add to the buffer, increasing the writing position by 2.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(2);
buf.write_i16_add(-9867);
assert_eq!(buf.data[0], 217);
assert_eq!(buf.data[1], 245);
assert_eq!(buf.write_pos, 2);
Writes a signed short add as a little endian to the buffer, increasing the writing position by 2.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(2);
buf.write_i16_le_add(-12632);
assert_eq!(buf.data[0], 40);
assert_eq!(buf.data[1], 206);
assert_eq!(buf.write_pos, 2);
Writes an unsigned short as a little endian to the buffer, increasing the writing position by 2.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(2);
buf.write_u16_le(29543);
assert_eq!(buf.data[0], 103);
assert_eq!(buf.data[1], 115);
assert_eq!(buf.write_pos, 2);
Writes a signed short as a little endian to the buffer, increasing the writing position by 2.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(2);
buf.write_i16_le(-7654);
assert_eq!(buf.data[0], 26);
assert_eq!(buf.data[1], 226);
assert_eq!(buf.write_pos, 2);
Writes an unsigned dword to the buffer, increasing the writing position by 4.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(4);
buf.write_u32(98571);
assert_eq!(buf.data[0], 0);
assert_eq!(buf.data[1], 1);
assert_eq!(buf.data[2], 129);
assert_eq!(buf.data[3], 11);
assert_eq!(buf.write_pos, 4);
Writes a signed dword to the buffer, increasing the writing position by 4.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(4);
buf.write_i32(-131045);
assert_eq!(buf.data[0], 255);
assert_eq!(buf.data[1], 254);
assert_eq!(buf.data[2], 0);
assert_eq!(buf.data[3], 27);
assert_eq!(buf.write_pos, 4);
Writes a signed dword as a middle endian to the buffer, increasing the writing position by 4.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(4);
buf.write_i32_ime(-98231);
assert_eq!(buf.data[0], 128);
assert_eq!(buf.data[1], 73);
assert_eq!(buf.data[2], 255);
assert_eq!(buf.data[3], 254);
assert_eq!(buf.write_pos, 4);
Writes am unsigned integer as little endian to the buffer, increasing the writing position by 2.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(4);
buf.write_u32_le(26904);
assert_eq!(buf.data[0], 24);
assert_eq!(buf.data[1], 105);
assert_eq!(buf.data[2], 0);
assert_eq!(buf.data[3], 0);
assert_eq!(buf.write_pos, 4);
Writes a signed integer as little endian to the buffer, increasing the writing position by 2.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(4);
buf.write_i32_le(18879);
assert_eq!(buf.data[0], 191);
assert_eq!(buf.data[1], 73);
assert_eq!(buf.data[2], 0);
assert_eq!(buf.data[3], 0);
assert_eq!(buf.write_pos, 4);
Writes an unsigned qword to the buffer, increasing the writing position by 8.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(8);
buf.write_u64(8589934592);
assert_eq!(buf.data[0], 0);
assert_eq!(buf.data[1], 0);
assert_eq!(buf.data[2], 0);
assert_eq!(buf.data[3], 2);
assert_eq!(buf.data[4], 0);
assert_eq!(buf.data[5], 0);
assert_eq!(buf.data[6], 0);
assert_eq!(buf.data[7], 0);
assert_eq!(buf.write_pos, 8);
Writes a signed qword to the buffer, increasing the writing position by 8.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(8);
buf.write_i64(-8589934592);
assert_eq!(buf.data[0], 255);
assert_eq!(buf.data[1], 255);
assert_eq!(buf.data[2], 255);
assert_eq!(buf.data[3], 254);
assert_eq!(buf.data[4], 0);
assert_eq!(buf.data[5], 0);
assert_eq!(buf.data[6], 0);
assert_eq!(buf.data[7], 0);
assert_eq!(buf.write_pos, 8);
Writes a null-terminated string to the buffer, increasing the writing position by the string’s length, plus the null terminated byte.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(8);
buf.write_string_null_terminated("hello");
assert_eq!(buf.data[0], 104);
assert_eq!(buf.data[1], 101);
assert_eq!(buf.data[2], 108);
assert_eq!(buf.data[3], 108);
assert_eq!(buf.data[4], 111);
assert_eq!(buf.data[5], 0);
assert_eq!(buf.write_pos, 6);Write bytes reversed with add, increasing the writing position by the length of the bytes written.
Examples
use osrs_buffer::ByteBuffer;
// Create the other buffer with the data, and set its writing position manually
let mut other_buf = ByteBuffer::new(3);
other_buf.data[0] = 1;
other_buf.data[1] = 2;
other_buf.data[2] = 3;
other_buf.write_pos = 3;
// Create the buffer which the reversed bytes with add should be written to
let mut buf = ByteBuffer::new(3);
buf.write_bytes_reversed_add(&other_buf);
assert_eq!(buf.data[0], 131);
assert_eq!(buf.data[1], 130);
assert_eq!(buf.data[2], 129);
assert_eq!(buf.write_pos, 3);Read bytes from the current buffer into another buffer.
Examples
use osrs_buffer::ByteBuffer;
// Create the other buffer with the data
let mut other_buf = ByteBuffer::new(3);
other_buf.data[0] = 99;
other_buf.data[1] = 54;
other_buf.data[2] = 31;
// Create the buffer which should read the bytes from the other buffer,
// and pass it to read_bytes(...) along with the length
let mut buf = ByteBuffer::new(3);
other_buf.read_bytes(&mut buf, 3);
assert_eq!(buf.data[0], 99);
assert_eq!(buf.data[1], 54);
assert_eq!(buf.data[2], 31);
assert_eq!(buf.write_pos, 3);Reads a bool from the buffer, increasing the reading position by 1.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(1);
buf.data[0] = 1;
assert_eq!(buf.read_bool(), true);
Reads an unsigned byte from the buffer, increasing the reading position by 1.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(1);
buf.data[0] = 231;
assert_eq!(buf.read_u8(), 231);
Reads a signed byte from the buffer, increasing the reading position by 1.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(1);
buf.data[0] = 6;
assert_eq!(buf.read_i8(), 6);
Reads an unsigned short from the buffer, increasing the reading position by 2.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(2);
buf.data[0] = 66;
buf.data[1] = 89;
assert_eq!(buf.read_u16(), 16985);
Reads a signed short from the buffer, increasing the reading position by 2.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(2);
buf.data[0] = 255;
buf.data[1] = 98;
assert_eq!(buf.read_i16(), -158);
Reads an unsigned dword from the buffer, increasing the reading position by 4.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(4);
buf.data[0] = 42;
buf.data[1] = 87;
buf.data[2] = 33;
buf.data[3] = 16;
assert_eq!(buf.read_u32(), 710353168);
Reads a signed dword from the buffer, increasing the reading position by 4.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(4);
buf.data[0] = 255;
buf.data[1] = 87;
buf.data[2] = 33;
buf.data[3] = 16;
assert_eq!(buf.read_i32(), -11067120);
Reads an unsigned qword from the buffer, increasing the reading position by 8.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(8);
buf.data[0] = 31;
buf.data[1] = 84;
buf.data[2] = 11;
buf.data[3] = 99;
buf.data[4] = 45;
buf.data[5] = 12;
buf.data[6] = 94;
buf.data[7] = 36;
assert_eq!(buf.read_u64(), 2257441833804914212);
Reads a signed qword from the buffer, increasing the reading position by 8.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(8);
buf.data[0] = 255;
buf.data[1] = 84;
buf.data[2] = 11;
buf.data[3] = 99;
buf.data[4] = 45;
buf.data[5] = 12;
buf.data[6] = 94;
buf.data[7] = 36;
assert_eq!(buf.read_i64(), -48401175408779740);
Reads a string from the buffer, increasing the reading position by the length of the string.
Examples
use osrs_buffer::ByteBuffer;
let mut buf = ByteBuffer::new(8);
buf.data[0] = 109;
buf.data[1] = 121;
buf.data[2] = 32;
buf.data[3] = 116;
buf.data[4] = 101;
buf.data[5] = 115;
buf.data[6] = 116;
buf.data[7] = 0;
assert_eq!(buf.read_string(), "my test");