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 * @version $Id: AutoCloseInputStream.java 1586350 2014-04-10 15:57:20Z ggregory $ 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 * Closes the underlying input stream and replaces the reference to it 052 * with a {@link ClosedInputStream} instance. 053 * <p> 054 * This method is automatically called by the read methods when the end 055 * of input has been reached. 056 * <p> 057 * Note that it is safe to call this method any number of times. The original 058 * underlying input stream is closed and discarded only once when this 059 * method is first called. 060 * 061 * @throws IOException if the underlying input stream can not be closed 062 */ 063 @Override 064 public void close() throws IOException { 065 in.close(); 066 in = new ClosedInputStream(); 067 } 068 069 /** 070 * Automatically closes the stream if the end of stream was reached. 071 * 072 * @param n number of bytes read, or -1 if no more bytes are available 073 * @throws IOException if the stream could not be closed 074 * @since 2.0 075 */ 076 @Override 077 protected void afterRead(final int n) throws IOException { 078 if (n == EOF) { 079 close(); 080 } 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}