#binary-data #binary #buffer #byte #writer #binary-reader #reader

buffer_io

an intuitive library for reading and writing binary buffers

4 stable releases

1.0.3 Jul 11, 2020
1.0.0 Dec 9, 2019

#2339 in Encoding

MIT license

19KB
312 lines

This crate provides convenience methods for reading and writing data to binary buffers. It supports writing primitive types, as well as Strings and Vectors to in-memory streams and files.

Installation

This crate works with Cargo and is on crates.io. Add it to your Cargo.toml like so:

[dependencies]
buffer_io = "1"

If you want to write data to a buffer you do it like so:

use crate::buffer::{BufferReader, BufferWriter, SeekOrigin};
use std::io::Cursor;
let mut buffer = BufferWriter::new(Cursor::new(Vec::new()));
buffer.write_u32(9001)?;
buffer.write_u32(9002).unwrap()?;
buffer.write_string("Hello World!")?;

You can then return the full buffer as a vector:

let data = buffer.to_vec()?;

Reading buffers is just as simple.

let mut reader = BufferReader::new(File::open("test.bin")?);
let magic = reader.read_u32()?;
let body = reader.read_string()?;

Dependencies

~305–770KB
~18K SLoC