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.InputStream;
021import java.util.function.Supplier;
022
023/**
024 * Always throws an {@link IOException} from all the {@link InputStream} methods where the exception is declared.
025 * <p>
026 * This class is mostly useful for testing error handling.
027 * </p>
028 *
029 * @since 2.0
030 */
031public class BrokenInputStream extends InputStream {
032
033    /**
034     * The singleton instance.
035     *
036     * @since 2.12.0
037     */
038    public static final BrokenInputStream INSTANCE = new BrokenInputStream();
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 stream that always throws an {@link IOException}.
047     */
048    public BrokenInputStream() {
049        this(() -> new IOException("Broken input stream"));
050    }
051
052    /**
053     * Constructs a new stream that always throws the given exception.
054     *
055     * @param exception the exception to be thrown.
056     */
057    public BrokenInputStream(final IOException exception) {
058        this(() -> exception);
059    }
060
061    /**
062     * Constructs a new stream 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 BrokenInputStream(final Supplier<IOException> exceptionSupplier) {
068        this.exceptionSupplier = exceptionSupplier;
069    }
070
071    /**
072     * Throws the configured exception.
073     *
074     * @return nothing
075     * @throws IOException always thrown
076     */
077    @Override
078    public int available() throws IOException {
079        throw exceptionSupplier.get();
080    }
081
082    /**
083     * Throws the configured exception.
084     *
085     * @throws IOException always thrown
086     */
087    @Override
088    public void close() throws IOException {
089        throw exceptionSupplier.get();
090    }
091
092    /**
093     * Throws the configured exception.
094     *
095     * @return nothing
096     * @throws IOException always thrown
097     */
098    @Override
099    public int read() throws IOException {
100        throw exceptionSupplier.get();
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 exceptionSupplier.get();
111    }
112
113    /**
114     * Throws the configured exception.
115     *
116     * @param n ignored
117     * @return nothing
118     * @throws IOException always thrown
119     */
120    @Override
121    public long skip(final long n) throws IOException {
122        throw exceptionSupplier.get();
123    }
124
125}