pub struct ByteWriter {
pub bytes: Vec<u8>,
max_size: usize,
}
impl Default for ByteWriter {
fn default() -> Self {
Self::new(usize::MAX)
}
}
impl ByteWriter {
pub fn new(max_size: usize) -> Self {
Self {
bytes: Vec::new(),
max_size,
}
}
fn _can_write(&self) -> bool {
self.bytes.len() < self.max_size
}
pub fn write_byte(&mut self, byte: u8) -> bool {
if self._can_write() {
self.bytes.push(byte);
return true;
}
false
}
pub fn write_vec(&mut self, bytes: &Vec<u8>) -> bool {
if self._can_write() {
self.bytes.extend_from_slice(bytes);
return true;
}
false
}
pub fn write_bytes(&mut self, bytes: &[u8]) -> bool {
if self._can_write() {
self.bytes.extend_from_slice(bytes);
return true;
}
false
}
pub fn write_bytes_iterator<'a>(&mut self, bytes: impl Iterator<Item = u8> + 'a) -> bool {
if self._can_write() {
self.bytes.extend(bytes);
return true;
}
false
}
pub fn write_ascii_string(&mut self, string: &str) -> bool {
if self._can_write() {
self.write_bytes(string.as_bytes());
return true;
}
false
}
pub fn write_string(&mut self, string: &str, write_end: bool) -> bool {
if self._can_write() {
self.bytes.extend_from_slice(string.as_bytes());
if write_end {
self.write_byte(0);
}
return true;
}
false
}
pub fn write_int(&mut self, value: i64, size: usize, le: bool) -> bool {
if self._can_write() {
let bytes = value.to_le_bytes();
let bytes = &bytes[0..size];
if le {
self.write_bytes(&bytes);
} else {
let bytes: Vec<u8> = bytes.iter().cloned().rev().collect();
self.write_bytes(&bytes);
};
}
false
}
pub fn write_int8(&mut self, value: i8, le: bool) -> bool {
if self._can_write() {
let bytes = value.to_le_bytes();
if le {
self.write_bytes(&bytes);
} else {
let bytes: Vec<u8> = bytes.iter().cloned().rev().collect();
self.write_bytes(&bytes);
};
}
false
}
pub fn write_int16(&mut self, value: i16, le: bool) -> bool {
if self._can_write() {
let bytes = value.to_le_bytes();
if le {
self.write_bytes(&bytes);
} else {
let bytes: Vec<u8> = bytes.iter().cloned().rev().collect();
self.write_bytes(&bytes);
};
}
false
}
pub fn write_int32(&mut self, value: i32, le: bool) -> bool {
if self._can_write() {
let bytes = value.to_le_bytes();
if le {
self.write_bytes(&bytes);
} else {
let bytes: Vec<u8> = bytes.iter().cloned().rev().collect();
self.write_bytes(&bytes);
};
}
false
}
pub fn write_int64(&mut self, value: i64, le: bool) -> bool {
if self._can_write() {
let bytes = value.to_le_bytes();
if le {
self.write_bytes(&bytes);
} else {
let bytes: Vec<u8> = bytes.iter().cloned().rev().collect();
self.write_bytes(&bytes);
};
}
false
}
pub fn write_float32(&mut self, value: f32, le: bool) -> bool {
if self._can_write() {
let bytes = value.to_le_bytes();
if le {
self.write_bytes(&bytes);
} else {
let bytes: Vec<u8> = bytes.iter().cloned().rev().collect();
self.write_bytes(&bytes);
};
return true;
}
false
}
pub fn write_float64(&mut self, value: f64, le: bool) -> bool {
if self._can_write() {
let bytes = value.to_le_bytes();
if le {
self.write_bytes(&bytes);
} else {
let bytes: Vec<u8> = bytes.iter().cloned().rev().collect();
self.write_bytes(&bytes);
};
return true;
}
false
}
}