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    *      https://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.output;
18  
19  import java.io.IOException;
20  import java.io.OutputStream;
21  import java.util.function.Function;
22  import java.util.function.Supplier;
23  
24  import org.apache.commons.io.function.Erase;
25  
26  /**
27   * Always throws an exception from all {@link OutputStream} methods where {@link IOException} is declared.
28   * <p>
29   * This class is mostly useful for testing error handling.
30   * </p>
31   *
32   * @since 2.0
33   */
34  public class BrokenOutputStream extends OutputStream {
35  
36      /**
37       * The singleton instance using a default IOException.
38       *
39       * @since 2.12.0
40       */
41      public static final BrokenOutputStream INSTANCE = new BrokenOutputStream();
42  
43      /**
44       * Supplies the exception that is thrown by all methods of this class.
45       */
46      private final Function<String, Throwable> exceptionFunction;
47  
48      /**
49       * Constructs a new stream that always throws an {@link IOException}.
50       */
51      public BrokenOutputStream() {
52          this(m -> new IOException("Broken output stream: " + m));
53      }
54  
55      /**
56       * Constructs a new stream that always throws the supplied exception.
57       * <p>
58       * This class uses the invoked method name as the function input.
59       * </p>
60       *
61       * @param exceptionFunction a supplier for the IOException or RuntimeException to be thrown.
62       * @since 2.19.0
63       */
64      public BrokenOutputStream(final Function<String, Throwable> exceptionFunction) {
65          this.exceptionFunction = exceptionFunction;
66      }
67  
68      /**
69       * Constructs a new stream that always throws the given exception.
70       *
71       * @param exception the exception to be thrown.
72       * @deprecated Use {@link #BrokenOutputStream(Throwable)}.
73       */
74      @Deprecated
75      public BrokenOutputStream(final IOException exception) {
76          this(m -> exception);
77      }
78  
79      /**
80       * Constructs a new stream that always throws the supplied exception.
81       *
82       * @param exceptionSupplier a supplier for the IOException or RuntimeException to be thrown.
83       * @since 2.12.0
84       * @deprecated Use {@link #BrokenOutputStream(Function)}.
85       */
86      @Deprecated
87      public BrokenOutputStream(final Supplier<Throwable> exceptionSupplier) {
88          this.exceptionFunction = m -> exceptionSupplier.get();
89      }
90  
91      /**
92       * Constructs a new stream that always throws the given exception.
93       *
94       * @param exception the exception to be thrown.
95       * @since 2.16.0
96       */
97      public BrokenOutputStream(final Throwable exception) {
98          this(m -> exception);
99      }
100 
101     /**
102      * Throws the configured exception.
103      *
104      * @throws IOException always throws the exception configured in a constructor.
105      */
106     @Override
107     public void close() throws IOException {
108         throw rethrow("close()");
109     }
110 
111     /**
112      * Throws the configured exception.
113      *
114      * @throws IOException always throws the exception configured in a constructor.
115      */
116     @Override
117     public void flush() throws IOException {
118         throw rethrow("flush()");
119     }
120 
121     /**
122      * Throws the configured exception from its supplier.
123      *
124      * @param method The exception function argument.
125      * @return Throws the configured exception from its supplier.
126      */
127     private RuntimeException rethrow(final String method) {
128         return Erase.rethrow(exceptionFunction.apply(method));
129     }
130 
131     /**
132      * Throws the configured exception.
133      *
134      * @param b ignored.
135      * @throws IOException always throws the exception configured in a constructor.
136      */
137     @Override
138     public void write(final int b) throws IOException {
139         throw rethrow("write(int)");
140     }
141 
142 }