org.apache.commons.io.input
public class AutoCloseInputStream extends ProxyInputStream
This class is typically used to release any resources related to an open stream as soon as possible even if the client application (by not explicitly closing the stream when no longer needed) or the underlying stream (by not releasing resources once the last byte has been read) do not do that.
Constructor and Description |
---|
AutoCloseInputStream(java.io.InputStream in)
Creates an automatically closing proxy for the given input stream.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the underlying input stream and replaces the reference to it
with a
ClosedInputStream instance. |
protected void |
finalize()
Ensures that the stream is closed before it gets garbage-collected.
|
int |
read()
Reads and returns a single byte from the underlying input stream.
|
int |
read(byte[] b)
Reads and returns bytes from the underlying input stream to the given
buffer.
|
int |
read(byte[] b,
int off,
int len)
Reads and returns bytes from the underlying input stream to the given
buffer.
|
available, mark, markSupported, reset, skip
public AutoCloseInputStream(java.io.InputStream in)
in
- underlying input streampublic void close() throws java.io.IOException
ClosedInputStream
instance.
This method is automatically called by the read methods when the end of input has been reached.
Note that it is safe to call this method any number of times. The original underlying input stream is closed and discarded only once when this method is first called.
close
in interface java.io.Closeable
close
in class ProxyInputStream
java.io.IOException
- if the underlying input stream can not be closedpublic int read() throws java.io.IOException
close()
method is
called to automatically close and discard the stream.read
in class ProxyInputStream
java.io.IOException
- if the stream could not be read or closedpublic int read(byte[] b) throws java.io.IOException
close()
method
i called to automatically close and discard the stream.read
in class ProxyInputStream
b
- buffer to which bytes from the stream are writtenjava.io.IOException
- if the stream could not be read or closedpublic int read(byte[] b, int off, int len) throws java.io.IOException
close()
method
i called to automatically close and discard the stream.read
in class ProxyInputStream
b
- buffer to which bytes from the stream are writtenoff
- start offset within the bufferlen
- maximum number of bytes to readjava.io.IOException
- if the stream could not be read or closedprotected void finalize() throws java.lang.Throwable
close()
, this is a no-op if the stream has
already been closed.finalize
in class java.lang.Object
java.lang.Throwable
- if an error occursCopyright (c) 2002-2015 Apache Software Foundation