forked from hush/silentdragonlite-cli
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
21 lines
733 B
21 lines
733 B
use std::io::{self, Read, Write};
|
|
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
|
|
|
|
pub fn read_string<R: Read>(mut reader: R) -> io::Result<String> {
|
|
// Strings are written as <littleendian> len + bytes
|
|
let str_len = reader.read_u64::<LittleEndian>()?;
|
|
let mut str_bytes = vec![0; str_len as usize];
|
|
reader.read_exact(&mut str_bytes)?;
|
|
|
|
let str = String::from_utf8(str_bytes).map_err(|e| {
|
|
io::Error::new(io::ErrorKind::InvalidData, e.to_string())
|
|
})?;
|
|
|
|
Ok(str)
|
|
}
|
|
|
|
pub fn write_string<W: Write>(mut writer: W, s: &String) -> io::Result<()> {
|
|
// Strings are written as len + utf8
|
|
writer.write_u64::<LittleEndian>(s.as_bytes().len() as u64)?;
|
|
writer.write_all(s.as_bytes())
|
|
}
|