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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
use std::io;
use std::fmt;
use std::cmp;
use std::io::{Error, ErrorKind};
use super::*;
/// Wraps a `Read`er.
///
/// This is useful when reading from a file, and it even works with a
/// `&[u8]` (but `BufferedReaderMemory` is more efficient).
pub struct BufferedReaderGeneric<T: io::Read, C> {
buffer: Option<Box<[u8]>>,
// The next byte to read in the buffer.
cursor: usize,
// The preferred chunk size. This is just a hint.
preferred_chunk_size: usize,
// XXX: This is pub for the decompressors. It would be better to
// change this to some accessor method.
pub reader: Box<T>,
// Whether we saw an EOF.
saw_eof: bool,
// The last error that we encountered, but have not yet returned.
error: Option<io::Error>,
// The user settable cookie.
cookie: C,
}
impl<T: io::Read, C> fmt::Debug for BufferedReaderGeneric<T, C> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let buffered_data = if let Some(ref buffer) = self.buffer {
buffer.len() - self.cursor
} else {
0
};
f.debug_struct("BufferedReaderGeneric")
.field("preferred_chunk_size", &self.preferred_chunk_size)
.field("buffer data", &buffered_data)
.field("saw eof", &self.saw_eof)
.field("error", &self.error)
.finish()
}
}
impl<T: io::Read> BufferedReaderGeneric<T, ()> {
/// Instantiate a new generic reader. `reader` is the source to
/// wrap. `preferred_chuck_size` is the preferred chuck size. If
/// None, then the default will be used, which is usually what you
/// want.
pub fn new(reader: T, preferred_chunk_size: Option<usize>) -> Self {
Self::with_cookie(reader, preferred_chunk_size, ())
}
}
impl<T: io::Read, C> BufferedReaderGeneric<T, C> {
/// Like `new()`, but sets a cookie, which can be retrieved using
/// the `cookie_ref` and `cookie_mut` methods, and set using
/// the `cookie_set` method.
pub fn with_cookie(
reader: T, preferred_chunk_size: Option<usize>, cookie: C)
-> Self {
BufferedReaderGeneric {
buffer: None,
cursor: 0,
preferred_chunk_size:
if let Some(s) = preferred_chunk_size { s }
else { DEFAULT_BUF_SIZE },
reader: Box::new(reader),
saw_eof: false,
error: None,
cookie: cookie,
}
}
/// Return the buffer. Ensure that it contains at least `amount`
/// bytes.
fn data_helper(&mut self, amount: usize, hard: bool, and_consume: bool)
-> Result<&[u8], io::Error> {
// println!("BufferedReaderGeneric.data_helper(\
// amount: {}, hard: {}, and_consume: {} (cursor: {}, buffer: {:?})",
// amount, hard, and_consume,
// self.cursor,
// if let Some(ref buffer) = self.buffer { Some(buffer.len()) }
// else { None });
if let Some(ref buffer) = self.buffer {
// We have a buffer. Make sure `cursor` is sane.
assert!(self.cursor <= buffer.len());
} else {
// We don't have a buffer. Make sure cursor is 0.
assert_eq!(self.cursor, 0);
}
let amount_buffered =
if let Some(ref buffer) = self.buffer { buffer.len() } else { 0 }
- self.cursor;
if !self.saw_eof && amount > amount_buffered {
// The caller wants more data than we have readily
// available. Read some more.
let capacity : usize = cmp::max(cmp::max(
DEFAULT_BUF_SIZE,
2 * self.preferred_chunk_size), amount);
let mut buffer_new : Vec<u8> = vec![0u8; capacity];
let mut amount_read = 0;
while amount_buffered + amount_read < amount {
match self.reader.read(&mut buffer_new
[amount_buffered + amount_read..]) {
Ok(read) => {
if read == 0 {
// XXX: Likely EOF.
self.saw_eof = true;
break;
} else {
amount_read += read;
continue;
}
},
Err(err) => {
// Don't return yet, because we may have
// actually read something.
self.saw_eof = true;
self.error = Some(err);
break;
},
}
}
if amount_read > 0 {
// We read something.
if let Some(ref buffer) = self.buffer {
// We need to copy in the old data.
buffer_new[0..amount_buffered]
.copy_from_slice(
&buffer[self.cursor..self.cursor + amount_buffered]);
}
buffer_new.truncate(amount_buffered + amount_read);
buffer_new.shrink_to_fit();
self.buffer = Some(buffer_new.into_boxed_slice());
self.cursor = 0;
}
}
if self.error.is_some() {
// An error occured. If we have enough data to fulfill
// the caller's request, then delay returning the error.
if let Some(ref buffer) = self.buffer {
if amount > buffer.len() {
// We return an error at most once (Recall: take
// clears self.error).
return Err(self.error.take().unwrap());
}
}
}
match self.buffer {
Some(ref buffer) => {
let amount_buffered = buffer.len() - self.cursor;
if hard && amount_buffered < amount {
return Err(Error::new(ErrorKind::UnexpectedEof, "EOF"));
}
if and_consume {
let amount_consumed = cmp::min(amount_buffered, amount);
self.cursor += amount_consumed;
assert!(self.cursor <= buffer.len());
return Ok(&buffer[self.cursor-amount_consumed..]);
} else
|