| 1 | use super::BufReadDecompressor; |
| 2 | use crate::lz4f::{FrameInfo, Result}; |
| 3 | use std::{ |
| 4 | borrow::Cow, |
| 5 | fmt, |
| 6 | io::{BufReader, Read}, |
| 7 | }; |
| 8 | |
| 9 | /// The [`Read`]-based streaming decompressor. |
| 10 | /// |
| 11 | /// # Example |
| 12 | /// |
| 13 | /// ``` |
| 14 | /// # use std::env; |
| 15 | /// # use std::path::Path; |
| 16 | /// # use lzzzz::{Error, Result}; |
| 17 | /// # use assert_fs::prelude::*; |
| 18 | /// # let tmp_dir = assert_fs::TempDir::new().unwrap().into_persistent(); |
| 19 | /// # env::set_current_dir(tmp_dir.path()).unwrap(); |
| 20 | /// # |
| 21 | /// # let mut buf = Vec::new(); |
| 22 | /// # lzzzz::lz4f::compress_to_vec(b"Hello world!", &mut buf, &Default::default())?; |
| 23 | /// # tmp_dir.child("foo.lz4").write_binary(&buf).unwrap(); |
| 24 | /// # |
| 25 | /// use lzzzz::lz4f::ReadDecompressor; |
| 26 | /// use std::{fs::File, io::prelude::*}; |
| 27 | /// |
| 28 | /// let mut f = File::open("foo.lz4")?; |
| 29 | /// let mut r = ReadDecompressor::new(&mut f)?; |
| 30 | /// |
| 31 | /// let mut buf = Vec::new(); |
| 32 | /// r.read_to_end(&mut buf)?; |
| 33 | /// # Ok::<(), std::io::Error>(()) |
| 34 | /// ``` |
| 35 | /// |
| 36 | /// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html |
| 37 | pub struct ReadDecompressor<'a, R: Read> { |
| 38 | inner: BufReadDecompressor<'a, BufReader<R>>, |
| 39 | } |
| 40 | |
| 41 | impl<R> fmt::Debug for ReadDecompressor<'_, R> |
| 42 | where |
| 43 | R: Read + fmt::Debug, |
| 44 | { |
| 45 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 46 | fmt.debug_struct("ReadDecompressor") |
| 47 | .field("reader", &self.inner.inner.get_ref()) |
| 48 | .finish() |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | impl<'a, R: Read> ReadDecompressor<'a, R> { |
| 53 | /// Creates a new `ReadDecompressor<R>`. |
| 54 | pub fn new(reader: R) -> Result<Self> { |
| 55 | Ok(Self { |
| 56 | inner: BufReadDecompressor::new(BufReader::new(reader))?, |
| 57 | }) |
| 58 | } |
| 59 | |
| 60 | /// Sets the dictionary. |
| 61 | pub fn set_dict<D>(&mut self, dict: D) |
| 62 | where |
| 63 | D: Into<Cow<'a, [u8]>>, |
| 64 | { |
| 65 | self.inner.set_dict(dict); |
| 66 | } |
| 67 | |
| 68 | /// Reads the frame header and returns `FrameInfo`. |
| 69 | /// |
| 70 | /// Calling this function before any `Read` operations |
| 71 | /// does not consume the frame body. |
| 72 | pub fn read_frame_info(&mut self) -> std::io::Result<FrameInfo> { |
| 73 | self.inner.read_frame_info() |
| 74 | } |
| 75 | |
| 76 | /// Returns ownership of the reader. |
| 77 | pub fn into_inner(self) -> R { |
| 78 | self.inner.into_inner().into_inner() |
| 79 | } |
| 80 | |
| 81 | /// Returns a mutable reference to the reader. |
| 82 | pub fn get_mut(&mut self) -> &mut R { |
| 83 | self.inner.get_mut().get_mut() |
| 84 | } |
| 85 | |
| 86 | /// Returns a shared reference to the reader. |
| 87 | pub fn get_ref(&self) -> &R { |
| 88 | self.inner.get_ref().get_ref() |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | impl<R: Read> Read for ReadDecompressor<'_, R> { |
| 93 | fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { |
| 94 | self.inner.read(buf) |
| 95 | } |
| 96 | } |