#base64 #stream #file #reader #writer

base64-stream

To encode/decode large data with the standard Base64 encoding

21 stable releases

1.2.6 Apr 21, 2021
1.2.5 Nov 13, 2020
1.2.4 Jul 29, 2020
1.0.11 Feb 21, 2020
0.1.0 Oct 23, 2018

#132 in Encoding

Download history 198/week @ 2021-08-11 275/week @ 2021-08-18 250/week @ 2021-08-25 211/week @ 2021-09-01 455/week @ 2021-09-08 370/week @ 2021-09-15 401/week @ 2021-09-22 381/week @ 2021-09-29 419/week @ 2021-10-06 283/week @ 2021-10-13 441/week @ 2021-10-20 285/week @ 2021-10-27 109/week @ 2021-11-03 76/week @ 2021-11-10 81/week @ 2021-11-17 57/week @ 2021-11-24

336 downloads per month
Used in 3 crates (via vcard)

MIT license

29KB
576 lines

Base64 Stream

CI

To encode/decode large data with the standard Base64 encoding.

Examples

Encode

ToBase64Reader

extern crate base64_stream;

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);

ToBase64Writer

extern crate base64_stream;

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());

Decode

FromBase64Reader

extern crate base64_stream;

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);

FromBase64Writer

extern crate base64_stream;

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());

Change the Buffer Size

The default buffer size is 4096 bytes. If you want to change that, you can use the new2 associated function and define a length explicitly to create an instance of the above structs.

For example, to change the buffer size to 256 bytes,

extern crate base64_stream;

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);

Crates.io

https://crates.io/crates/base64-stream

Documentation

https://docs.rs/base64-stream

License

MIT

Dependencies

~1.6–2.1MB
~48K SLoC