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;
021
022/**
023 * Broken input stream. This stream always throws an {@link IOException} from
024 * all the {@link InputStream} methods where the exception is declared.
025 * <p>
026 * This class is mostly useful for testing error handling in code that uses an
027 * input stream.
028 * </p>
029 *
030 * @since 2.0
031 */
032public class BrokenInputStream extends InputStream {
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 stream that always throws the given exception.
041     *
042     * @param exception the exception to be thrown
043     */
044    public BrokenInputStream(final IOException exception) {
045        this.exception = exception;
046    }
047
048    /**
049     * Creates a new stream that always throws an {@link IOException}
050     */
051    public BrokenInputStream() {
052        this(new IOException("Broken input stream"));
053    }
054
055    /**
056     * Throws the configured exception.
057     *
058     * @return nothing
059     * @throws IOException always thrown
060     */
061    @Override
062    public int read() throws IOException {
063        throw exception;
064    }
065
066    /**
067     * Throws the configured exception.
068     *
069     * @return nothing
070     * @throws IOException always thrown
071     */
072    @Override
073    public int available() throws IOException {
074        throw exception;
075    }
076
077    /**
078     * Throws the configured exception.
079     *
080     * @param n ignored
081     * @return nothing
082     * @throws IOException always thrown
083     */
084    @Override
085    public long skip(final long n) throws IOException {
086        throw exception;
087    }
088
089    /**
090     * Throws the configured exception.
091     *
092     * @throws IOException always thrown
093     */
094    @Override
095    public synchronized void reset() throws IOException {
096        throw exception;
097    }
098
099    /**
100     * Throws the configured exception.
101     *
102     * @throws IOException always thrown
103     */
104    @Override
105    public void close() throws IOException {
106        throw exception;
107    }
108
109}