BrokenInputStream.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.input;

  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.util.function.Supplier;

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

  22. /**
  23.  * Always throws an exception from all {@link InputStream} methods where {@link IOException} is declared.
  24.  * <p>
  25.  * This class is mostly useful for testing error handling.
  26.  * </p>
  27.  *
  28.  * @since 2.0
  29.  */
  30. public class BrokenInputStream extends InputStream {

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

  37.     /**
  38.      * A supplier for the exception that is thrown by all methods of this class.
  39.      */
  40.     private final Supplier<Throwable> exceptionSupplier;

  41.     /**
  42.      * Constructs a new stream that always throws an {@link IOException}.
  43.      */
  44.     public BrokenInputStream() {
  45.         this(() -> new IOException("Broken input stream"));
  46.     }

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

  57.     /**
  58.      * Constructs a new stream that always throws the supplied exception.
  59.      *
  60.      * @param exceptionSupplier a supplier for the IOException or RuntimeException to be thrown.
  61.      * @since 2.12.0
  62.      */
  63.     public BrokenInputStream(final Supplier<Throwable> exceptionSupplier) {
  64.         this.exceptionSupplier = exceptionSupplier;
  65.     }

  66.     /**
  67.      * Constructs a new stream that always throws the given exception.
  68.      *
  69.      * @param exception the exception to be thrown.
  70.      * @since 2.16.0
  71.      */
  72.     public BrokenInputStream(final Throwable exception) {
  73.         this(() -> exception);
  74.     }

  75.     /**
  76.      * Throws the configured exception.
  77.      *
  78.      * @return nothing.
  79.      * @throws IOException always throws the exception configured in a constructor.
  80.      */
  81.     @Override
  82.     public int available() throws IOException {
  83.         throw rethrow();
  84.     }

  85.     /**
  86.      * Throws the configured exception.
  87.      *
  88.      * @throws IOException always throws the exception configured in a constructor.
  89.      */
  90.     @Override
  91.     public void close() throws IOException {
  92.         throw rethrow();
  93.     }

  94.     /**
  95.      * Gets the Throwable to throw. Package-private for testing.
  96.      *
  97.      * @return  the Throwable to throw.
  98.      */
  99.     Throwable getThrowable() {
  100.         return exceptionSupplier.get();
  101.     }

  102.     /**
  103.      * Throws the configured exception.
  104.      *
  105.      * @return nothing.
  106.      * @throws IOException always throws the exception configured in a constructor.
  107.      */
  108.     @Override
  109.     public int read() throws IOException {
  110.         throw rethrow();
  111.     }

  112.     /**
  113.      * Throws the configured exception.
  114.      *
  115.      * @throws IOException always throws the exception configured in a constructor.
  116.      */
  117.     @Override
  118.     public synchronized void reset() throws IOException {
  119.         throw rethrow();
  120.     }

  121.     /**
  122.      * Throws the configured exception from its supplier.
  123.      *
  124.      * @return Throws the configured exception from its supplier.
  125.      */
  126.     private RuntimeException rethrow() {
  127.         return Erase.rethrow(getThrowable());
  128.     }

  129.     /**
  130.      * Throws the configured exception.
  131.      *
  132.      * @param n ignored.
  133.      * @return nothing.
  134.      * @throws IOException always throws the exception configured in a constructor.
  135.      */
  136.     @Override
  137.     public long skip(final long n) throws IOException {
  138.         throw rethrow();
  139.     }

  140. }