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 import java.util.function.Supplier;
22
23 /**
24 * Always throws an {@link IOException} from all the {@link InputStream} methods where the exception is declared.
25 * <p>
26 * This class is mostly useful for testing error handling.
27 * </p>
28 *
29 * @since 2.0
30 */
31 public class BrokenInputStream extends InputStream {
32
33 /**
34 * The singleton instance.
35 *
36 * @since 2.12.0
37 */
38 public static final BrokenInputStream INSTANCE = new BrokenInputStream();
39
40 /**
41 * A supplier for the exception that is thrown by all methods of this class.
42 */
43 private final Supplier<IOException> exceptionSupplier;
44
45 /**
46 * Constructs a new stream that always throws an {@link IOException}.
47 */
48 public BrokenInputStream() {
49 this(() -> new IOException("Broken input stream"));
50 }
51
52 /**
53 * Constructs a new stream that always throws the given exception.
54 *
55 * @param exception the exception to be thrown.
56 */
57 public BrokenInputStream(final IOException exception) {
58 this(() -> exception);
59 }
60
61 /**
62 * Constructs a new stream that always throws an {@link IOException}.
63 *
64 * @param exceptionSupplier a supplier for the exception to be thrown.
65 * @since 2.12.0
66 */
67 public BrokenInputStream(final Supplier<IOException> exceptionSupplier) {
68 this.exceptionSupplier = exceptionSupplier;
69 }
70
71 /**
72 * Throws the configured exception.
73 *
74 * @return nothing
75 * @throws IOException always thrown
76 */
77 @Override
78 public int available() throws IOException {
79 throw exceptionSupplier.get();
80 }
81
82 /**
83 * Throws the configured exception.
84 *
85 * @throws IOException always thrown
86 */
87 @Override
88 public void close() throws IOException {
89 throw exceptionSupplier.get();
90 }
91
92 /**
93 * Throws the configured exception.
94 *
95 * @return nothing
96 * @throws IOException always thrown
97 */
98 @Override
99 public int read() throws IOException {
100 throw exceptionSupplier.get();
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 exceptionSupplier.get();
111 }
112
113 /**
114 * Throws the configured exception.
115 *
116 * @param n ignored
117 * @return nothing
118 * @throws IOException always thrown
119 */
120 @Override
121 public long skip(final long n) throws IOException {
122 throw exceptionSupplier.get();
123 }
124
125 }