| 1 | use super::{Compressor, Dictionary, Preferences}; |
| 2 | use crate::lz4f::Result; |
| 3 | use std::{ |
| 4 | fmt, |
| 5 | io::{BufRead, Read}, |
| 6 | }; |
| 7 | |
| 8 | /// The [`BufRead`]-based streaming compressor. |
| 9 | /// |
| 10 | /// # Example |
| 11 | /// |
| 12 | /// ``` |
| 13 | /// # use std::env; |
| 14 | /// # use std::path::Path; |
| 15 | /// # use lzzzz::{Error, Result}; |
| 16 | /// # use assert_fs::prelude::*; |
| 17 | /// # let tmp_dir = assert_fs::TempDir::new().unwrap().into_persistent(); |
| 18 | /// # env::set_current_dir(tmp_dir.path()).unwrap(); |
| 19 | /// # |
| 20 | /// # tmp_dir.child("foo.txt").write_str("Hello").unwrap(); |
| 21 | /// # |
| 22 | /// use lzzzz::lz4f::BufReadCompressor; |
| 23 | /// use std::{ |
| 24 | /// fs::File, |
| 25 | /// io::{prelude::*, BufReader}, |
| 26 | /// }; |
| 27 | /// |
| 28 | /// let mut f = File::open("foo.txt")?; |
| 29 | /// let mut b = BufReader::new(f); |
| 30 | /// let mut r = BufReadCompressor::new(&mut b, Default::default())?; |
| 31 | /// |
| 32 | /// let mut buf = Vec::new(); |
| 33 | /// r.read_to_end(&mut buf)?; |
| 34 | /// # Ok::<(), std::io::Error>(()) |
| 35 | /// ``` |
| 36 | /// |
| 37 | /// [`BufRead`]: https://doc.rust-lang.org/std/io/trait.BufRead.html |
| 38 | pub struct BufReadCompressor<R: BufRead> { |
| 39 | pub(super) inner: R, |
| 40 | pub(super) comp: Compressor, |
| 41 | consumed: usize, |
| 42 | } |
| 43 | |
| 44 | impl<R: BufRead> BufReadCompressor<R> { |
| 45 | /// Creates a new `BufReadCompressor<R>`. |
| 46 | pub fn new(reader: R, prefs: Preferences) -> Result<Self> { |
| 47 | Ok(Self { |
| 48 | inner: reader, |
| 49 | comp: Compressor::new(prefs, None)?, |
| 50 | consumed: 0, |
| 51 | }) |
| 52 | } |
| 53 | |
| 54 | /// Creates a new `BufReadCompressor<R>` with a dictionary. |
| 55 | pub fn with_dict(reader: R, prefs: Preferences, dict: Dictionary) -> Result<Self> { |
| 56 | Ok(Self { |
| 57 | inner: reader, |
| 58 | comp: Compressor::new(prefs, Some(dict))?, |
| 59 | consumed: 0, |
| 60 | }) |
| 61 | } |
| 62 | |
| 63 | /// Returns ownership of the reader. |
| 64 | pub fn into_inner(self) -> R { |
| 65 | self.inner |
| 66 | } |
| 67 | |
| 68 | /// Returns a mutable reference to the reader. |
| 69 | pub fn get_mut(&mut self) -> &mut R { |
| 70 | &mut self.inner |
| 71 | } |
| 72 | |
| 73 | /// Returns a shared reference to the reader. |
| 74 | pub fn get_ref(&self) -> &R { |
| 75 | &self.inner |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | impl<R> fmt::Debug for BufReadCompressor<R> |
| 80 | where |
| 81 | R: BufRead + fmt::Debug, |
| 82 | { |
| 83 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 84 | fmt.debug_struct("BufReadCompressor") |
| 85 | .field("reader", &self.inner) |
| 86 | .field("prefs", &self.comp.prefs()) |
| 87 | .finish() |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | impl<R: BufRead> Read for BufReadCompressor<R> { |
| 92 | fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { |
| 93 | let consumed = { |
| 94 | let inner_buf = self.inner.fill_buf()?; |
| 95 | if inner_buf.is_empty() { |
| 96 | self.comp.end(false)?; |
| 97 | if self.comp.buf().is_empty() { |
| 98 | return Ok(0); |
| 99 | } |
| 100 | 0 |
| 101 | } else { |
| 102 | self.comp.update(inner_buf, false)?; |
| 103 | inner_buf.len() |
| 104 | } |
| 105 | }; |
| 106 | self.inner.consume(consumed); |
| 107 | |
| 108 | let len = std::cmp::min(buf.len(), self.comp.buf().len() - self.consumed); |
| 109 | buf[..len].copy_from_slice(&self.comp.buf()[self.consumed..][..len]); |
| 110 | self.consumed += len; |
| 111 | if self.consumed >= self.comp.buf().len() { |
| 112 | self.comp.clear_buf(); |
| 113 | self.consumed = 0; |
| 114 | } |
| 115 | Ok(len) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | impl<R: BufRead> BufRead for BufReadCompressor<R> { |
| 120 | fn fill_buf(&mut self) -> std::io::Result<&[u8]> { |
| 121 | let _ = self.read(&mut [])?; |
| 122 | Ok(&self.comp.buf()[self.consumed..]) |
| 123 | } |
| 124 | |
| 125 | fn consume(&mut self, amt: usize) { |
| 126 | self.consumed += amt; |
| 127 | if self.consumed >= self.comp.buf().len() { |
| 128 | self.comp.clear_buf(); |
| 129 | self.consumed = 0; |
| 130 | } |
| 131 | } |
| 132 | } |