public class SubStream extends InputStream
Constructor and Description |
---|
SubStream(InputStream source,
long startIndex,
long streamLength,
Object lock)
Creates a new substream instance that partitions the wrapped stream
source from
startIndex up to streamLength . |
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the substream.
|
InputStream |
getInputStream() |
long |
getLength() |
void |
mark(int readlimit)
Marks the current position in the substream.
|
boolean |
markSupported()
The substream wrapper class is only compatible with markable input streams and hence
will always return true.
|
int |
read()
Reads the next byte of data from the wrapped stream.
|
int |
read(byte[] b)
Reads some number of bytes from the wrapped stream and stores them into
the buffer array
b . |
int |
read(byte[] b,
int off,
int len)
Reads up to
len bytes of data from the substream. |
void |
reset()
Repositions the substream position to the index where the
mark method
was last called. |
long |
skip(long n)
Advances the current position of the substream by
n . |
available
public SubStream(InputStream source, long startIndex, long streamLength, Object lock)
source
from
startIndex
up to streamLength
. Each substream instance that wraps the same
underlying InputStream
must share the same mutual exclusion lock
to avoid race
conditions from concurrent operations.source
- The markable InputStream to be wrapped.startIndex
- A valid index in the wrapped stream where the substream should logically begin.streamLength
- The length of the substream.lock
- An intrinsic lock to ensure thread-safe, concurrent operations
on substream instances wrapping the same InputStream.public InputStream getInputStream()
public long getLength()
public int read() throws IOException
int
in the range 0
to
255
. If no byte is available because the end of the substream
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.read
in class InputStream
-1
if the end of the
substream is reached.IOException
- if an I/O error occurs.public int read(byte[] b) throws IOException
b
. The number of bytes actually read is
returned as an integer. This method blocks until input data is
available, end of file is detected, or an exception is thrown.
If the length of b
is zero, then no bytes are read and
0
is returned; otherwise, there is an attempt to read at
least one byte. If no byte is available because the substream is at the
end of the file, the value -1
is returned; otherwise, at
least one byte is read and stored into b
.
The first byte read is stored into element b[0]
, the
next one into b[1]
, and so on. The number of bytes read is,
at most, equal to the length of b
. Let k be the
number of bytes actually read; these bytes will be stored in elements
b[0]
through b[
k-1]
,
leaving elements b[
k]
through
b[b.length-1]
unaffected.
The read(b)
method for class SubStream
has the same effect as:
read(b, 0, b.length)
read
in class InputStream
b
- the buffer into which the data is read.-1
if there is no more data because the end of
the stream has been reached.IOException
- If the first byte cannot be read for any reason
other than the end of the file, if the wrapped stream has been closed, or
if some other I/O error occurs.NullPointerException
- if b
is null
.read(byte[], int, int)
public int read(byte[] b, int off, int len) throws IOException
len
bytes of data from the substream. Buffers data from the wrapped stream
in order to minimize skip and read overhead. The wrappedstream will only be invoked if the readBuffer
cannot fulfil the the read request.
In order to ensure valid results, the wrapped stream must be marked prior to reading from the substream.
This allows us to reset to the relative substream position in the wrapped stream.
The number of bytes actually read is returned as an integer. All these operations are done
synchronously within an intrinsic lock to ensure other concurrent requests by substream instances
do not result in race conditions.
The underlying call to the read of the wrapped stream will blocks until input data is available, end of file is detected, or an exception is thrown.
If len
is zero, then no bytes are read and
0
is returned; otherwise, there is an attempt to read at
least one byte. If no byte is available because the substream is at end of
file, the value -1
is returned; otherwise, at least one
byte is read and stored into b
.
read
in class InputStream
b
- the buffer into which the data is read.off
- the start offset in array b
at which the data is written.len
- the maximum number of bytes to read.-1
if there is no more data because the end of
the stream has been reached.IOException
- If the first byte cannot be read for any reason
other than end of file, or if the wrapped stream has been closed, or if
some other I/O error occurs.NullPointerException
- If b
is null
.IndexOutOfBoundsException
- If off
is negative,
len
is negative, or len
is greater than
b.length - off
read()
public long skip(long n)
n
.
The skip
method does not invoke the underlying skip
method
of the wrapped stream class. The actual skipping of bytes will be accounted for
during subsequent substream read operations.skip
in class InputStream
n
- the number of bytes to be effectively skipped.public void mark(int readlimit)
reset
method will reposition the stream to this stored position.mark
in class InputStream
readlimit
- the maximum limit of bytes that can be read before
the mark position becomes invalid.reset()
public void reset()
mark
method
was last called.
The new reset position on substream does not take effect until subsequent reads.
reset
in class InputStream
mark(int)
public boolean markSupported()
markSupported
in class InputStream
true
mark(int)
,
reset()
public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
close
in class InputStream
IOException
Copyright © 2018. All Rights Reserved.