-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathpartialreader.rs
214 lines (177 loc) · 6.81 KB
/
partialreader.rs
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
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
// spell-checker:ignore mockstream abcdefgh bcdefgh
use std::cmp;
use std::io;
use std::io::Read;
use crate::multifilereader::HasError;
/// When a large number of bytes must be skipped, it will be read into a
/// dynamically allocated buffer. The buffer will be limited to this size.
const MAX_SKIP_BUFFER: usize = 16 * 1024;
/// Wrapper for `std::io::Read` which can skip bytes at the beginning
/// of the input, and it can limit the returned bytes to a particular
/// number of bytes.
pub struct PartialReader<R> {
inner: R,
skip: u64,
limit: Option<u64>,
}
impl<R> PartialReader<R> {
/// Create a new `PartialReader` wrapping `inner`, which will skip
/// `skip` bytes, and limits the output to `limit` bytes. Set `limit`
/// to `None` if there should be no limit.
pub fn new(inner: R, skip: u64, limit: Option<u64>) -> Self {
Self { inner, skip, limit }
}
}
impl<R: Read> Read for PartialReader<R> {
fn read(&mut self, out: &mut [u8]) -> io::Result<usize> {
if self.skip > 0 {
let mut bytes = [0; MAX_SKIP_BUFFER];
while self.skip > 0 {
let skip_count: usize = cmp::min(self.skip as usize, MAX_SKIP_BUFFER);
match self.inner.read(&mut bytes[..skip_count])? {
0 => {
// this is an error as we still have more to skip
return Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"tried to skip past end of input",
));
}
n => self.skip -= n as u64,
}
}
}
match self.limit {
None => self.inner.read(out),
Some(0) => Ok(0),
Some(ref mut limit) => {
let slice = if *limit > (out.len() as u64) {
out
} else {
&mut out[0..(*limit as usize)]
};
match self.inner.read(slice) {
Err(e) => Err(e),
Ok(r) => {
*limit -= r as u64;
Ok(r)
}
}
}
}
}
}
impl<R: HasError> HasError for PartialReader<R> {
fn has_error(&self) -> bool {
self.inner.has_error()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::mockstream::*;
use std::io::{Cursor, ErrorKind, Read};
#[test]
fn test_read_without_limits() {
let mut v = [0; 10];
let mut sut = PartialReader::new(Cursor::new(&b"abcdefgh"[..]), 0, None);
assert_eq!(sut.read(v.as_mut()).unwrap(), 8);
assert_eq!(v, [0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0, 0]);
}
#[test]
fn test_read_without_limits_with_error() {
let mut v = [0; 10];
let f = FailingMockStream::new(ErrorKind::PermissionDenied, "No access", 3);
let mut sut = PartialReader::new(f, 0, None);
let error = sut.read(v.as_mut()).unwrap_err();
assert_eq!(error.kind(), ErrorKind::PermissionDenied);
assert_eq!(error.to_string(), "No access");
}
#[test]
fn test_read_skipping_bytes() {
let mut v = [0; 10];
let mut sut = PartialReader::new(Cursor::new(&b"abcdefgh"[..]), 2, None);
assert_eq!(sut.read(v.as_mut()).unwrap(), 6);
assert_eq!(v, [0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0, 0, 0, 0]);
}
#[test]
fn test_read_skipping_all() {
let mut v = [0; 10];
let mut sut = PartialReader::new(Cursor::new(&b"abcdefgh"[..]), 20, None);
let error = sut.read(v.as_mut()).unwrap_err();
assert_eq!(error.kind(), ErrorKind::UnexpectedEof);
}
#[test]
fn test_read_skipping_with_error() {
let mut v = [0; 10];
let f = FailingMockStream::new(ErrorKind::PermissionDenied, "No access", 3);
let mut sut = PartialReader::new(f, 2, None);
let error = sut.read(v.as_mut()).unwrap_err();
assert_eq!(error.kind(), ErrorKind::PermissionDenied);
assert_eq!(error.to_string(), "No access");
}
#[test]
fn test_read_skipping_with_two_reads_during_skip() {
let mut v = [0; 10];
let c = Cursor::new(&b"a"[..]).chain(Cursor::new(&b"bcdefgh"[..]));
let mut sut = PartialReader::new(c, 2, None);
assert_eq!(sut.read(v.as_mut()).unwrap(), 6);
assert_eq!(v, [0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0, 0, 0, 0]);
}
#[test]
fn test_read_skipping_huge_number() {
let mut v = [0; 10];
// test if it does not eat all memory....
let mut sut = PartialReader::new(Cursor::new(&b"abcdefgh"[..]), u64::MAX, None);
sut.read(v.as_mut()).unwrap_err();
}
#[test]
fn test_read_limiting_all() {
let mut v = [0; 10];
let mut sut = PartialReader::new(Cursor::new(&b"abcdefgh"[..]), 0, Some(0));
assert_eq!(sut.read(v.as_mut()).unwrap(), 0);
}
#[test]
fn test_read_limiting() {
let mut v = [0; 10];
let mut sut = PartialReader::new(Cursor::new(&b"abcdefgh"[..]), 0, Some(6));
assert_eq!(sut.read(v.as_mut()).unwrap(), 6);
assert_eq!(v, [0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0, 0, 0, 0]);
}
#[test]
fn test_read_limiting_with_error() {
let mut v = [0; 10];
let f = FailingMockStream::new(ErrorKind::PermissionDenied, "No access", 3);
let mut sut = PartialReader::new(f, 0, Some(6));
let error = sut.read(v.as_mut()).unwrap_err();
assert_eq!(error.kind(), ErrorKind::PermissionDenied);
assert_eq!(error.to_string(), "No access");
}
#[test]
fn test_read_limiting_with_large_limit() {
let mut v = [0; 10];
let mut sut = PartialReader::new(Cursor::new(&b"abcdefgh"[..]), 0, Some(20));
assert_eq!(sut.read(v.as_mut()).unwrap(), 8);
assert_eq!(v, [0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0, 0]);
}
#[test]
fn test_read_limiting_with_multiple_reads() {
let mut v = [0; 3];
let mut sut = PartialReader::new(Cursor::new(&b"abcdefgh"[..]), 0, Some(6));
assert_eq!(sut.read(v.as_mut()).unwrap(), 3);
assert_eq!(v, [0x61, 0x62, 0x63]);
assert_eq!(sut.read(v.as_mut()).unwrap(), 3);
assert_eq!(v, [0x64, 0x65, 0x66]);
assert_eq!(sut.read(v.as_mut()).unwrap(), 0);
}
#[test]
fn test_read_skipping_and_limiting() {
let mut v = [0; 10];
let mut sut = PartialReader::new(Cursor::new(&b"abcdefgh"[..]), 2, Some(4));
assert_eq!(sut.read(v.as_mut()).unwrap(), 4);
assert_eq!(v, [0x63, 0x64, 0x65, 0x66, 0, 0, 0, 0, 0, 0]);
}
}