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
023import org.apache.commons.io.function.Erase;
024
025/**
026 * Always throws an exception from all {@link Reader} methods where {@link IOException} is declared.
027 * <p>
028 * This class is mostly useful for testing error handling.
029 * </p>
030 *
031 * @since 2.7
032 */
033public class BrokenReader extends Reader {
034
035    /**
036     * A singleton instance using a default IOException.
037     *
038     * @since 2.12.0
039     */
040    public static final BrokenReader INSTANCE = new BrokenReader();
041
042    /**
043     * A supplier for the exception that is thrown by all methods of this class.
044     */
045    private final Supplier<Throwable> exceptionSupplier;
046
047    /**
048     * Constructs a new reader that always throws an {@link IOException}.
049     */
050    public BrokenReader() {
051        this(() -> new IOException("Broken reader"));
052    }
053
054    /**
055     * Constructs a new reader that always throws the given exception.
056     *
057     * @param exception the exception to be thrown.
058     * @deprecated Use {@link #BrokenReader(Throwable)}.
059     */
060    @Deprecated
061    public BrokenReader(final IOException exception) {
062        this(() -> exception);
063    }
064
065    /**
066     * Constructs a new reader 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 BrokenReader(final Supplier<Throwable> exceptionSupplier) {
072        this.exceptionSupplier = exceptionSupplier;
073    }
074
075    /**
076     * Constructs a new reader that always throws the given exception.
077     *
078     * @param exception the exception to be thrown.
079     * @since 2.16.0
080     */
081    public BrokenReader(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     * @param readAheadLimit ignored.
099     * @throws IOException always throws the exception configured in a constructor.
100     */
101    @Override
102    public void mark(final int readAheadLimit) throws IOException {
103        throw rethrow();
104    }
105
106    /**
107     * Throws the configured exception.
108     *
109     * @param cbuf ignored.
110     * @param off  ignored.
111     * @param len  ignored.
112     * @return nothing.
113     * @throws IOException always throws the exception configured in a constructor.
114     */
115    @Override
116    public int read(final char[] cbuf, final int off, final int len) throws IOException {
117        throw rethrow();
118    }
119
120    /**
121     * Throws the configured exception.
122     *
123     * @return nothing.
124     * @throws IOException always throws the exception configured in a constructor.
125     */
126    @Override
127    public boolean ready() throws IOException {
128        throw rethrow();
129    }
130
131    /**
132     * Throws the configured exception.
133     *
134     * @throws IOException always throws the exception configured in a constructor.
135     */
136    @Override
137    public void reset() throws IOException {
138        throw rethrow();
139    }
140
141    /**
142     * Throws the configured exception from its supplier.
143     *
144     * @return Throws the configured exception from its supplier.
145     */
146    private RuntimeException rethrow() {
147        return Erase.rethrow(exceptionSupplier.get());
148    }
149
150    /**
151     * Throws the configured exception.
152     *
153     * @param n ignored.
154     * @return nothing.
155     * @throws IOException always throws the exception configured in a constructor.
156     */
157    @Override
158    public long skip(final long n) throws IOException {
159        throw rethrow();
160    }
161
162}