-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
NullReader.java
executable file
·313 lines (294 loc) · 10.3 KB
/
NullReader.java
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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.io.input;
import java.io.EOFException;
import java.io.IOException;
import java.io.Reader;
/**
* A functional, light weight {@link Reader} that emulates
* a reader of a specified size.
* <p>
* This implementation provides a light weight
* object for testing with an {@link Reader}
* where the contents don't matter.
* <p>
* One use case would be for testing the handling of
* large {@link Reader} as it can emulate that
* scenario without the overhead of actually processing
* large numbers of characters - significantly speeding up
* test execution times.
* <p>
* This implementation returns a space from the method that
* reads a character and leaves the array unchanged in the read
* methods that are passed a character array.
* If alternative data is required the <code>processChar()</code> and
* <code>processChars()</code> methods can be implemented to generate
* data, for example:
*
* <pre>
* public class TestReader extends NullReader {
* public TestReader(int size) {
* super(size);
* }
* protected char processChar() {
* return ... // return required value here
* }
* protected void processChars(char[] chars, int offset, int length) {
* for (int i = offset; i < length; i++) {
* chars[i] = ... // set array value here
* }
* }
* }
* </pre>
*
* @since Commons IO 1.3
* @version $Revision: 463529 $
*/
public class NullReader extends Reader {
private long size;
private long position;
private long mark = -1;
private long readlimit;
private boolean eof;
private boolean throwEofException;
private boolean markSupported;
/**
* Create a {@link Reader} that emulates a specified size
* which supports marking and does not throw EOFException.
*
* @param size The size of the reader to emulate.
*/
public NullReader(long size) {
this(size, true, false);
}
/**
* Create a {@link Reader} that emulates a specified
* size with option settings.
*
* @param size The size of the reader to emulate.
* @param markSupported Whether this instance will support
* the <code>mark()</code> functionality.
* @param throwEofException Whether this implementation
* will throw an {@link EOFException} or return -1 when the
* end of file is reached.
*/
public NullReader(long size, boolean markSupported, boolean throwEofException) {
this.size = size;
this.markSupported = markSupported;
this.throwEofException = throwEofException;
}
/**
* Return the current position.
*
* @return the current position.
*/
public long getPosition() {
return position;
}
/**
* Return the size this {@link Reader} emulates.
*
* @return The size of the reader to emulate.
*/
public long getSize() {
return size;
}
/**
* Close this Reader - resets the internal state to
* the initial values.
*
* @throws IOException If an error occurs.
*/
public void close() throws IOException {
eof = false;
position = 0;
mark = -1;
}
/**
* Mark the current position.
*
* @param readlimit The number of characters before this marked position
* is invalid.
* @throws UnsupportedOperationException if mark is not supported.
*/
public synchronized void mark(int readlimit) {
if (!markSupported) {
throw new UnsupportedOperationException("Mark not supported");
}
mark = position;
this.readlimit = readlimit;
}
/**
* Indicates whether <i>mark</i> is supported.
*
* @return Whether <i>mark</i> is supported or not.
*/
public boolean markSupported() {
return markSupported;
}
/**
* Read a character.
*
* @return Either The character value returned by <code>processChar()</code>
* or <code>-1</code> if the end of file has been reached and
* <code>throwEofException</code> is set to <code>false</code>.
* @throws EOFException if the end of file is reached and
* <code>throwEofException</code> is set to <code>true</code>.
* @throws IOException if trying to read past the end of file.
*/
public int read() throws IOException {
if (eof) {
throw new IOException("Read after end of file");
}
if (position == size) {
return doEndOfFile();
}
position++;
return processChar();
}
/**
* Read some characters into the specified array.
*
* @param chars The character array to read into
* @return The number of characters read or <code>-1</code>
* if the end of file has been reached and
* <code>throwEofException</code> is set to <code>false</code>.
* @throws EOFException if the end of file is reached and
* <code>throwEofException</code> is set to <code>true</code>.
* @throws IOException if trying to read past the end of file.
*/
public int read(char[] chars) throws IOException {
return read(chars, 0, chars.length);
}
/**
* Read the specified number characters into an array.
*
* @param chars The character array to read into.
* @param offset The offset to start reading characters into.
* @param length The number of characters to read.
* @return The number of characters read or <code>-1</code>
* if the end of file has been reached and
* <code>throwEofException</code> is set to <code>false</code>.
* @throws EOFException if the end of file is reached and
* <code>throwEofException</code> is set to <code>true</code>.
* @throws IOException if trying to read past the end of file.
*/
public int read(char[] chars, int offset, int length) throws IOException {
if (eof) {
throw new IOException("Read after end of file");
}
if (position == size) {
return doEndOfFile();
}
position += length;
int returnLength = length;
if (position > size) {
returnLength = length - (int)(position - size);
position = size;
}
processChars(chars, offset, returnLength);
return returnLength;
}
/**
* Reset the stream to the point when mark was last called.
*
* @throws UnsupportedOperationException if mark is not supported.
* @throws IOException If no position has been marked
* or the read limit has been exceed since the last position was
* marked.
*/
public synchronized void reset() throws IOException {
if (!markSupported) {
throw new UnsupportedOperationException("Mark not supported");
}
if (mark < 0) {
throw new IOException("No position has been marked");
}
if (position > (mark + readlimit)) {
throw new IOException("Marked position [" + mark +
"] is no longer valid - passed the read limit [" +
readlimit + "]");
}
position = mark;
eof = false;
}
/**
* Skip a specified number of characters.
*
* @param numberOfChars The number of characters to skip.
* @return The number of characters skipped or <code>-1</code>
* if the end of file has been reached and
* <code>throwEofException</code> is set to <code>false</code>.
* @throws EOFException if the end of file is reached and
* <code>throwEofException</code> is set to <code>true</code>.
* @throws IOException if trying to read past the end of file.
*/
public long skip(long numberOfChars) throws IOException {
if (eof) {
throw new IOException("Skip after end of file");
}
if (position == size) {
return doEndOfFile();
}
position += numberOfChars;
long returnLength = numberOfChars;
if (position > size) {
returnLength = numberOfChars - (position - size);
position = size;
}
return returnLength;
}
/**
* Return a character value for the <code>read()</code> method.
* <p>
* This implementation returns zero.
*
* @return This implementation always returns zero.
*/
protected int processChar() {
// do nothing - overridable by subclass
return 0;
}
/**
* Process the characters for the <code>read(char[], offset, length)</code>
* method.
* <p>
* This implementation leaves the character array unchanged.
*
* @param chars The character array
* @param offset The offset to start at.
* @param length The number of characters.
*/
protected void processChars(char[] chars, int offset, int length) {
// do nothing - overridable by subclass
}
/**
* Handle End of File.
*
* @return <code>-1</code> if <code>throwEofException</code> is
* set to <code>false</code>
* @throws EOFException if <code>throwEofException</code> is set
* to <code>true</code>.
*/
private int doEndOfFile() throws EOFException {
eof = true;
if (throwEofException) {
throw new EOFException();
}
return -1;
}
}