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.input;
018
019import java.io.IOException;
020import java.io.Reader;
021import java.util.function.Supplier;
022
023/**
024 * Always throws an {@link IOException} from all the {@link Reader} methods where the exception is declared.
025 * <p>
026 * This class is mostly useful for testing error handling.
027 * </p>
028 *
029 * @since 2.7
030 */
031public class BrokenReader extends Reader {
032
033    /**
034     * A singleton instance using a default IOException.
035     *
036     * @since 2.12.0
037     */
038    public static final BrokenReader INSTANCE = new BrokenReader();
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 reader that always throws an {@link IOException}.
047     */
048    public BrokenReader() {
049        this(() -> new IOException("Broken reader"));
050    }
051
052    /**
053     * Constructs a new reader that always throws the given exception.
054     *
055     * @param exception the exception to be thrown.
056     */
057    public BrokenReader(final IOException exception) {
058        this(() -> exception);
059    }
060
061    /**
062     * Constructs a new reader 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 BrokenReader(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     * @param readAheadLimit ignored
085     * @throws IOException always thrown
086     */
087    @Override
088    public void mark(final int readAheadLimit) throws IOException {
089        throw exceptionSupplier.get();
090    }
091
092    /**
093     * Throws the configured exception.
094     *
095     * @param cbuf ignored
096     * @param off  ignored
097     * @param len  ignored
098     * @return nothing
099     * @throws IOException always thrown
100     */
101    @Override
102    public int read(final char[] cbuf, final int off, final int len) throws IOException {
103        throw exceptionSupplier.get();
104    }
105
106    /**
107     * Throws the configured exception.
108     *
109     * @return nothing
110     * @throws IOException always thrown
111     */
112    @Override
113    public boolean ready() throws IOException {
114        throw exceptionSupplier.get();
115    }
116
117    /**
118     * Throws the configured exception.
119     *
120     * @throws IOException always thrown
121     */
122    @Override
123    public synchronized void reset() throws IOException {
124        throw exceptionSupplier.get();
125    }
126
127    /**
128     * Throws the configured exception.
129     *
130     * @param n ignored
131     * @return nothing
132     * @throws IOException always thrown
133     */
134    @Override
135    public long skip(final long n) throws IOException {
136        throw exceptionSupplier.get();
137    }
138
139}