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 */
017
018package org.apache.commons.io.input;
019
020import java.io.IOException;
021import java.io.InputStream;
022
023/**
024 * Abstracts some InputStream operations for implementations in this package.
025 *
026 * @since 2.17.0
027 */
028public abstract class AbstractInputStream extends InputStream {
029
030    /**
031     * Whether {@link #close()} completed successfully.
032     */
033    private boolean closed;
034
035    /**
036     * Constructs a new instance for subclasses.
037     */
038    public AbstractInputStream() {
039        // empty
040    }
041
042    /**
043     * Checks if this instance is closed and throws an IOException if so.
044     *
045     * @throws IOException if this instance is closed.
046     */
047    void checkOpen() throws IOException {
048        Input.checkOpen(!isClosed());
049    }
050
051    @Override
052    public void close() throws IOException {
053        super.close();
054        closed = true;
055    }
056
057    /**
058     * Tests whether this instance is closed; if {@link #close()} completed successfully.
059     *
060     * @return whether this instance is closed.
061     */
062    public boolean isClosed() {
063        return closed;
064    }
065
066    /**
067     * Sets whether this instance is closed.
068     *
069     * @param closed whether this instance is closed.
070     */
071    public void setClosed(final boolean closed) {
072        this.closed = closed;
073    }
074
075}