001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.io.output;
018
019import java.io.IOException;
020import java.io.Writer;
021import java.util.function.Supplier;
022
023import org.apache.commons.io.function.Erase;
024
025/**
026 * Always throws an exception from all {@link Writer} methods where {@link IOException} is declared.
027 * <p>
028 * This class is mostly useful for testing error handling.
029 * </p>
030 *
031 * @since 2.0
032 */
033public class BrokenWriter extends Writer {
034
035    /**
036     * The singleton instance using a default IOException.
037     *
038     * @since 2.12.0
039     */
040    public static final BrokenWriter INSTANCE = new BrokenWriter();
041
042    /**
043     * Supplies the exception that is thrown by all methods of this class.
044     */
045    private final Supplier<Throwable> exceptionSupplier;
046
047    /**
048     * Constructs a new writer that always throws an {@link IOException}.
049     */
050    public BrokenWriter() {
051        this(() -> new IOException("Broken writer"));
052    }
053
054    /**
055     * Constructs a new writer that always throws the given exception.
056     *
057     * @param exception the exception to be thrown.
058     * @deprecated Use {@link #BrokenWriter(Throwable)}.
059     */
060    @Deprecated
061    public BrokenWriter(final IOException exception) {
062        this(() -> exception);
063    }
064
065    /**
066     * Constructs a new writer that always throws the supplied exception.
067     *
068     * @param exceptionSupplier a supplier for the IOException or RuntimeException to be thrown.
069     * @since 2.12.0
070     */
071    public BrokenWriter(final Supplier<Throwable> exceptionSupplier) {
072        this.exceptionSupplier = exceptionSupplier;
073    }
074
075    /**
076     * Constructs a new writer that always throws the given exception.
077     *
078     * @param exception the exception to be thrown.
079     * @since 2.16.0
080     */
081    public BrokenWriter(final Throwable exception) {
082        this(() -> exception);
083    }
084
085    /**
086     * Throws the configured exception.
087     *
088     * @throws IOException always throws the exception configured in a constructor.
089     */
090    @Override
091    public void close() throws IOException {
092        throw rethrow();
093    }
094
095    /**
096     * Throws the configured exception.
097     *
098     * @throws IOException always throws the exception configured in a constructor.
099     */
100    @Override
101    public void flush() throws IOException {
102        throw rethrow();
103    }
104
105    /**
106     * Throws the configured exception from its supplier.
107     *
108     * @return Throws the configured exception from its supplier.
109     */
110    private RuntimeException rethrow() {
111        return Erase.rethrow(exceptionSupplier.get());
112    }
113
114    /**
115     * Throws the configured exception.
116     *
117     * @param cbuf ignored.
118     * @param off  ignored.
119     * @param len  ignored.
120     * @throws IOException always throws the exception configured in a constructor.
121     */
122    @Override
123    public void write(final char[] cbuf, final int off, final int len) throws IOException {
124        throw rethrow();
125    }
126
127}