public class ResettableInputStream extends ReleasableInputStream
ResettableInputStream
allows the close operation to be disabled via
ReleasableInputStream.disableClose()
(to avoid accidentally being closed). This is necessary when such input
stream needs to be marked-and-reset multiple times but only as long as the stream has not been
closed.
The creator of this input stream should therefore always call ReleasableInputStream.release()
in a finally
block to truly release the underlying resources.
Releasable
构造器和说明 |
---|
ResettableInputStream(java.io.File file) |
ResettableInputStream(java.io.FileInputStream fis) |
ResettableInputStream(java.lang.String filePath) |
限定符和类型 | 方法和说明 |
---|---|
int |
available()
Returns an estimate of the number of bytes that can be read (or
skipped over) from this input stream without blocking by the next
caller of a method for this input stream.
|
java.io.File |
getFile()
Returns the underlying file, if known; or null if not;
|
void |
mark(int limit)
Marks the current position in this input stream.
|
boolean |
markSupported()
Tests if this input stream supports the
mark
and reset methods. |
static ResettableInputStream |
newResettableInputStream(java.io.File file)
Convenient factory method to construct a new resettable input stream for the given file,
converting any IOException into CosClientException.
|
static ResettableInputStream |
newResettableInputStream(java.io.FileInputStream fis)
Convenient factory method to construct a new resettable input stream for the given file input
stream, converting any IOException into CosClientException.
|
static ResettableInputStream |
newResettableInputStream(java.io.FileInputStream fis,
java.lang.String errmsg)
Convenient factory method to construct a new resettable input stream for the given file input
stream, converting any IOException into CosClientException with the given error message.
|
static ResettableInputStream |
newResettableInputStream(java.io.File file,
java.lang.String errmsg)
Convenient factory method to construct a new resettable input stream for the given file,
converting any IOException into CosClientException with the given error message.
|
int |
read()
Reads the next byte of data from this input stream.
|
int |
read(byte[] arg0,
int arg1,
int arg2)
Reads up to
len bytes of data from this input stream
into an array of bytes. |
void |
reset()
Repositions this stream to the position at the time the
mark method was last
called on this input stream. |
long |
skip(long n)
Skips over and discards
n bytes of data from the
input stream. |
close, disableClose, isCloseDisabled, release, wrap
abort, abortIfNeeded, isAborted
public ResettableInputStream(java.io.File file) throws java.io.IOException
file
- must not be null. Upon successful construction the the file will be opened with
an input stream automatically marked at the starting position of the given file.
Note the creation of a ResettableInputStream
would entail physically opening a
file. If the opened file is meant to be closed only (in a finally block) by the very
same code block that created it, then it is necessary that the release method must not
be called while the execution is made in other stack frames.
In such case, as other stack frames may inadvertently or indirectly call the close
method of the stream, the creator of the stream would need to explicitly disable the
accidental closing via ReleasableInputStream.disableClose()
, so that the
release method becomes the only way to truly close the opened file.
java.io.IOException
public ResettableInputStream(java.lang.String filePath) throws java.io.IOException
java.io.IOException
public ResettableInputStream(java.io.FileInputStream fis) throws java.io.IOException
fis
- file input stream; must not be null. Upon successful construction the input stream
will be automatically marked at the current position of the given file input stream.
Note the creation of a ResettableInputStream
would entail physically opening a
file. If the opened file is meant to be closed only (in a finally block) by the very
same code block that created it, then it is necessary that the release method must not
be called while the execution is made in other stack frames.
In such case, as other stack frames may inadvertently or indirectly call the close
method of the stream, the creator of the stream would need to explicitly disable the
accidental closing via ReleasableInputStream.disableClose()
, so that the
release method becomes the only way to truly close the opened file.
java.io.IOException
public final boolean markSupported()
java.io.FilterInputStream
mark
and reset
methods.
This method
simply performs in.markSupported()
.markSupported
在类中 SdkFilterInputStream
true
if this stream type supports the
mark
and reset
method;
false
otherwise.FilterInputStream.in
,
InputStream.mark(int)
,
InputStream.reset()
public void mark(int limit)
reset
method repositions this stream at the last marked position so that subsequent reads re-read
the same bytes. This method works as long as the underlying file has not been closed.
Note the creation of a ResettableInputStream
would entail physically opening a file.
If the opened file is meant to be closed only (in a finally block) by the very same code
block that created it, then it is necessary that the release method must not be called while
the execution is made in other stack frames.
In such case, as other stack frames may inadvertently or indirectly call the close method of
the stream, the creator of the stream would need to explicitly disable the accidental closing
via ReleasableInputStream.disableClose()
, so that the release method becomes the only
way to truly close the opened file.
mark
在类中 SdkFilterInputStream
limit
- limitFilterInputStream.in
,
FilterInputStream.reset()
public void reset() throws java.io.IOException
mark
method was last
called on this input stream. This method works as long as the underlying file has not been
closed.
Note the creation of a ResettableInputStream
would entail physically opening a file.
If the opened file is meant to be closed only (in a finally block) by the very same code
block that created it, then it is necessary that the release method must not be called while
the execution is made in other stack frames.
In such case, as other stack frames may inadvertently or indirectly call the close method of
the stream, the creator of the stream would need to explicitly disable the accidental closing
via ReleasableInputStream.disableClose()
, so that the release method becomes the only
way to truly close the opened file.
reset
在类中 SdkFilterInputStream
java.io.IOException
- if the stream has not been marked or if the
mark has been invalidated.FilterInputStream.in
,
FilterInputStream.mark(int)
public int available() throws java.io.IOException
java.io.FilterInputStream
This method returns the result of in
.available().
available
在类中 SdkFilterInputStream
java.io.IOException
- if an I/O error occurs.public int read() throws java.io.IOException
java.io.FilterInputStream
int
in the range
0
to 255
. If no byte is available
because the end of the stream has been reached, the value
-1
is returned. This method blocks until input data
is available, the end of the stream is detected, or an exception
is thrown.
This method
simply performs in.read()
and returns the result.
read
在类中 SdkFilterInputStream
-1
if the end of the
stream is reached.java.io.IOException
- if an I/O error occurs.FilterInputStream.in
public long skip(long n) throws java.io.IOException
java.io.FilterInputStream
n
bytes of data from the
input stream. The skip
method may, for a variety of
reasons, end up skipping over some smaller number of bytes,
possibly 0
. The actual number of bytes skipped is
returned.
This method simply performs in.skip(n)
.
skip
在类中 SdkFilterInputStream
n
- the number of bytes to be skipped.java.io.IOException
- if the stream does not support seek,
or if some other I/O error occurs.public int read(byte[] arg0, int arg1, int arg2) throws java.io.IOException
java.io.FilterInputStream
len
bytes of data from this input stream
into an array of bytes. If len
is not zero, the method
blocks until some input is available; otherwise, no
bytes are read and 0
is returned.
This method simply performs in.read(b, off, len)
and returns the result.
read
在类中 SdkFilterInputStream
arg0
- the buffer into which the data is read.arg1
- the start offset in the destination array b
arg2
- the maximum number of bytes read.-1
if there is no more data because the end of
the stream has been reached.java.io.IOException
- if an I/O error occurs.FilterInputStream.in
public java.io.File getFile()
public static ResettableInputStream newResettableInputStream(java.io.File file)
Note the creation of a ResettableInputStream
would entail physically opening a file.
If the opened file is meant to be closed only (in a finally block) by the very same code
block that created it, then it is necessary that the release method must not be called while
the execution is made in other stack frames.
In such case, as other stack frames may inadvertently or indirectly call the close method of
the stream, the creator of the stream would need to explicitly disable the accidental closing
via ReleasableInputStream.disableClose()
, so that the release method becomes the only
way to truly close the opened file.
public static ResettableInputStream newResettableInputStream(java.io.File file, java.lang.String errmsg)
Note the creation of a ResettableInputStream
would entail physically opening a file.
If the opened file is meant to be closed only (in a finally block) by the very same code
block that created it, then it is necessary that the release method must not be called while
the execution is made in other stack frames.
In such case, as other stack frames may inadvertently or indirectly call the close method of
the stream, the creator of the stream would need to explicitly disable the accidental closing
via ReleasableInputStream.disableClose()
, so that the release method becomes the only
way to truly close the opened file.
public static ResettableInputStream newResettableInputStream(java.io.FileInputStream fis)
Note the creation of a ResettableInputStream
would entail physically opening a file.
If the opened file is meant to be closed only (in a finally block) by the very same code
block that created it, then it is necessary that the release method must not be called while
the execution is made in other stack frames.
In such case, as other stack frames may inadvertently or indirectly call the close method of
the stream, the creator of the stream would need to explicitly disable the accidental closing
via ReleasableInputStream.disableClose()
, so that the release method becomes the only
way to truly close the opened file.
public static ResettableInputStream newResettableInputStream(java.io.FileInputStream fis, java.lang.String errmsg)
Note the creation of a ResettableInputStream
would entail physically opening a file.
If the opened file is meant to be closed only (in a finally block) by the very same code
block that created it, then it is necessary that the release method must not be called while
the execution is made in other stack frames.
In such case, as other stack frames may inadvertently or indirectly call the close method of
the stream, the creator of the stream would need to explicitly disable the accidental closing
via ReleasableInputStream.disableClose()
, so that the release method becomes the only
way to truly close the opened file.