Crates.io | buf_stream_reader |
lib.rs | buf_stream_reader |
version | 0.2.0 |
source | src |
created_at | 2022-02-07 19:52:05.848989 |
updated_at | 2022-02-08 12:01:38.242437 |
description | provides a buffered access to a Read object with a limited Seek implementation. |
homepage | https://www.github.com/janstarke/buf_stream_reader |
repository | |
max_upload_size | |
id | 528579 |
size | 47,933 |
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 supporteduse std::io::{Cursor, Read, Seek, SeekFrom};
use buf_stream_reader::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 */
assert_eq!(reader.read(&mut buffer).unwrap(), buffer.len());
assert_eq!(&buffer, &arr[0..7]);
/* seeking backwards inside the current buffer */
assert!(reader.seek(SeekFrom::Current(-4)).is_ok());
assert_eq!(reader.read(&mut buffer).unwrap(), 7);
assert_eq!(&buffer, &arr[3..10]);
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)).is_ok());
assert!(reader.seek(SeekFrom::Start(95)).is_err());
assert!(reader.seek(SeekFrom::Current(-1)).is_err());
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)).is_ok());
assert_eq!(reader.read(&mut buffer).unwrap(), buffer.len());
assert_eq!(&buffer, &arr[10..17]);
assert!(reader.seek(SeekFrom::Current(122)).is_ok());
assert_eq!(reader.read(&mut buffer).unwrap(), buffer.len());
assert_eq!(&buffer, &arr[139..146]);