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
191
192
193
194
195
196
197
use std::io::prelude::*;
use std::io::{self, BufReader};
use bufread;
use super::CompressParams;
pub struct BrotliEncoder<R: Read> {
inner: bufread::BrotliEncoder<BufReader<R>>,
}
pub struct BrotliDecoder<R: Read> {
inner: bufread::BrotliDecoder<BufReader<R>>,
}
impl<R: Read> BrotliEncoder<R> {
pub fn new(r: R, level: u32) -> BrotliEncoder<R> {
BrotliEncoder {
inner: bufread::BrotliEncoder::new(BufReader::new(r), level),
}
}
pub fn from_params( r: R, params: &CompressParams) -> BrotliEncoder<R> {
BrotliEncoder{
inner: bufread::BrotliEncoder::from_params(
BufReader::with_capacity(params.get_lgwin_readable(),r), params)
}
}
pub fn get_ref(&self) -> &R {
self.inner.get_ref().get_ref()
}
pub fn get_mut(&mut self) -> &mut R {
self.inner.get_mut().get_mut()
}
pub fn into_inner(self) -> R {
self.inner.into_inner().into_inner()
}
}
impl<R: Read> Read for BrotliEncoder<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.read(buf)
}
}
impl<R: Read> BrotliDecoder<R> {
pub fn new(r: R) -> BrotliDecoder<R> {
BrotliDecoder {
inner: bufread::BrotliDecoder::new(BufReader::new(r)),
}
}
pub fn get_ref(&self) -> &R {
self.inner.get_ref().get_ref()
}
pub fn get_mut(&mut self) -> &mut R {
self.inner.get_mut().get_mut()
}
pub fn into_inner(self) -> R {
self.inner.into_inner().into_inner()
}
}
impl<R: Read> Read for BrotliDecoder<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.read(buf)
}
}
#[cfg(test)]
mod tests {
use std::io::prelude::*;
use read::{BrotliEncoder, BrotliDecoder};
use rand::{thread_rng, Rng};
#[test]
fn smoke() {
let m: &[u8] = &[1, 2, 3, 4, 5, 6, 7, 8];
let mut c = BrotliEncoder::new(m, 6);
let mut data = vec![];
c.read_to_end(&mut data).unwrap();
let mut d = BrotliDecoder::new(&data[..]);
let mut data2 = Vec::new();
d.read_to_end(&mut data2).unwrap();
assert_eq!(data2, m);
}
#[test]
fn smoke2() {
let m: &[u8] = &[1, 2, 3, 4, 5, 6, 7, 8];
let c = BrotliEncoder::new(m, 6);
let mut d = BrotliDecoder::new(c);
let mut data = vec![];
d.read_to_end(&mut data).unwrap();
assert_eq!(data, [1, 2, 3, 4, 5, 6, 7, 8]);
}
#[test]
fn smoke3() {
let m = vec![3u8; 128 * 1024 + 1];
let c = BrotliEncoder::new(&m[..], 6);
let mut d = BrotliDecoder::new(c);
let mut data = vec![];
d.read_to_end(&mut data).unwrap();
assert!(data == &m[..]);
}
#[test]
fn self_terminating() {
let m = vec![3u8; 128 * 1024 + 1];
let mut c = BrotliEncoder::new(&m[..], 6);
let mut result = Vec::new();
c.read_to_end(&mut result).unwrap();
let v = thread_rng().gen_iter::<u8>().take(1024).collect::<Vec<_>>();
for _ in 0..200 {
result.extend(v.iter().map(|x| *x));
}
let mut d = BrotliDecoder::new(&result[..]);
let mut data = Vec::with_capacity(m.len());
unsafe { data.set_len(m.len()); }
assert!(d.read(&mut data).unwrap() == m.len());
assert!(data == &m[..]);
}
#[test]
fn zero_length_read_at_eof() {
let m = Vec::new();
let mut c = BrotliEncoder::new(&m[..], 6);
let mut result = Vec::new();
c.read_to_end(&mut result).unwrap();
let mut d = BrotliDecoder::new(&result[..]);
let mut data = Vec::new();
assert!(d.read(&mut data).unwrap() == 0);
}
#[test]
fn zero_length_read_with_data() {
let m = vec![3u8; 128 * 1024 + 1];
let mut c = BrotliEncoder::new(&m[..], 6);
let mut result = Vec::new();
c.read_to_end(&mut result).unwrap();
let mut d = BrotliDecoder::new(&result[..]);
let mut data = Vec::new();
assert!(d.read(&mut data).unwrap() == 0);
}
#[test]
fn qc() {
::quickcheck::quickcheck(test as fn(_) -> _);
fn test(v: Vec<u8>) -> bool {
let r = BrotliEncoder::new(&v[..], 6);
let mut r = BrotliDecoder::new(r);
let mut v2 = Vec::new();
r.read_to_end(&mut v2).unwrap();
v == v2
}
}
}