Reading as chunks


use std::fs::File;
use std::io::{BufReader, Read};
use std::path::Path;

const CHUNK_SIZE: usize = 1 * 1024 * 1024 * 1024; // 1 GB

fn inspect_binary_file_in_chunks(filepath: &Path) -> Result<(), std::io::Error> {
    let file = File::open(filepath)?;
    let mut reader = BufReader::new(file);
    let mut buffer = vec![0; CHUNK_SIZE];

    let mut total_bytes_read = 0;

    loop {
        let bytes_read = reader.read(&mut buffer)?;

        if bytes_read == 0 {
            // End of file reached
            break;
        }

        println!("Read {} bytes in this chunk (Total: {} bytes)", bytes_read, total_bytes_read + bytes_read);

        // Process the current chunk in 'buffer' (from index 0 to bytes_read)
        // You'll need to implement your message parsing logic here for each chunk.

        // Example of inspecting the first few bytes of each chunk:
        if bytes_read > 0 {
            println!("First few bytes of this chunk:");
            for i in 0..std::cmp::min(32, bytes_read) {
                print!("{:02X} ", buffer[i]);
                if (i + 1) % 16 == 0 {
                    println!();
                }
            }
            println!();

            if bytes_read >= 1 {
                let message_type = buffer[0] as char;
                println!("First message type indicator in this chunk: '{}' (ASCII: {}) (Hex: {:02X})",
                         message_type, buffer[0], buffer[0]);
            }
        }

        total_bytes_read += bytes_read;
    }

    println!("Finished reading the file. Total bytes read: {}", total_bytes_read);

    Ok(())
}

fn main() {
    let filepath = Path::new("12302019.NASDAQ_ITCH50"); // Replace with your actual file path
    if let Err(e) = inspect_binary_file_in_chunks(filepath) {
        eprintln!("Error reading file in chunks: {}", e);
    }
}