1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use std::{
    fs::File,
    io::{self, Read, Write},
};

use crate::constants::DEFAULT_BUF_LEN;

fn move_chunk(
    buf: &mut [u8; DEFAULT_BUF_LEN],
    from: &mut File,
    to: &mut File,
) -> io::Result<usize> {
    let read_len = from.read(buf)?;
    to.write(&buf[..read_len])?;
    Ok(read_len)
}

pub fn stream(from: &mut File, to: &mut File) -> io::Result<()> {
    let mut buffer = [0u8; DEFAULT_BUF_LEN];
    loop {
        let read_len = move_chunk(&mut buffer, from, to)?;
        if read_len != DEFAULT_BUF_LEN {
            break;
        }
    }
    Ok(())
}