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     * Checks if this instance is closed and throws an IOException if so.
037     *
038     * @throws IOException if this instance is closed.
039     */
040    void checkOpen() throws IOException {
041        Input.checkOpen(!isClosed());
042    }
043
044    @Override
045    public void close() throws IOException {
046        super.close();
047        closed = true;
048    }
049
050    /**
051     * Tests whether this instance is closed; if {@link #close()} completed successfully.
052     *
053     * @return whether this instance is closed.
054     */
055    public boolean isClosed() {
056        return closed;
057    }
058
059    /**
060     * Sets whether this instance is closed.
061     *
062     * @param closed whether this instance is closed.
063     */
064    public void setClosed(final boolean closed) {
065        this.closed = closed;
066    }
067
068}