This commit is contained in:
Florian THIERRY
2026-07-13 22:52:40 +02:00
parent b5a95b0fa3
commit ab4960c79c
7 changed files with 181 additions and 1 deletions
+73
View File
@@ -0,0 +1,73 @@
// Video service implementations for streaming files chunk by chunk
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::Path;
use std::io::Read;
/// Basic example: Read file line by line using BufReader
pub fn read_file_line_by_line(path: &str) -> std::io::Result<()> {
let path = Path::new(path);
let file = File::open(path)?;
let reader = BufReader::new(file);
// Read line by line
for line in reader.lines() {
let line = line?;
println!("Line: {}", line);
}
Ok(())
}
/// Read file in fixed-size chunks
pub fn read_file_in_chunks(path: &str, chunk_size: usize) -> std::io::Result<()> {
let mut file = File::open(path)?;
let mut buffer = vec![0; chunk_size];
loop {
let n = file.read(&mut buffer)?;
if n == 0 {
break; // EOF
}
println!("Read {} bytes: {:?}", n, &buffer[..n]);
}
Ok(())
}
/// Using std::io::copy for streaming between files
pub fn copy_file_streaming(input_path: &str, output_path: &str) -> std::io::Result<()> {
use std::io;
let mut input = File::open(input_path)?;
let mut output = File::create(output_path)?;
// Copy in chunks without loading entire file into memory
io::copy(&mut input, &mut output)?;
Ok(())
}
/// Process large files efficiently with configurable buffer size
pub fn process_large_file(path: &str, buffer_size: usize) -> std::io::Result<()> {
let file = File::open(path)?;
let mut reader = BufReader::new(file);
let mut buffer = vec![0; buffer_size];
loop {
let bytes_read = reader.read(&mut buffer)?;
if bytes_read == 0 {
break;
}
process_chunk(&buffer[..bytes_read]);
}
Ok(())
}
/// Example chunk processing function
fn process_chunk(chunk: &[u8]) {
// Process your chunk here
println!("Processing {} bytes", chunk.len());
}