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
023/**
024 * Always throws an {@link IOException} from all {@link Writer} methods.
025 * <p>
026 * This class is mostly useful for testing error handling in code that uses a writer.
027 * </p>
028 *
029 * @since 2.0
030 */
031public class BrokenWriter extends Writer {
032
033    /**
034     * The singleton instance.
035     *
036     * @since 2.12.0
037     */
038    public static final BrokenWriter INSTANCE = new BrokenWriter();
039
040    /**
041     * A supplier for the exception that is thrown by all methods of this class.
042     */
043    private final Supplier<IOException> exceptionSupplier;
044
045    /**
046     * Constructs a new writer that always throws an {@link IOException}.
047     */
048    public BrokenWriter() {
049        this(() -> new IOException("Broken writer"));
050    }
051
052    /**
053     * Constructs a new writer that always throws the given exception.
054     *
055     * @param exception the exception to be thrown.
056     */
057    public BrokenWriter(final IOException exception) {
058        this(() -> exception);
059    }
060
061    /**
062     * Constructs a new writer that always throws an {@link IOException}.
063     *
064     * @param exceptionSupplier a supplier for the exception to be thrown.
065     * @since 2.12.0
066     */
067    public BrokenWriter(final Supplier<IOException> exceptionSupplier) {
068        this.exceptionSupplier = exceptionSupplier;
069    }
070
071    /**
072     * Throws the configured exception.
073     *
074     * @throws IOException always thrown
075     */
076    @Override
077    public void close() throws IOException {
078        throw exceptionSupplier.get();
079    }
080
081    /**
082     * Throws the configured exception.
083     *
084     * @throws IOException always thrown
085     */
086    @Override
087    public void flush() throws IOException {
088        throw exceptionSupplier.get();
089    }
090
091    /**
092     * Throws the configured exception.
093     *
094     * @param cbuf ignored
095     * @param off ignored
096     * @param len ignored
097     * @throws IOException always thrown
098     */
099    @Override
100    public void write(final char[] cbuf, final int off, final int len) throws IOException {
101        throw exceptionSupplier.get();
102    }
103
104}