compression_codecs/lzma/
encoder.rs

1use crate::{EncodeV2, Xz2Encoder, Xz2FileFormat};
2use compression_core::{
3    util::{PartialBuffer, WriteBuffer},
4    Level,
5};
6use std::io::Result;
7
8/// Lzma encoding stream
9#[derive(Debug)]
10pub struct LzmaEncoder {
11    inner: Xz2Encoder,
12}
13
14impl LzmaEncoder {
15    pub fn new(level: Level) -> Self {
16        Self {
17            inner: Xz2Encoder::new(Xz2FileFormat::Lzma, level),
18        }
19    }
20}
21
22impl From<Xz2Encoder> for LzmaEncoder {
23    fn from(inner: Xz2Encoder) -> Self {
24        Self { inner }
25    }
26}
27
28impl EncodeV2 for LzmaEncoder {
29    fn encode(
30        &mut self,
31        input: &mut PartialBuffer<&[u8]>,
32        output: &mut WriteBuffer<'_>,
33    ) -> Result<()> {
34        self.inner.encode(input, output)
35    }
36
37    fn flush(&mut self, _output: &mut WriteBuffer<'_>) -> Result<bool> {
38        // Flush on LZMA 1 is not supported
39        Ok(true)
40    }
41
42    fn finish(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool> {
43        self.inner.finish(output)
44    }
45}