Struct Reader

Source
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>

Source

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.

Source

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?
examples/flate-dict.rs (line 50)
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}
Source

pub fn close(&mut self) -> Result<()>

Examples found in repository?
examples/flate-dict.rs (line 53)
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}
Source

pub fn reset(&mut self, r: Input, dict: &[u8])

Source

pub fn reset_state(&mut self, dict: &[u8])

Source

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>

Source§

fn read(&mut self, buf: &mut [u8]) -> Result<usize>

Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
1.36.0 · Source§

fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>

Like read, except that it reads into a slice of buffers. Read more
Source§

fn is_read_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
1.0.0 · Source§

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>

Reads all bytes until EOF in this source, placing them into buf. Read more
1.0.0 · Source§

fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>

Reads all bytes until EOF in this source, appending them to buf. Read more
1.6.0 · Source§

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>

Reads the exact number of bytes required to fill buf. Read more
Source§

fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
Source§

fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Reads the exact number of bytes required to fill cursor. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adaptor for this instance of Read. Read more
1.0.0 · Source§

fn bytes(self) -> Bytes<Self>
where Self: Sized,

Transforms this Read instance to an Iterator over its bytes. Read more
1.0.0 · Source§

fn chain<R>(self, next: R) -> Chain<Self, R>
where R: Read, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
1.0.0 · Source§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more
Source§

impl<Input: BufRead> Reader for Reader<Input>

Source§

fn read(&mut self, p: &mut [u8]) -> IoRes

read reads up to len(p) bytes into p. It returns the number of bytes read (0 <= n <= len(p)) and any error encountered. Even if read returns n < len(p), it may use all of p as scratch space during the call. If some data is available but not len(p) bytes, read conventionally returns what is available instead of waiting for more. Read more

Auto Trait Implementations§

§

impl<Input> Freeze for Reader<Input>
where Input: Freeze,

§

impl<Input> !RefUnwindSafe for Reader<Input>

§

impl<Input> Send for Reader<Input>
where Input: Send,

§

impl<Input> Sync for Reader<Input>
where Input: Sync,

§

impl<Input> Unpin for Reader<Input>
where Input: Unpin,

§

impl<Input> !UnwindSafe for Reader<Input>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.