BrokenOutputStream.java

  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.output;

  18. import java.io.IOException;
  19. import java.io.OutputStream;
  20. import java.util.function.Function;
  21. import java.util.function.Supplier;

  22. import org.apache.commons.io.function.Erase;

  23. /**
  24.  * Always throws an exception from all {@link OutputStream} methods where {@link IOException} 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 BrokenOutputStream extends OutputStream {

  32.     /**
  33.      * The singleton instance using a default IOException.
  34.      *
  35.      * @since 2.12.0
  36.      */
  37.     public static final BrokenOutputStream INSTANCE = new BrokenOutputStream();

  38.     /**
  39.      * Supplies the exception that is thrown by all methods of this class.
  40.      */
  41.     private final Function<String, Throwable> exceptionFunction;

  42.     /**
  43.      * Constructs a new stream that always throws an {@link IOException}.
  44.      */
  45.     public BrokenOutputStream() {
  46.         this(m -> new IOException("Broken output stream: " + m));
  47.     }

  48.     /**
  49.      * Constructs a new stream that always throws the given exception.
  50.      *
  51.      * @param exception the exception to be thrown.
  52.      * @deprecated Use {@link #BrokenOutputStream(Throwable)}.
  53.      */
  54.     @Deprecated
  55.     public BrokenOutputStream(final IOException exception) {
  56.         this(m -> exception);
  57.     }

  58.     /**
  59.      * Constructs a new stream that always throws the supplied exception.
  60.      * <p>
  61.      * This class uses the invoked method name as the function input.
  62.      * </p>
  63.      *
  64.      * @param exceptionFunction a supplier for the IOException or RuntimeException to be thrown.
  65.      * @since 2.19.0
  66.      */
  67.     public BrokenOutputStream(final Function<String, Throwable> exceptionFunction) {
  68.         this.exceptionFunction = exceptionFunction;
  69.     }

  70.     /**
  71.      * Constructs a new stream that always throws the supplied exception.
  72.      *
  73.      * @param exceptionSupplier a supplier for the IOException or RuntimeException to be thrown.
  74.      * @since 2.12.0
  75.      * @deprecated Use {@link #BrokenOutputStream(Function)}.
  76.      */
  77.     @Deprecated
  78.     public BrokenOutputStream(final Supplier<Throwable> exceptionSupplier) {
  79.         this.exceptionFunction = m -> exceptionSupplier.get();
  80.     }

  81.     /**
  82.      * Constructs a new stream that always throws the given exception.
  83.      *
  84.      * @param exception the exception to be thrown.
  85.      * @since 2.16.0
  86.      */
  87.     public BrokenOutputStream(final Throwable exception) {
  88.         this(m -> exception);
  89.     }

  90.     /**
  91.      * Throws the configured exception.
  92.      *
  93.      * @throws IOException always throws the exception configured in a constructor.
  94.      */
  95.     @Override
  96.     public void close() throws IOException {
  97.         throw rethrow("close()");
  98.     }

  99.     /**
  100.      * Throws the configured exception.
  101.      *
  102.      * @throws IOException always throws the exception configured in a constructor.
  103.      */
  104.     @Override
  105.     public void flush() throws IOException {
  106.         throw rethrow("flush()");
  107.     }

  108.     /**
  109.      * Throws the configured exception from its supplier.
  110.      *
  111.      * @param method The exception function argument.
  112.      * @return Throws the configured exception from its supplier.
  113.      */
  114.     private RuntimeException rethrow(final String method) {
  115.         return Erase.rethrow(exceptionFunction.apply(method));
  116.     }

  117.     /**
  118.      * Throws the configured exception.
  119.      *
  120.      * @param b ignored.
  121.      * @throws IOException always throws the exception configured in a constructor.
  122.      */
  123.     @Override
  124.     public void write(final int b) throws IOException {
  125.         throw rethrow("write(int)");
  126.     }

  127. }