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

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

  22. /**
  23.  * Always throws an exception from all {@link Writer} 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 BrokenWriter extends Writer {

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

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

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

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

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

  66.     /**
  67.      * Constructs a new writer that always throws the given exception.
  68.      *
  69.      * @param exception the exception to be thrown.
  70.      * @since 2.16.0
  71.      */
  72.     public BrokenWriter(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.      * @throws IOException always throws the exception configured in a constructor.
  88.      */
  89.     @Override
  90.     public void flush() throws IOException {
  91.         throw rethrow();
  92.     }

  93.     /**
  94.      * Throws the configured exception from its supplier.
  95.      *
  96.      * @return Throws the configured exception from its supplier.
  97.      */
  98.     private RuntimeException rethrow() {
  99.         return Erase.rethrow(exceptionSupplier.get());
  100.     }

  101.     /**
  102.      * Throws the configured exception.
  103.      *
  104.      * @param cbuf ignored.
  105.      * @param off  ignored.
  106.      * @param len  ignored.
  107.      * @throws IOException always throws the exception configured in a constructor.
  108.      */
  109.     @Override
  110.     public void write(final char[] cbuf, final int off, final int len) throws IOException {
  111.         throw rethrow();
  112.     }

  113. }