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.InputStream;
21  
22  /**
23   * Broken input stream. This stream always throws an {@link IOException} from
24   * all the {@link InputStream} methods where the exception is declared.
25   * <p>
26   * This class is mostly useful for testing error handling in code that uses an
27   * input stream.
28   * </p>
29   *
30   * @since 2.0
31   */
32  public class BrokenInputStream extends InputStream {
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 stream that always throws the given exception.
41       *
42       * @param exception the exception to be thrown
43       */
44      public BrokenInputStream(final IOException exception) {
45          this.exception = exception;
46      }
47  
48      /**
49       * Creates a new stream that always throws an {@link IOException}
50       */
51      public BrokenInputStream() {
52          this(new IOException("Broken input stream"));
53      }
54  
55      /**
56       * Throws the configured exception.
57       *
58       * @return nothing
59       * @throws IOException always thrown
60       */
61      @Override
62      public int read() throws IOException {
63          throw exception;
64      }
65  
66      /**
67       * Throws the configured exception.
68       *
69       * @return nothing
70       * @throws IOException always thrown
71       */
72      @Override
73      public int available() throws IOException {
74          throw exception;
75      }
76  
77      /**
78       * Throws the configured exception.
79       *
80       * @param n ignored
81       * @return nothing
82       * @throws IOException always thrown
83       */
84      @Override
85      public long skip(final long n) throws IOException {
86          throw exception;
87      }
88  
89      /**
90       * Throws the configured exception.
91       *
92       * @throws IOException always thrown
93       */
94      @Override
95      public synchronized void reset() throws IOException {
96          throw exception;
97      }
98  
99      /**
100      * Throws the configured exception.
101      *
102      * @throws IOException always thrown
103      */
104     @Override
105     public void close() throws IOException {
106         throw exception;
107     }
108 
109 }