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;
021
022/**
023 * Broken reader. This reader always throws an {@link IOException} from
024 * all the {@link Reader} methods where the exception is declared.
025 * <p>
026 * This class is mostly useful for testing error handling in code that uses a
027 * reader.
028 * </p>
029 *
030 * @since 2.7
031 */
032public class BrokenReader extends Reader {
033
034    /**
035     * The exception that is thrown by all methods of this class.
036     */
037    private final IOException exception;
038
039    /**
040     * Creates a new reader that always throws the given exception.
041     *
042     * @param exception the exception to be thrown
043     */
044    public BrokenReader(final IOException exception) {
045        this.exception = exception;
046    }
047
048    /**
049     * Creates a new reader that always throws an {@link IOException}
050     */
051    public BrokenReader() {
052        this(new IOException("Broken reader"));
053    }
054
055    /**
056     * Throws the configured exception.
057     *
058     * @param cbuf ignored
059     * @param off ignored
060     * @param len ignored
061     * @return nothing
062     * @throws IOException always thrown
063     */
064    @Override
065    public int read(final char[] cbuf, final int off, final int len) throws IOException {
066        throw exception;
067    }
068
069    /**
070     * Throws the configured exception.
071     *
072     * @param n ignored
073     * @return nothing
074     * @throws IOException always thrown
075     */
076    @Override
077    public long skip(final long n) throws IOException {
078        throw exception;
079    }
080
081    /**
082     * Throws the configured exception.
083     *
084     * @return nothing
085     * @throws IOException always thrown
086     */
087    @Override
088    public boolean ready() throws IOException {
089        throw exception;
090    }
091
092    /**
093     * Throws the configured exception.
094     *
095     * @param readAheadLimit ignored
096     * @throws IOException always thrown
097     */
098    @Override
099    public void mark(final int readAheadLimit) throws IOException {
100        throw exception;
101    }
102
103    /**
104     * Throws the configured exception.
105     *
106     * @throws IOException always thrown
107     */
108    @Override
109    public synchronized void reset() throws IOException {
110        throw exception;
111    }
112
113    /**
114     * Throws the configured exception.
115     *
116     * @throws IOException always thrown
117     */
118    @Override
119    public void close() throws IOException {
120        throw exception;
121    }
122
123}