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 * 036 * @since 1.4 037 */ 038public class AutoCloseInputStream extends ProxyInputStream { 039 040 /** 041 * Creates an automatically closing proxy for the given input stream. 042 * 043 * @param in underlying input stream 044 */ 045 public AutoCloseInputStream(final InputStream in) { 046 super(in); 047 } 048 049 /** 050 * Closes the underlying input stream and replaces the reference to it 051 * with a {@link ClosedInputStream} instance. 052 * <p> 053 * This method is automatically called by the read methods when the end 054 * of input has been reached. 055 * <p> 056 * Note that it is safe to call this method any number of times. The original 057 * underlying input stream is closed and discarded only once when this 058 * method is first called. 059 * 060 * @throws IOException if the underlying input stream can not be closed 061 */ 062 @Override 063 public void close() throws IOException { 064 in.close(); 065 in = ClosedInputStream.CLOSED_INPUT_STREAM; 066 } 067 068 /** 069 * Automatically closes the stream if the end of stream was reached. 070 * 071 * @param n number of bytes read, or -1 if no more bytes are available 072 * @throws IOException if the stream could not be closed 073 * @since 2.0 074 */ 075 @Override 076 protected void afterRead(final int n) throws IOException { 077 if (n == EOF) { 078 close(); 079 } 080 } 081 082 /** 083 * Ensures that the stream is closed before it gets garbage-collected. 084 * As mentioned in {@link #close()}, this is a no-op if the stream has 085 * already been closed. 086 * @throws Throwable if an error occurs 087 */ 088 @Override 089 protected void finalize() throws Throwable { 090 close(); 091 super.finalize(); 092 } 093 094}