| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| AutoCloseInputStream |
|
| 1.25;1.25 |
| 1 | /* | |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
| 3 | * contributor license agreements. See the NOTICE file distributed with | |
| 4 | * this work for additional information regarding copyright ownership. | |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
| 6 | * (the "License"); you may not use this file except in compliance with | |
| 7 | * the License. You may obtain a copy of the License at | |
| 8 | * | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 10 | * | |
| 11 | * Unless required by applicable law or agreed to in writing, software | |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 14 | * See the License for the specific language governing permissions and | |
| 15 | * limitations under the License. | |
| 16 | */ | |
| 17 | package org.apache.commons.io.input; | |
| 18 | ||
| 19 | import java.io.IOException; | |
| 20 | import java.io.InputStream; | |
| 21 | ||
| 22 | /** | |
| 23 | * Proxy stream that closes and discards the underlying stream as soon as the | |
| 24 | * end of input has been reached or when the stream is explicitly closed. | |
| 25 | * Not even a reference to the underlying stream is kept after it has been | |
| 26 | * closed, so any allocated in-memory buffers can be freed even if the | |
| 27 | * client application still keeps a reference to the proxy stream. | |
| 28 | * <p> | |
| 29 | * This class is typically used to release any resources related to an open | |
| 30 | * stream as soon as possible even if the client application (by not explicitly | |
| 31 | * closing the stream when no longer needed) or the underlying stream (by not | |
| 32 | * releasing resources once the last byte has been read) do not do that. | |
| 33 | * | |
| 34 | * @version $Id: AutoCloseInputStream.java 1415850 2012-11-30 20:51:39Z ggregory $ | |
| 35 | * @since 1.4 | |
| 36 | */ | |
| 37 | public class AutoCloseInputStream extends ProxyInputStream { | |
| 38 | ||
| 39 | /** | |
| 40 | * Creates an automatically closing proxy for the given input stream. | |
| 41 | * | |
| 42 | * @param in underlying input stream | |
| 43 | */ | |
| 44 | public AutoCloseInputStream(final InputStream in) { | |
| 45 | 4 | super(in); |
| 46 | 4 | } |
| 47 | ||
| 48 | /** | |
| 49 | * Closes the underlying input stream and replaces the reference to it | |
| 50 | * with a {@link ClosedInputStream} instance. | |
| 51 | * <p> | |
| 52 | * This method is automatically called by the read methods when the end | |
| 53 | * of input has been reached. | |
| 54 | * <p> | |
| 55 | * Note that it is safe to call this method any number of times. The original | |
| 56 | * underlying input stream is closed and discarded only once when this | |
| 57 | * method is first called. | |
| 58 | * | |
| 59 | * @throws IOException if the underlying input stream can not be closed | |
| 60 | */ | |
| 61 | @Override | |
| 62 | public void close() throws IOException { | |
| 63 | 7 | in.close(); |
| 64 | 7 | in = new ClosedInputStream(); |
| 65 | 7 | } |
| 66 | ||
| 67 | /** | |
| 68 | * Automatically closes the stream if the end of stream was reached. | |
| 69 | * | |
| 70 | * @param n number of bytes read, or -1 if no more bytes are available | |
| 71 | * @throws IOException if the stream could not be closed | |
| 72 | * @since 2.0 | |
| 73 | */ | |
| 74 | @Override | |
| 75 | protected void afterRead(final int n) throws IOException { | |
| 76 | 11 | if (n == -1) { |
| 77 | 6 | close(); |
| 78 | } | |
| 79 | 11 | } |
| 80 | ||
| 81 | /** | |
| 82 | * Ensures that the stream is closed before it gets garbage-collected. | |
| 83 | * As mentioned in {@link #close()}, this is a no-op if the stream has | |
| 84 | * already been closed. | |
| 85 | * @throws Throwable if an error occurs | |
| 86 | */ | |
| 87 | @Override | |
| 88 | protected void finalize() throws Throwable { | |
| 89 | 0 | close(); |
| 90 | 0 | super.finalize(); |
| 91 | 0 | } |
| 92 | ||
| 93 | } |