pub struct Reader<Input: BufRead> { /* private fields */ }
Expand description
Reader uncompress data provided by another reader (input source). Reader returns Ok(0) after the final block in the DEFLATE stream has been encountered. Any trailing data after the final block is ignored.
The input source must be buffered because the implementation uses many 1-byte reads.
Implementations§
Source§impl<'a, Input: BufRead> Reader<Input>
impl<'a, Input: BufRead> Reader<Input>
Sourcepub fn new(r: Input) -> Reader<Input> ⓘ
pub fn new(r: Input) -> Reader<Input> ⓘ
new returns a new Reader that can be used to read the uncompressed version of r. The reader returns std::io::Error::EOF after the final block in the DEFLATE stream has been encountered. Any trailing data after the final block is ignored.
Sourcepub fn new_dict(r: Input, dict: &'a [u8]) -> Reader<Input> ⓘ
pub fn new_dict(r: Input, dict: &'a [u8]) -> Reader<Input> ⓘ
new_dict is like new but initializes the reader with a preset dictionary. The returned Reader behaves as if the uncompressed data stream started with the given dictionary, which has already been read. new_dict is typically used to read data compressed by Writer::new_dict.
Examples found in repository?
16fn example_dictionary() {
17 // The dictionary is a string of bytes. When compressing some input data,
18 // the compressor will attempt to substitute substrings with matches found
19 // in the dictionary. As such, the dictionary should only contain substrings
20 // that are expected to be found in the actual data stream.
21 let dict = b"<?xml version=\"1.0\"?><book><data><meta name=\"\" content=\"";
22
23 // The data to compress should (but is not required to) contain frequent
24 // substrings that match those in the dictionary.
25 let data = r#"<?xml version="1.0"?>
26<book>
27 <meta name="title" content="The Go Programming Language"/>
28 <meta name="authors" content="Alan Donovan and Brian Kernighan"/>
29 <meta name="published" content="2015-10-26"/>
30 <meta name="isbn" content="978-0134190440"/>
31 <data>...</data>
32</book>
33"#;
34
35 let mut b = bytes::Buffer::new();
36
37 // Compress the data using the specially crafted dictionary.
38 {
39 let mut zw = flate::Writer::new_dict(&mut b, flate::DEFAULT_COMPRESSION, dict).unwrap();
40 let mut data_reader = bytes::new_buffer_string(data);
41 std::io::copy(&mut data_reader, &mut zw).unwrap();
42 zw.close().unwrap();
43 }
44
45 // The decompressor must use the same dictionary as the compressor.
46 // Otherwise, the input may appear as corrupted.
47 println!("Decompressed output using the dictionary:");
48 {
49 let mut data_reader = b.bytes();
50 let mut zr = flate::Reader::new_dict(&mut data_reader, dict);
51 let mut decompressed = bytes::Buffer::new();
52 std::io::copy(&mut zr, &mut decompressed).unwrap();
53 zr.close().unwrap();
54 println!("{}", String::from_utf8_lossy(decompressed.bytes()));
55 println!();
56 }
57
58 // Substitute all of the bytes in the dictionary with a '#' to visually
59 // demonstrate the approximate effectiveness of using a preset dictionary.
60 println!("Substrings matched by the dictionary are marked with #:");
61 {
62 let hash_dict = vec![b'#'; dict.len()];
63 let mut zr = flate::Reader::new_dict(&mut b, &hash_dict);
64 let mut decompressed = bytes::Buffer::new();
65 std::io::copy(&mut zr, &mut decompressed).unwrap();
66 zr.close().unwrap();
67 println!("{}", String::from_utf8_lossy(decompressed.bytes()));
68 }
69
70 // Output:
71 // Decompressed output using the dictionary:
72 // <?xml version="1.0"?>
73 // <book>
74 // <meta name="title" content="The Go Programming Language"/>
75 // <meta name="authors" content="Alan Donovan and Brian Kernighan"/>
76 // <meta name="published" content="2015-10-26"/>
77 // <meta name="isbn" content="978-0134190440"/>
78 // <data>...</data>
79 // </book>
80 //
81 // Substrings matched by the dictionary are marked with #:
82 // #####################
83 // ######
84 // ############title###########The Go Programming Language"/#
85 // ############authors###########Alan Donovan and Brian Kernighan"/#
86 // ############published###########2015-10-26"/#
87 // ############isbn###########978-0134190440"/#
88 // ######...</#####
89 // </#####
90}
Sourcepub fn close(&mut self) -> Result<()>
pub fn close(&mut self) -> Result<()>
Examples found in repository?
16fn example_dictionary() {
17 // The dictionary is a string of bytes. When compressing some input data,
18 // the compressor will attempt to substitute substrings with matches found
19 // in the dictionary. As such, the dictionary should only contain substrings
20 // that are expected to be found in the actual data stream.
21 let dict = b"<?xml version=\"1.0\"?><book><data><meta name=\"\" content=\"";
22
23 // The data to compress should (but is not required to) contain frequent
24 // substrings that match those in the dictionary.
25 let data = r#"<?xml version="1.0"?>
26<book>
27 <meta name="title" content="The Go Programming Language"/>
28 <meta name="authors" content="Alan Donovan and Brian Kernighan"/>
29 <meta name="published" content="2015-10-26"/>
30 <meta name="isbn" content="978-0134190440"/>
31 <data>...</data>
32</book>
33"#;
34
35 let mut b = bytes::Buffer::new();
36
37 // Compress the data using the specially crafted dictionary.
38 {
39 let mut zw = flate::Writer::new_dict(&mut b, flate::DEFAULT_COMPRESSION, dict).unwrap();
40 let mut data_reader = bytes::new_buffer_string(data);
41 std::io::copy(&mut data_reader, &mut zw).unwrap();
42 zw.close().unwrap();
43 }
44
45 // The decompressor must use the same dictionary as the compressor.
46 // Otherwise, the input may appear as corrupted.
47 println!("Decompressed output using the dictionary:");
48 {
49 let mut data_reader = b.bytes();
50 let mut zr = flate::Reader::new_dict(&mut data_reader, dict);
51 let mut decompressed = bytes::Buffer::new();
52 std::io::copy(&mut zr, &mut decompressed).unwrap();
53 zr.close().unwrap();
54 println!("{}", String::from_utf8_lossy(decompressed.bytes()));
55 println!();
56 }
57
58 // Substitute all of the bytes in the dictionary with a '#' to visually
59 // demonstrate the approximate effectiveness of using a preset dictionary.
60 println!("Substrings matched by the dictionary are marked with #:");
61 {
62 let hash_dict = vec![b'#'; dict.len()];
63 let mut zr = flate::Reader::new_dict(&mut b, &hash_dict);
64 let mut decompressed = bytes::Buffer::new();
65 std::io::copy(&mut zr, &mut decompressed).unwrap();
66 zr.close().unwrap();
67 println!("{}", String::from_utf8_lossy(decompressed.bytes()));
68 }
69
70 // Output:
71 // Decompressed output using the dictionary:
72 // <?xml version="1.0"?>
73 // <book>
74 // <meta name="title" content="The Go Programming Language"/>
75 // <meta name="authors" content="Alan Donovan and Brian Kernighan"/>
76 // <meta name="published" content="2015-10-26"/>
77 // <meta name="isbn" content="978-0134190440"/>
78 // <data>...</data>
79 // </book>
80 //
81 // Substrings matched by the dictionary are marked with #:
82 // #####################
83 // ######
84 // ############title###########The Go Programming Language"/#
85 // ############authors###########Alan Donovan and Brian Kernighan"/#
86 // ############published###########2015-10-26"/#
87 // ############isbn###########978-0134190440"/#
88 // ######...</#####
89 // </#####
90}
pub fn reset(&mut self, r: Input, dict: &[u8])
pub fn reset_state(&mut self, dict: &[u8])
Sourcepub fn input_reader(&mut self) -> &mut Input
pub fn input_reader(&mut self) -> &mut Input
input_reader returns mutable reference to the underlying reader.
Trait Implementations§
Source§impl<Input: BufRead> Read for Reader<Input>
impl<Input: BufRead> Read for Reader<Input>
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf
. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read more