### Customize Base64 Stream Buffer Size in Rust Source: https://github.com/magiclen/base64-stream/blob/master/README.md Demonstrates how to change the default buffer size for Base64 streaming operations. By using the `new2` associated function and specifying a type for the buffer length (e.g., `U256`), users can control memory usage and potentially performance. This example shows customizing `ToBase64Reader`. ```rust use std::io::{Cursor, Read}; use base64_stream::ToBase64Reader; use base64_stream::generic_array::typenum::U256; let test_data = b"Hi there, this is a simple sentence used for testing this crate. I hope all cases are correct.".to_vec(); let mut reader: ToBase64Reader<_, U256> = ToBase64Reader::new2(Cursor::new(test_data)); let mut base64 = String::new(); reader.read_to_string(&mut base64).unwrap(); assert_eq!("SGkgdGhlcmUsIHRoaXMgaXMgYSBzaW1wbGUgc2VudGVuY2UgdXNlZCBmb3IgdGVzdGluZyB0aGlzIGNyYXRlLiBJIGhvcGUgYWxsIGNhc2VzIGFyZSBjb3JyZWN0Lg==", base64); ``` -------------------------------- ### Configure Custom Buffer Size for Base64 Encoding Reader in Rust Source: https://context7.com/magiclen/base64-stream/llms.txt Demonstrates how to use a custom buffer size (U256, i.e., 256 bytes) with `ToBase64Reader` for Base64 encoding. This allows optimization for specific use cases where the default 4096-byte buffer might not be ideal. It requires the `base64_stream` and `generic-array` crates. ```rust use std::io::{Cursor, Read}; use base64_stream::ToBase64Reader; use base64_stream::generic_array::typenum::U256; let test_data = b"Hi there, this is a simple sentence used for testing this crate. I hope all cases are correct.".to_vec(); // Create a reader with a 256-byte buffer instead of the default 4096 bytes let mut reader: ToBase64Reader<_, U256> = ToBase64Reader::new2(Cursor::new(test_data)); let mut base64 = String::new(); reader.read_to_string(&mut base64).unwrap(); assert_eq!( "SGkgdGhlcmUsIHRoaXMgaXMgYSBzaW1wbGUgc2VudGVuY2UgdXNlZCBmb3IgdGVzdGluZyB0aGlzIGNyYXRlLiBJIGhvcGUgYWxsIGNhc2VzIGFyZSBjb3JyZWN0Lg==", base64 ); ``` -------------------------------- ### Decode Base64 Data using FromBase64Reader Source: https://github.com/magiclen/base64-stream/blob/master/README.md Decodes Base64 encoded data from a source reader. This is useful for reading and decoding large Base64 encoded streams. The output is the original plain text data. Ensure the input reader contains valid Base64 data. ```rust use std::io::Cursor; use std::io::Read; use base64_stream::FromBase64Reader; let base64 = b"SGkgdGhlcmUsIHRoaXMgaXMgYSBzaW1wbGUgc2VudGVuY2UgdXNlZCBmb3IgdGVzdGluZyB0aGlzIGNyYXRlLiBJIGhvcGUgYWxsIGNhc2VzIGFyZSBjb3JyZWN0Lg==".to_vec(); let mut reader = FromBase64Reader::new(Cursor::new(base64)); let mut test_data = String::new(); reader.read_to_string(&mut test_data).unwrap(); assert_eq!("Hi there, this is a simple sentence used for testing this crate. I hope all cases are correct.", test_data); ``` -------------------------------- ### Decode Base64 Data using FromBase64Writer Source: https://github.com/magiclen/base64-stream/blob/master/README.md Decodes Base64 encoded data from an input source and writes the original plain text data to a destination writer. This allows decoding directly to files or other writers. The `flush` method should be called after writing all Base64 data. ```rust use std::fs::{self, File}; use std::io::Write; use std::path::Path; use base64_stream::FromBase64Writer; const DATA_FOLDER: &str = "data"; const DECODE_OUTPUT: &str = "decode_output.txt"; let base64 = b"SGkgdGhlcmUsIHRoaXMgaXMgYSBzaW1wbGUgc2VudGVuY2UgdXNlZCBmb3IgdGVzdGluZyB0aGlzIGNyYXRlLiBJIGhvcGUgYWxsIGNhc2VzIGFyZSBjb3JyZWN0Lg==".as_ref(); let file_path = Path::new("tests").join(DATA_FOLDER).join(DECODE_OUTPUT); let test_data = File::create(file_path.as_path()).unwrap(); let mut writer = FromBase64Writer::new(test_data); writer.write_all(base64).unwrap(); writer.flush().unwrap(); // the flush method is only used when the full base64 data has been written assert_eq!("Hi there, this is a simple sentence used for testing this crate. I hope all cases are correct.", fs::read_to_string(file_path).unwrap()); ``` -------------------------------- ### Decode Base64 Data with FromBase64Writer Source: https://context7.com/magiclen/base64-stream/llms.txt Decodes Base64 encoded data written to a Rust `Write` implementation into plain data using `FromBase64Writer`. Data is decoded as it's written, and a final `flush()` call is required to finalize the decoding process and handle any remaining buffered data. This is ideal for decoding Base64 data from a source and writing the plain output. ```rust use std::fs::{self, File}; use std::io::Write; use std::path::Path; use base64_stream::FromBase64Writer; let base64 = b"SGkgdGhlcmUsIHRoaXMgaXMgYSBzaW1wbGUgc2VudGVuY2UgdXNlZCBmb3IgdGVzdGluZyB0aGlzIGNyYXRlLiBJIGhvcGUgYWxsIGNhc2VzIGFyZSBjb3JyZWN0Lg==".as_ref(); let file_path = Path::new("tests").join("data").join("decode_output.txt"); let test_data = File::create(file_path.as_path()).unwrap(); let mut writer = FromBase64Writer::new(test_data); writer.write_all(base64).unwrap(); writer.flush().unwrap(); // Must flush when all base64 data has been written assert_eq!( "Hi there, this is a simple sentence used for testing this crate. I hope all cases are correct.", fs::read_to_string(file_path).unwrap() ); ``` -------------------------------- ### Encode Data to Base64 using ToBase64Writer Source: https://github.com/magiclen/base64-stream/blob/master/README.md Encodes input data into Base64 format and writes it to a destination writer. This is suitable for writing Base64 encoded data directly to files or network streams. The `flush` method should be called after all data has been written to ensure the encoding is complete. ```rust use std::fs::{self, File}; use std::io::Write; use std::path::Path; use base64_stream::ToBase64Writer; const DATA_FOLDER: &str = "data"; const ENCODE_OUTPUT: &str = "encode_output.txt"; let test_data = b"Hi there, this is a simple sentence used for testing this crate. I hope all cases are correct.".as_ref(); let file_path = Path::new("tests").join(DATA_FOLDER).join(ENCODE_OUTPUT); let base64 = File::create(file_path.as_path()).unwrap(); let mut writer = ToBase64Writer::new(base64); writer.write_all(test_data).unwrap(); writer.flush().unwrap(); // the flush method is only used when the full plain data has been written assert_eq!("SGkgdGhlcmUsIHRoaXMgaXMgYSBzaW1wbGUgc2VudGVuY2UgdXNlZCBmb3IgdGVzdGluZyB0aGlzIGNyYXRlLiBJIGhvcGUgYWxsIGNhc2VzIGFyZSBjb3JyZWN0Lg==", fs::read_to_string(file_path).unwrap()); ``` -------------------------------- ### Encode Plain Data to Base64 with ToBase64Writer Source: https://context7.com/magiclen/base64-stream/llms.txt Encodes plain data written to a Rust `Write` implementation into Base64 format using `ToBase64Writer`. Data is encoded as it's written, and a final `flush()` call is necessary to complete the encoding process. This is suitable for writing Base64 encoded data to files or network streams. ```rust use std::fs::{self, File}; use std::io::Write; use std::path::Path; use base64_stream::ToBase64Writer; let test_data = b"Hi there, this is a simple sentence used for testing this crate. I hope all cases are correct.".as_ref(); let file_path = Path::new("tests").join("data").join("encode_output.txt"); let base64 = File::create(file_path.as_path()).unwrap(); let mut writer = ToBase64Writer::new(base64); writer.write_all(test_data).unwrap(); writer.flush().unwrap(); // Must flush when all plain data has been written assert_eq!( "SGkgdGhlcmUsIHRoaXMgaXMgYSBzaW1wbGUgc2VudGVuY2UgdXNlZCBmb3IgdGVzdGluZyB0aGlzIGNyYXRlLiBJIGhvcGUgYWxsIGNhc2VzIGFyZSBjb3JyZWN0Lg==", fs::read_to_string(file_path).unwrap() ); ``` -------------------------------- ### Encode Plain Data to Base64 with ToBase64Reader Source: https://context7.com/magiclen/base64-stream/llms.txt Encodes data from a Rust `Read` implementation to Base64 format using `ToBase64Reader`. It reads data incrementally and transforms it into Base64 as it's processed. This is useful for streaming large amounts of data to be Base64 encoded. ```rust use std::io::{Cursor, Read}; use base64_stream::ToBase64Reader; let test_data = b"Hi there, this is a simple sentence used for testing this crate. I hope all cases are correct.".to_vec(); let mut reader = ToBase64Reader::new(Cursor::new(test_data)); let mut base64 = String::new(); reader.read_to_string(&mut base64).unwrap(); assert_eq!( "SGkgdGhlcmUsIHRoaXMgaXMgYSBzaW1wbGUgc2VudGVuY2UgdXNlZCBmb3IgdGVzdGluZyB0aGlzIGNyYXRlLiBJIGhvcGUgYWxsIGNhc2VzIGFyZSBjb3JyZWN0Lg==", base64 ); ``` -------------------------------- ### Decode Base64 Data with FromBase64Reader Source: https://context7.com/magiclen/base64-stream/llms.txt Decodes Base64 encoded data from a Rust `Read` implementation into plain data using `FromBase64Reader`. It reads Base64 data incrementally and decodes it as it's processed, returning IO errors for invalid Base64 input. This is useful for streaming Base64 data that needs to be decoded. ```rust use std::io::{Cursor, Read}; use base64_stream::FromBase64Reader; let base64 = b"SGkgdGhlcmUsIHRoaXMgaXMgYSBzaW1wbGUgc2VudGVuY2UgdXNlZCBmb3IgdGVzdGluZyB0aGlzIGNyYXRlLiBJIGhvcGUgYWxsIGNhc2VzIGFyZSBjb3JyZWN0Lg==".to_vec(); let mut reader = FromBase64Reader::new(Cursor::new(base64)); let mut test_data = String::new(); reader.read_to_string(&mut test_data).unwrap(); assert_eq!( "Hi there, this is a simple sentence used for testing this crate. I hope all cases are correct.", test_data ); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.