BrokenReader.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.Reader;
  20. import java.util.function.Supplier;

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

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

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

  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 reader that always throws an {@link IOException}.
  43.      */
  44.     public BrokenReader() {
  45.         this(() -> new IOException("Broken reader"));
  46.     }

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

  57.     /**
  58.      * Constructs a new reader 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 BrokenReader(final Supplier<Throwable> exceptionSupplier) {
  64.         this.exceptionSupplier = exceptionSupplier;
  65.     }

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

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

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

  94.     /**
  95.      * Throws the configured exception.
  96.      *
  97.      * @param cbuf ignored.
  98.      * @param off  ignored.
  99.      * @param len  ignored.
  100.      * @return nothing.
  101.      * @throws IOException always throws the exception configured in a constructor.
  102.      */
  103.     @Override
  104.     public int read(final char[] cbuf, final int off, final int len) throws IOException {
  105.         throw rethrow();
  106.     }

  107.     /**
  108.      * Throws the configured exception.
  109.      *
  110.      * @return nothing.
  111.      * @throws IOException always throws the exception configured in a constructor.
  112.      */
  113.     @Override
  114.     public boolean ready() throws IOException {
  115.         throw rethrow();
  116.     }

  117.     /**
  118.      * Throws the configured exception.
  119.      *
  120.      * @throws IOException always throws the exception configured in a constructor.
  121.      */
  122.     @Override
  123.     public void reset() throws IOException {
  124.         throw rethrow();
  125.     }

  126.     /**
  127.      * Throws the configured exception from its supplier.
  128.      *
  129.      * @return Throws the configured exception from its supplier.
  130.      */
  131.     private RuntimeException rethrow() {
  132.         return Erase.rethrow(exceptionSupplier.get());
  133.     }

  134.     /**
  135.      * Throws the configured exception.
  136.      *
  137.      * @param n ignored.
  138.      * @return nothing.
  139.      * @throws IOException always throws the exception configured in a constructor.
  140.      */
  141.     @Override
  142.     public long skip(final long n) throws IOException {
  143.         throw rethrow();
  144.     }

  145. }