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 *
029 * @since 2.0
030 */
031public class BrokenInputStream extends InputStream {
032
033    /**
034     * The exception that is thrown by all methods of this class.
035     */
036    private final IOException exception;
037
038    /**
039     * Creates a new stream that always throws the given exception.
040     *
041     * @param exception the exception to be thrown
042     */
043    public BrokenInputStream(final IOException exception) {
044        this.exception = exception;
045    }
046
047    /**
048     * Creates a new stream that always throws an {@link IOException}
049     */
050    public BrokenInputStream() {
051        this(new IOException("Broken input stream"));
052    }
053
054    /**
055     * Throws the configured exception.
056     *
057     * @return nothing
058     * @throws IOException always thrown
059     */
060    @Override
061    public int read() throws IOException {
062        throw exception;
063    }
064
065    /**
066     * Throws the configured exception.
067     *
068     * @return nothing
069     * @throws IOException always thrown
070     */
071    @Override
072    public int available() throws IOException {
073        throw exception;
074    }
075
076    /**
077     * Throws the configured exception.
078     *
079     * @param n ignored
080     * @return nothing
081     * @throws IOException always thrown
082     */
083    @Override
084    public long skip(final long n) throws IOException {
085        throw exception;
086    }
087
088    /**
089     * Throws the configured exception.
090     *
091     * @throws IOException always thrown
092     */
093    @Override
094    public synchronized void reset() throws IOException {
095        throw exception;
096    }
097
098    /**
099     * Throws the configured exception.
100     *
101     * @throws IOException always thrown
102     */
103    @Override
104    public void close() throws IOException {
105        throw exception;
106    }
107
108}