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 java.io.IOException;
20  import java.io.Reader;
21  
22  /**
23   * Broken reader. This reader always throws an {@link IOException} from
24   * all the {@link Reader} methods where the exception is declared.
25   * <p>
26   * This class is mostly useful for testing error handling in code that uses a
27   * reader.
28   * </p>
29   *
30   * @since 2.7
31   */
32  public class BrokenReader extends Reader {
33  
34      /**
35       * The exception that is thrown by all methods of this class.
36       */
37      private final IOException exception;
38  
39      /**
40       * Creates a new reader that always throws the given exception.
41       *
42       * @param exception the exception to be thrown
43       */
44      public BrokenReader(final IOException exception) {
45          this.exception = exception;
46      }
47  
48      /**
49       * Creates a new reader that always throws an {@link IOException}
50       */
51      public BrokenReader() {
52          this(new IOException("Broken reader"));
53      }
54  
55      /**
56       * Throws the configured exception.
57       *
58       * @param cbuf ignored
59       * @param off ignored
60       * @param len ignored
61       * @return nothing
62       * @throws IOException always thrown
63       */
64      @Override
65      public int read(final char[] cbuf, final int off, final int len) throws IOException {
66          throw exception;
67      }
68  
69      /**
70       * Throws the configured exception.
71       *
72       * @param n ignored
73       * @return nothing
74       * @throws IOException always thrown
75       */
76      @Override
77      public long skip(final long n) throws IOException {
78          throw exception;
79      }
80  
81      /**
82       * Throws the configured exception.
83       *
84       * @return nothing
85       * @throws IOException always thrown
86       */
87      @Override
88      public boolean ready() throws IOException {
89          throw exception;
90      }
91  
92      /**
93       * Throws the configured exception.
94       *
95       * @param readAheadLimit ignored
96       * @throws IOException always thrown
97       */
98      @Override
99      public void mark(final int readAheadLimit) throws IOException {
100         throw exception;
101     }
102 
103     /**
104      * Throws the configured exception.
105      *
106      * @throws IOException always thrown
107      */
108     @Override
109     public synchronized void reset() throws IOException {
110         throw exception;
111     }
112 
113     /**
114      * Throws the configured exception.
115      *
116      * @throws IOException always thrown
117      */
118     @Override
119     public void close() throws IOException {
120         throw exception;
121     }
122 
123 }