public class SharedFileInputStream extends java.io.BufferedInputStream implements SharedInputStream
SharedFileInputStream
is a
BufferedInputStream
that buffers
data from the file and supports the mark
and reset
methods. It also supports the
newStream
method that allows you to create
other streams that represent subsets of the file.
A RandomAccessFile
object is used to
access the file data.
Note that when the SharedFileInputStream is closed,
all streams created with the newStream
method are also closed. This allows the creator of the
SharedFileInputStream object to control access to the
underlying file and ensure that it is closed when
needed, to avoid leaking file descriptors. Note also
that this behavior contradicts the requirements of
SharedInputStream and may change in a future release.
Modifier and Type | Class and Description |
---|---|
(package private) static class |
SharedFileInputStream.SharedFile
A shared class that keeps track of the references
to a particular file so it can be closed when the
last reference is gone.
|
Modifier and Type | Field and Description |
---|---|
protected long |
bufpos
The file offset that corresponds to the first byte in
the read buffer.
|
protected int |
bufsize
The normal size of the read buffer.
|
protected long |
datalen
The amount of data in this subset of the file.
|
private static int |
defaultBufferSize |
protected java.io.RandomAccessFile |
in
The file containing the data.
|
private boolean |
master
True if this is a top level stream created directly by "new".
|
private SharedFileInputStream.SharedFile |
sf |
protected long |
start
The file offset of the start of data in this subset of the file.
|
Modifier | Constructor and Description |
---|---|
|
SharedFileInputStream(java.io.File file)
Creates a
SharedFileInputStream
for the file. |
|
SharedFileInputStream(java.io.File file,
int size)
Creates a
SharedFileInputStream
with the specified buffer size. |
private |
SharedFileInputStream(SharedFileInputStream.SharedFile sf,
long start,
long len,
int bufsize)
Used internally by the
newStream method. |
|
SharedFileInputStream(java.lang.String file)
Creates a
SharedFileInputStream
for the named file |
|
SharedFileInputStream(java.lang.String file,
int size)
Creates a
SharedFileInputStream
with the specified buffer size. |
Modifier and Type | Method and Description |
---|---|
int |
available()
Returns the number of bytes that can be read from this input
stream without blocking.
|
void |
close()
Closes this input stream and releases any system resources
associated with the stream.
|
private void |
ensureOpen()
Check to make sure that this stream has not been closed
|
private void |
fill()
Fills the buffer with more data, taking into account
shuffling and other tricks for dealing with marks.
|
protected void |
finalize()
Force this stream to close.
|
long |
getPosition()
Return the current position in the InputStream, as an
offset from the beginning of the InputStream.
|
private int |
in_available() |
private void |
init(SharedFileInputStream.SharedFile sf,
int size) |
void |
mark(int readlimit)
See the general contract of the
mark
method of InputStream . |
boolean |
markSupported()
Tests if this input stream supports the
mark
and reset methods. |
java.io.InputStream |
newStream(long start,
long end)
Return a new InputStream representing a subset of the data
from this InputStream, starting at
start (inclusive)
up to end (exclusive). |
int |
read()
See the general contract of the
read
method of InputStream . |
int |
read(byte[] b,
int off,
int len)
Reads bytes from this stream into the specified byte array,
starting at the given offset.
|
private int |
read1(byte[] b,
int off,
int len)
Read characters into a portion of an array, reading from the underlying
stream at most once if necessary.
|
void |
reset()
See the general contract of the
reset
method of InputStream . |
long |
skip(long n)
See the general contract of the
skip
method of InputStream . |
private static int defaultBufferSize
protected java.io.RandomAccessFile in
protected int bufsize
protected long bufpos
protected long start
protected long datalen
private boolean master
private SharedFileInputStream.SharedFile sf
public SharedFileInputStream(java.io.File file) throws java.io.IOException
SharedFileInputStream
for the file.file
- the filejava.io.IOException
- for errors opening the filepublic SharedFileInputStream(java.lang.String file) throws java.io.IOException
SharedFileInputStream
for the named filefile
- the filejava.io.IOException
- for errors opening the filepublic SharedFileInputStream(java.io.File file, int size) throws java.io.IOException
SharedFileInputStream
with the specified buffer size.file
- the filesize
- the buffer size.java.io.IOException
- for errors opening the filejava.lang.IllegalArgumentException
- if size ≤ 0.public SharedFileInputStream(java.lang.String file, int size) throws java.io.IOException
SharedFileInputStream
with the specified buffer size.file
- the filesize
- the buffer size.java.io.IOException
- for errors opening the filejava.lang.IllegalArgumentException
- if size ≤ 0.private SharedFileInputStream(SharedFileInputStream.SharedFile sf, long start, long len, int bufsize)
newStream
method.private void ensureOpen() throws java.io.IOException
java.io.IOException
private void init(SharedFileInputStream.SharedFile sf, int size) throws java.io.IOException
java.io.IOException
private void fill() throws java.io.IOException
java.io.IOException
public int read() throws java.io.IOException
read
method of InputStream
.read
in class java.io.BufferedInputStream
-1
if the end of the
stream is reached.java.io.IOException
- if an I/O error occurs.private int read1(byte[] b, int off, int len) throws java.io.IOException
java.io.IOException
public int read(byte[] b, int off, int len) throws java.io.IOException
This method implements the general contract of the corresponding
method of the read
class.InputStream
read
in class java.io.BufferedInputStream
b
- destination buffer.off
- offset at which to start storing bytes.len
- maximum number of bytes to read.-1
if the end of
the stream has been reached.java.io.IOException
- if an I/O error occurs.public long skip(long n) throws java.io.IOException
skip
method of InputStream
.skip
in class java.io.BufferedInputStream
n
- the number of bytes to be skipped.java.io.IOException
- if an I/O error occurs.public int available() throws java.io.IOException
available
in class java.io.BufferedInputStream
java.io.IOException
- if an I/O error occurs.private int in_available() throws java.io.IOException
java.io.IOException
public void mark(int readlimit)
mark
method of InputStream
.mark
in class java.io.BufferedInputStream
readlimit
- the maximum limit of bytes that can be read before
the mark position becomes invalid.reset()
public void reset() throws java.io.IOException
reset
method of InputStream
.
If markpos
is -1
(no mark has been set or the mark has been
invalidated), an IOException
is thrown. Otherwise, pos
is
set equal to markpos
.
reset
in class java.io.BufferedInputStream
java.io.IOException
- if this stream has not been marked or
if the mark has been invalidated.mark(int)
public boolean markSupported()
mark
and reset
methods. The markSupported
method of SharedFileInputStream
returns
true
.markSupported
in class java.io.BufferedInputStream
boolean
indicating if this stream type supports
the mark
and reset
methods.InputStream.mark(int)
,
InputStream.reset()
public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.BufferedInputStream
java.io.IOException
- if an I/O error occurs.public long getPosition()
getPosition
in interface SharedInputStream
public java.io.InputStream newStream(long start, long end)
start
(inclusive)
up to end
(exclusive). start
must be
non-negative. If end
is -1, the new stream ends
at the same place as this stream. The returned InputStream
will also implement the SharedInputStream interface.newStream
in interface SharedInputStream
start
- the starting positionend
- the ending position + 1protected void finalize() throws java.lang.Throwable
finalize
in class java.lang.Object
java.lang.Throwable