copy_with_buffer

Function copy_with_buffer 

Source
pub fn copy_with_buffer<R: Read, W: Write>(
    reader: &mut R,
    writer: &mut W,
    buffer: &mut CopyBuffer,
) -> Result<u64, ExtractionError>
Expand description

Copies data from reader to writer using the provided reusable buffer.

This is an optimized version of std::io::copy that:

  • Uses a caller-provided buffer (avoiding heap allocation)
  • Uses checked arithmetic to detect quota overflows
  • Returns the total number of bytes copied

§Errors

Returns an error if:

  • Reading from the source fails
  • Writing to the destination fails
  • Total bytes written would overflow u64 (quota protection)

§Security

Quota overflow is explicitly checked using checked_add, ensuring that malicious archives cannot bypass size limits via integer overflow.

§Examples

let mut buffer = CopyBuffer::new();
let mut input = std::fs::File::open("large_file.bin")?;
let mut output = std::fs::File::create("output.bin")?;

let total = copy_with_buffer(&mut input, &mut output, &mut buffer)?;
println!("Copied {} bytes without heap allocation", total);