Chunks
ChunkIterator reads a file in fixed-size byte blocks. Each chunk is exactly size bytes, except the last one, which may be shorter
In Rust, each item is a Result<Vec<u8>>. In Python, each item is a bytes object
Basic Usage
Rust
use preader::PReader;
let reader = PReader::default();
let chunks: Vec<Vec<u8>> = reader.chunks("data.bin")
.size(7)
.build()?
.collect::<preader::Result<_>>()?;
Python
from preader import PReader
reader = PReader()
chunks = list(reader.chunks("data.bin", chunk_size=7))
For a 26-byte file (abcdefghijklmnopqrstuvwxyz) with a chunk size of 7, the iterator yields four chunks: abcdefg, hijklmn, opqrstu, and vwxyz. The last chunk is only 5 bytes
Options
| Option | Rust Builder Method | Python Keyword |
|---|---|---|
| Chunk size | size(7) | chunk_size=7 |
| Drop partial | drop_partial(true) | drop_partial=True |
Chunk Size
The default chunk size is 1024 bytes. You set a custom size with size() in Rust or chunk_size= in Python
Dropping Partial Chunks
When drop_partial is enabled, the iterator discards the final chunk if it is shorter than the requested size
Rust
use preader::PReader;
let reader = PReader::default();
// All chunks, including a shorter last one
let all: Vec<Vec<u8>> = reader.chunks("data.bin")
.size(7)
.build()?
.collect::<preader::Result<_>>()?;
// 4 chunks: 7, 7, 7, 5 bytes
// Only full-sized chunks
let full: Vec<Vec<u8>> = reader.chunks("data.bin")
.size(7)
.drop_partial(true)
.build()?
.collect::<preader::Result<_>>()?;
// 3 chunks: 7, 7, 7 bytes
Python
from preader import PReader
reader = PReader()
all_chunks = list(reader.chunks("data.bin", chunk_size=7))
# 4 chunks: 7, 7, 7, 5 bytes
full_chunks = list(reader.chunks("data.bin", chunk_size=7, drop_partial=True))
# 3 chunks: 7, 7, 7 bytes