This struct provides a buffered access to a Read
object
with a limited Seek
implementation. In other words, [BufStreamReader
] turns a
Read
into a Read
+Seek
, which can be used
together with binary parsers such as binread
(which is the reason why I created this crate).
Seeking is limited by the following constraints:
buffer_size
parameter of [BufStreamReader::new()
])SeekFrom::End
is not supported```rust use std::io::{Cursor, Read, Seek, SeekFrom}; use bufstreamreader::BufStreamReader; let cursor = Cursor::new(&arr); // points to array with values from \x00 .. \xff let mut reader = BufStreamReader::new(cursor, 16);
let mut buffer: [u8; 7] = [0; 7];
/* straightly reading 7 bytes works */ asserteq!(reader.read(&mut buffer).unwrap(), buffer.len()); asserteq!(&buffer, &arr[0..7]);
/* seeking backwards inside the current buffer */ assert!(reader.seek(SeekFrom::Current(-4)).isok()); asserteq!(reader.read(&mut buffer).unwrap(), 7); assert_eq!(&buffer, &arr[3..10]); ```
```rust let cursor = Cursor::new(&arr); // points to array with values from \x00 .. \xff let mut reader = BufStreamReader::new(cursor, 16);
let mut buffer: [u8; 7] = [0; 7]; assert!(reader.seek(SeekFrom::Start(96)).isok()); assert!(reader.seek(SeekFrom::Start(95)).iserr()); assert!(reader.seek(SeekFrom::Current(-1)).is_err()); ```
```rust let cursor = Cursor::new(&arr); // points to array with values from \x00 .. \xff let mut reader = BufStreamReader::new(cursor, 16);
let mut buffer: [u8; 7] = [0; 7]; assert!(reader.seek(SeekFrom::Start(10)).isok()); asserteq!(reader.read(&mut buffer).unwrap(), buffer.len()); assert_eq!(&buffer, &arr[10..17]);
assert!(reader.seek(SeekFrom::Current(122)).isok()); asserteq!(reader.read(&mut buffer).unwrap(), buffer.len()); assert_eq!(&buffer, &arr[139..146]); ```