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 static org.apache.commons.io.IOUtils.EOF;
020
021import java.io.IOException;
022import java.io.InputStream;
023
024/**
025 * Proxy stream that closes and discards the underlying stream as soon as the
026 * end of input has been reached or when the stream is explicitly closed.
027 * Not even a reference to the underlying stream is kept after it has been
028 * closed, so any allocated in-memory buffers can be freed even if the
029 * client application still keeps a reference to the proxy stream.
030 * <p>
031 * This class is typically used to release any resources related to an open
032 * stream as soon as possible even if the client application (by not explicitly
033 * closing the stream when no longer needed) or the underlying stream (by not
034 * releasing resources once the last byte has been read) do not do that.
035 * </p>
036 *
037 * @since 1.4
038 */
039public class AutoCloseInputStream extends ProxyInputStream {
040
041    /**
042     * Creates an automatically closing proxy for the given input stream.
043     *
044     * @param in underlying input stream
045     */
046    public AutoCloseInputStream(final InputStream in) {
047        super(in);
048    }
049
050    /**
051     * Automatically closes the stream if the end of stream was reached.
052     *
053     * @param n number of bytes read, or -1 if no more bytes are available
054     * @throws IOException if the stream could not be closed
055     * @since 2.0
056     */
057    @Override
058    protected void afterRead(final int n) throws IOException {
059        if (n == EOF) {
060            close();
061        }
062    }
063
064    /**
065     * Closes the underlying input stream and replaces the reference to it
066     * with a {@link ClosedInputStream} instance.
067     * <p>
068     * This method is automatically called by the read methods when the end
069     * of input has been reached.
070     * <p>
071     * Note that it is safe to call this method any number of times. The original
072     * underlying input stream is closed and discarded only once when this
073     * method is first called.
074     *
075     * @throws IOException if the underlying input stream can not be closed
076     */
077    @Override
078    public void close() throws IOException {
079        in.close();
080        in = ClosedInputStream.INSTANCE;
081    }
082
083    /**
084     * Ensures that the stream is closed before it gets garbage-collected.
085     * As mentioned in {@link #close()}, this is a no-op if the stream has
086     * already been closed.
087     * @throws Throwable if an error occurs
088     */
089    @Override
090    protected void finalize() throws Throwable {
091        close();
092        super.finalize();
093    }
094
095}