1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use crate::lz4f::{Decompressor, FrameInfo, Result};
use std::{borrow::Cow, fmt, io::Write};
pub struct WriteDecompressor<'a, W: Write> {
inner: W,
decomp: Decompressor<'a>,
}
impl<'a, W: Write> WriteDecompressor<'a, W> {
pub fn new(writer: W) -> Result<Self> {
Ok(Self {
inner: writer,
decomp: Decompressor::new()?,
})
}
pub fn set_dict<D>(&mut self, dict: D)
where
D: Into<Cow<'a, [u8]>>,
{
self.decomp.set_dict(dict);
}
pub fn frame_info(&self) -> Option<FrameInfo> {
self.decomp.frame_info()
}
pub fn decode_header_only(&mut self, flag: bool) {
self.decomp.decode_header_only(flag);
}
pub fn get_mut(&mut self) -> &mut W {
&mut self.inner
}
pub fn get_ref(&self) -> &W {
&self.inner
}
pub fn into_inner(self) -> W {
self.inner
}
}
impl<W> fmt::Debug for WriteDecompressor<'_, W>
where
W: Write + fmt::Debug,
{
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("WriteDecompressor")
.field("writer", &self.inner)
.finish()
}
}
impl<W: Write> Write for WriteDecompressor<'_, W> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
let report = self.decomp.decompress(buf)?;
self.inner.write_all(self.decomp.buf())?;
self.decomp.clear_buf();
Ok(report)
}
fn flush(&mut self) -> std::io::Result<()> {
self.inner.flush()
}
}