View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.io.input;
18  
19  import static org.apache.commons.io.IOUtils.EOF;
20  
21  import java.io.EOFException;
22  import java.io.IOException;
23  import java.io.Reader;
24  
25  /**
26   * A functional, light weight {@link Reader} that emulates
27   * a reader of a specified size.
28   * <p>
29   * This implementation provides a light weight
30   * object for testing with an {@link Reader}
31   * where the contents don't matter.
32   * <p>
33   * One use case would be for testing the handling of
34   * large {@link Reader} as it can emulate that
35   * scenario without the overhead of actually processing
36   * large numbers of characters - significantly speeding up
37   * test execution times.
38   * <p>
39   * This implementation returns a space from the method that
40   * reads a character and leaves the array unchanged in the read
41   * methods that are passed a character array.
42   * If alternative data is required the <code>processChar()</code> and
43   * <code>processChars()</code> methods can be implemented to generate
44   * data, for example:
45   *
46   * <pre>
47   *  public class TestReader extends NullReader {
48   *      public TestReader(int size) {
49   *          super(size);
50   *      }
51   *      protected char processChar() {
52   *          return ... // return required value here
53   *      }
54   *      protected void processChars(char[] chars, int offset, int length) {
55   *          for (int i = offset; i &lt; length; i++) {
56   *              chars[i] = ... // set array value here
57   *          }
58   *      }
59   *  }
60   * </pre>
61   *
62   * @since 1.3
63   * @version $Id: NullReader.java 1586350 2014-04-10 15:57:20Z ggregory $
64   */
65  public class NullReader extends Reader {
66  
67      private final long size;
68      private long position;
69      private long mark = -1;
70      private long readlimit;
71      private boolean eof;
72      private final boolean throwEofException;
73      private final boolean markSupported;
74  
75      /**
76       * Create a {@link Reader} that emulates a specified size
77       * which supports marking and does not throw EOFException.
78       *
79       * @param size The size of the reader to emulate.
80       */
81      public NullReader(final long size) {
82         this(size, true, false);
83      }
84  
85      /**
86       * Create a {@link Reader} that emulates a specified
87       * size with option settings.
88       *
89       * @param size The size of the reader to emulate.
90       * @param markSupported Whether this instance will support
91       * the <code>mark()</code> functionality.
92       * @param throwEofException Whether this implementation
93       * will throw an {@link EOFException} or return -1 when the
94       * end of file is reached.
95       */
96      public NullReader(final long size, final boolean markSupported, final boolean throwEofException) {
97         this.size = size;
98         this.markSupported = markSupported;
99         this.throwEofException = throwEofException;
100     }
101 
102     /**
103      * Return the current position.
104      *
105      * @return the current position.
106      */
107     public long getPosition() {
108         return position;
109     }
110 
111     /**
112      * Return the size this {@link Reader} emulates.
113      *
114      * @return The size of the reader to emulate.
115      */
116     public long getSize() {
117         return size;
118     }
119 
120     /**
121      * Close this Reader - resets the internal state to
122      * the initial values.
123      *
124      * @throws IOException If an error occurs.
125      */
126     @Override
127     public void close() throws IOException {
128         eof = false;
129         position = 0;
130         mark = -1;
131     }
132 
133     /**
134      * Mark the current position.
135      *
136      * @param readlimit The number of characters before this marked position
137      * is invalid.
138      * @throws UnsupportedOperationException if mark is not supported.
139      */
140     @Override
141     public synchronized void mark(final int readlimit) {
142         if (!markSupported) {
143             throw new UnsupportedOperationException("Mark not supported");
144         }
145         mark = position;
146         this.readlimit = readlimit;
147     }
148 
149     /**
150      * Indicates whether <i>mark</i> is supported.
151      *
152      * @return Whether <i>mark</i> is supported or not.
153      */
154     @Override
155     public boolean markSupported() {
156         return markSupported;
157     }
158 
159     /**
160      * Read a character.
161      *
162      * @return Either The character value returned by <code>processChar()</code>
163      * or <code>-1</code> if the end of file has been reached and
164      * <code>throwEofException</code> is set to {@code false}.
165      * @throws EOFException if the end of file is reached and
166      * <code>throwEofException</code> is set to {@code true}.
167      * @throws IOException if trying to read past the end of file.
168      */
169     @Override
170     public int read() throws IOException {
171         if (eof) {
172             throw new IOException("Read after end of file");
173         }
174         if (position == size) {
175             return doEndOfFile();
176         }
177         position++;
178         return processChar();
179     }
180 
181     /**
182      * Read some characters into the specified array.
183      *
184      * @param chars The character array to read into
185      * @return The number of characters read or <code>-1</code>
186      * if the end of file has been reached and
187      * <code>throwEofException</code> is set to {@code false}.
188      * @throws EOFException if the end of file is reached and
189      * <code>throwEofException</code> is set to {@code true}.
190      * @throws IOException if trying to read past the end of file.
191      */
192     @Override
193     public int read(final char[] chars) throws IOException {
194         return read(chars, 0, chars.length);
195     }
196 
197     /**
198      * Read the specified number characters into an array.
199      *
200      * @param chars The character array to read into.
201      * @param offset The offset to start reading characters into.
202      * @param length The number of characters to read.
203      * @return The number of characters read or <code>-1</code>
204      * if the end of file has been reached and
205      * <code>throwEofException</code> is set to {@code false}.
206      * @throws EOFException if the end of file is reached and
207      * <code>throwEofException</code> is set to {@code true}.
208      * @throws IOException if trying to read past the end of file.
209      */
210     @Override
211     public int read(final char[] chars, final int offset, final int length) throws IOException {
212         if (eof) {
213             throw new IOException("Read after end of file");
214         }
215         if (position == size) {
216             return doEndOfFile();
217         }
218         position += length;
219         int returnLength = length;
220         if (position > size) {
221             returnLength = length - (int)(position - size);
222             position = size;
223         }
224         processChars(chars, offset, returnLength);
225         return returnLength;
226     }
227 
228     /**
229      * Reset the stream to the point when mark was last called.
230      *
231      * @throws UnsupportedOperationException if mark is not supported.
232      * @throws IOException If no position has been marked
233      * or the read limit has been exceed since the last position was
234      * marked.
235      */
236     @Override
237     public synchronized void reset() throws IOException {
238         if (!markSupported) {
239             throw new UnsupportedOperationException("Mark not supported");
240         }
241         if (mark < 0) {
242             throw new IOException("No position has been marked");
243         }
244         if (position > mark + readlimit) {
245             throw new IOException("Marked position [" + mark +
246                     "] is no longer valid - passed the read limit [" +
247                     readlimit + "]");
248         }
249         position = mark;
250         eof = false;
251     }
252 
253     /**
254      * Skip a specified number of characters.
255      *
256      * @param numberOfChars The number of characters to skip.
257      * @return The number of characters skipped or <code>-1</code>
258      * if the end of file has been reached and
259      * <code>throwEofException</code> is set to {@code false}.
260      * @throws EOFException if the end of file is reached and
261      * <code>throwEofException</code> is set to {@code true}.
262      * @throws IOException if trying to read past the end of file.
263      */
264     @Override
265     public long skip(final long numberOfChars) throws IOException {
266         if (eof) {
267             throw new IOException("Skip after end of file");
268         }
269         if (position == size) {
270             return doEndOfFile();
271         }
272         position += numberOfChars;
273         long returnLength = numberOfChars;
274         if (position > size) {
275             returnLength = numberOfChars - (position - size);
276             position = size;
277         }
278         return returnLength;
279     }
280 
281     /**
282      * Return a character value for the  <code>read()</code> method.
283      * <p>
284      * This implementation returns zero.
285      *
286      * @return This implementation always returns zero.
287      */
288     protected int processChar() {
289         // do nothing - overridable by subclass
290         return 0;
291     }
292 
293     /**
294      * Process the characters for the <code>read(char[], offset, length)</code>
295      * method.
296      * <p>
297      * This implementation leaves the character array unchanged.
298      *
299      * @param chars The character array
300      * @param offset The offset to start at.
301      * @param length The number of characters.
302      */
303     protected void processChars(final char[] chars, final int offset, final int length) {
304         // do nothing - overridable by subclass
305     }
306 
307     /**
308      * Handle End of File.
309      *
310      * @return <code>-1</code> if <code>throwEofException</code> is
311      * set to {@code false}
312      * @throws EOFException if <code>throwEofException</code> is set
313      * to {@code true}.
314      */
315     private int doEndOfFile() throws EOFException {
316         eof = true;
317         if (throwEofException) {
318             throw new EOFException();
319         }
320         return EOF;
321     }
322 
323 }