compression_codecs/bzip2/
encoder.rs

1use crate::{bzip2::params::Bzip2EncoderParams, Encode};
2use bzip2::{Action, Compress, Compression, Status};
3use compression_core::util::PartialBuffer;
4use std::{fmt, io};
5
6pub struct BzEncoder {
7    compress: Compress,
8}
9
10impl fmt::Debug for BzEncoder {
11    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
12        write!(
13            f,
14            "BzEncoder {{total_in: {}, total_out: {}}}",
15            self.compress.total_in(),
16            self.compress.total_out()
17        )
18    }
19}
20
21impl BzEncoder {
22    /// Creates a new stream prepared for compression.
23    ///
24    /// The `work_factor` parameter controls how the compression phase behaves
25    /// when presented with worst case, highly repetitive, input data. If
26    /// compression runs into difficulties caused by repetitive data, the
27    /// library switches from the standard sorting algorithm to a fallback
28    /// algorithm. The fallback is slower than the standard algorithm by perhaps
29    /// a factor of three, but always behaves reasonably, no matter how bad the
30    /// input.
31    ///
32    /// Lower values of `work_factor` reduce the amount of effort the standard
33    /// algorithm will expend before resorting to the fallback. You should set
34    /// this parameter carefully; too low, and many inputs will be handled by
35    /// the fallback algorithm and so compress rather slowly, too high, and your
36    /// average-to-worst case compression times can become very large. The
37    /// default value of 30 gives reasonable behaviour over a wide range of
38    /// circumstances.
39    ///
40    /// Allowable values range from 0 to 250 inclusive. 0 is a special case,
41    /// equivalent to using the default value of 30.
42    pub fn new(params: Bzip2EncoderParams, work_factor: u32) -> Self {
43        let params = Compression::from(params);
44        Self {
45            compress: Compress::new(params, work_factor),
46        }
47    }
48
49    fn encode(
50        &mut self,
51        input: &mut PartialBuffer<impl AsRef<[u8]>>,
52        output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
53        action: Action,
54    ) -> io::Result<Status> {
55        let prior_in = self.compress.total_in();
56        let prior_out = self.compress.total_out();
57
58        let status = self
59            .compress
60            .compress(input.unwritten(), output.unwritten_mut(), action)
61            .map_err(io::Error::other)?;
62
63        input.advance((self.compress.total_in() - prior_in) as usize);
64        output.advance((self.compress.total_out() - prior_out) as usize);
65
66        Ok(status)
67    }
68}
69
70impl Encode for BzEncoder {
71    fn encode(
72        &mut self,
73        input: &mut PartialBuffer<impl AsRef<[u8]>>,
74        output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
75    ) -> io::Result<()> {
76        match self.encode(input, output, Action::Run)? {
77            // Decompression went fine, nothing much to report.
78            Status::Ok => Ok(()),
79
80            // The Flush action on a compression went ok.
81            Status::FlushOk => unreachable!(),
82
83            // The Run action on compression went ok.
84            Status::RunOk => Ok(()),
85
86            // The Finish action on compression went ok.
87            Status::FinishOk => unreachable!(),
88
89            // The stream's end has been met, meaning that no more data can be input.
90            Status::StreamEnd => unreachable!(),
91
92            // There was insufficient memory in the input or output buffer to complete
93            // the request, but otherwise everything went normally.
94            Status::MemNeeded => Err(io::Error::other("out of memory")),
95        }
96    }
97
98    fn flush(
99        &mut self,
100        output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
101    ) -> io::Result<bool> {
102        match self.encode(&mut PartialBuffer::new(&[][..]), output, Action::Flush)? {
103            // Decompression went fine, nothing much to report.
104            Status::Ok => unreachable!(),
105
106            // The Flush action on a compression went ok.
107            Status::FlushOk => Ok(false),
108
109            // The Run action on compression went ok.
110            Status::RunOk => Ok(true),
111
112            // The Finish action on compression went ok.
113            Status::FinishOk => unreachable!(),
114
115            // The stream's end has been met, meaning that no more data can be input.
116            Status::StreamEnd => unreachable!(),
117
118            // There was insufficient memory in the input or output buffer to complete
119            // the request, but otherwise everything went normally.
120            Status::MemNeeded => Err(io::Error::other("out of memory")),
121        }
122    }
123
124    fn finish(
125        &mut self,
126        output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
127    ) -> io::Result<bool> {
128        match self.encode(&mut PartialBuffer::new(&[][..]), output, Action::Finish)? {
129            // Decompression went fine, nothing much to report.
130            Status::Ok => Ok(false),
131
132            // The Flush action on a compression went ok.
133            Status::FlushOk => unreachable!(),
134
135            // The Run action on compression went ok.
136            Status::RunOk => unreachable!(),
137
138            // The Finish action on compression went ok.
139            Status::FinishOk => Ok(false),
140
141            // The stream's end has been met, meaning that no more data can be input.
142            Status::StreamEnd => Ok(true),
143
144            // There was insufficient memory in the input or output buffer to complete
145            // the request, but otherwise everything went normally.
146            Status::MemNeeded => Err(io::Error::other("out of memory")),
147        }
148    }
149}