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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use std::io::prelude::*;
use std::io;
use std::iter::repeat;
use raw;
pub struct EncoderWriter<W: Write> {
inner: raw::EncoderWriter<W>,
}
pub struct EncoderReader<R: Read> {
inner: raw::EncoderReader<R>,
}
pub struct DecoderReader<R: Read> {
inner: raw::DecoderReader<R>,
}
pub struct DecoderWriter<W: Write> {
inner: raw::DecoderWriter<W>,
}
impl<W: Write> EncoderWriter<W> {
pub fn new(w: W, level: ::Compression) -> EncoderWriter<W> {
EncoderWriter {
inner: raw::EncoderWriter::new(w, level, false,
Vec::with_capacity(32 * 1024)),
}
}
pub fn finish(mut self) -> io::Result<W> {
try!(self.inner.finish());
Ok(self.inner.into_inner())
}
}
impl<W: Write> Write for EncoderWriter<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { self.inner.write(buf) }
fn flush(&mut self) -> io::Result<()> { self.inner.flush() }
}
impl<R: Read> EncoderReader<R> {
pub fn new(r: R, level: ::Compression) -> EncoderReader<R> {
EncoderReader {
inner: raw::EncoderReader::new(r, level, false,
repeat(0).take(32 * 1024).collect())
}
}
pub fn into_inner(self) -> R {
self.inner.into_inner()
}
}
impl<R: Read> Read for EncoderReader<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.read(buf)
}
}
impl<R: Read> DecoderReader<R> {
pub fn new(r: R) -> DecoderReader<R> {
DecoderReader::new_with_buf(r, repeat(0).take(32 * 1024).collect())
}
pub fn new_with_buf(r: R, buf: Vec<u8>) -> DecoderReader<R> {
DecoderReader { inner: raw::DecoderReader::new(r, false, buf) }
}
pub fn into_inner(self) -> R {
self.inner.into_inner()
}
}
impl<R: Read> Read for DecoderReader<R> {
fn read(&mut self, into: &mut [u8]) -> io::Result<usize> {
self.inner.read(into)
}
}
impl<W: Write> DecoderWriter<W> {
pub fn new(w: W) -> DecoderWriter<W> {
DecoderWriter {
inner: raw::DecoderWriter::new(w, false,
Vec::with_capacity(32 * 1024)),
}
}
pub fn finish(mut self) -> io::Result<W> {
try!(self.inner.finish());
Ok(self.inner.into_inner())
}
}
impl<W: Write> Write for DecoderWriter<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { self.inner.write(buf) }
fn flush(&mut self) -> io::Result<()> { self.inner.flush() }
}
#[cfg(test)]
mod tests {
use std::io::prelude::*;
use rand::{thread_rng, Rng};
use zlib::{EncoderWriter, EncoderReader, DecoderReader, DecoderWriter};
use Compression::Default;
#[test]
fn roundtrip() {
let mut real = Vec::new();
let mut w = EncoderWriter::new(Vec::new(), Default);
let v = thread_rng().gen_iter::<u8>().take(1024).collect::<Vec<_>>();
for _ in 0..200 {
let to_write = &v[..thread_rng().gen_range(0, v.len())];
real.extend(to_write.iter().map(|x| *x));
w.write_all(to_write).unwrap();
}
let result = w.finish().unwrap();
let mut r = DecoderReader::new(&result[..]);
let mut ret = Vec::new();
r.read_to_end(&mut ret).unwrap();
assert!(ret == real);
}
#[test]
fn roundtrip2() {
let v = thread_rng().gen_iter::<u8>().take(1024 * 1024).collect::<Vec<_>>();
let mut r = DecoderReader::new(EncoderReader::new(&v[..], Default));
let mut ret = Vec::new();
r.read_to_end(&mut ret).unwrap();
assert_eq!(ret, v);
}
#[test]
fn roundtrip3() {
let v = thread_rng().gen_iter::<u8>().take(1024 * 1024).collect::<Vec<_>>();
let mut w = EncoderWriter::new(DecoderWriter::new(Vec::new()), Default);
w.write_all(&v).unwrap();
let w = w.finish().unwrap().finish().unwrap();
assert!(w == v);
}
}