@NotThreadSafe public class BufferedReadOnlyFile extends DecoratingReadOnlyFile
ReadOnlyFile
implementation which provides buffered random read
only access to another ReadOnlyFile
.
Note that this class implements a virtual file pointer. Thus, if you would like to use the decorated read only file again after you have finished using the decorating read only file, then you should synchronize their file pointers using the following idiom:
ReadOnlyFile rof = new DefaultReadOnlyFile(new File("HelloWorld.java")); try { ReadOnlyFile brof = new BufferedReadOnlyFile(rof); try { // Do any file input on frof here... brof.seek(1); } finally { // Synchronize the file pointers. rof.seek(brof.getFilePointer()); } // This assertion would fail if we hadn't done the file pointer // synchronization! assert rof.getFilePointer() == 1; } finally { rof.close(); }
Modifier and Type | Field and Description |
---|---|
static int |
WINDOW_LEN
The default buffer length of the window to the file.
|
delegate
Constructor and Description |
---|
BufferedReadOnlyFile(File file)
Constructs a new buffered read only file.
|
BufferedReadOnlyFile(File file,
int bufferSize)
Constructs a new buffered read only file.
|
BufferedReadOnlyFile(ReadOnlyFile rof)
Constructs a new buffered read only file.
|
BufferedReadOnlyFile(ReadOnlyFile rof,
int bufferSize)
Constructs a new buffered read only file.
|
Modifier and Type | Method and Description |
---|---|
protected void |
assertOpen()
Asserts that this file is open.
|
void |
close()
Closes this read only file.
|
long |
getFilePointer()
Returns the current byte position in the file as a zero-based index.
|
long |
length()
Returns the length of the file in bytes.
|
int |
read()
Reads and returns the next byte or -1 if the end of the file has been
reached.
|
int |
read(byte[] dst,
int offset,
int remaining)
Reads up to
len bytes of data from this read only file into
the given array. |
void |
seek(long pos)
Sets the current byte position in the file as a zero-based index at
which the next read occurs.
|
toString
read, readFully, readFully
public static final int WINDOW_LEN
@CreatesObligation public BufferedReadOnlyFile(File file) throws IOException
file
- The file to read.FileNotFoundException
- If the file cannot get opened for reading.IOException
- on any I/O error.@CreatesObligation public BufferedReadOnlyFile(File file, int bufferSize) throws IOException
file
- the file to read.bufferSize
- the size of the buffer window in bytes.FileNotFoundException
- if the file cannot get opened for reading.IOException
- on any I/O error.@CreatesObligation public BufferedReadOnlyFile(@WillCloseWhenClosed ReadOnlyFile rof) throws IOException
rof
- the read only file to read.FileNotFoundException
- if the file cannot get opened for reading.IOException
- on any I/O error.@CreatesObligation public BufferedReadOnlyFile(@WillCloseWhenClosed ReadOnlyFile rof, int bufferSize) throws IOException
rof
- the read only file to read.bufferSize
- the size of the buffer window in bytes.FileNotFoundException
- if the file cannot get opened for reading.IOException
- on any I/O error.protected final void assertOpen() throws IOException
IOException
- If the preconditions do not hold.public void close() throws IOException
DecoratingReadOnlyFile.delegate
to null
.close
in interface ReadOnlyFile
close
in interface Closeable
close
in interface AutoCloseable
close
in class DecoratingReadOnlyFile
IOException
- On any I/O failure.public long getFilePointer() throws IOException
ReadOnlyFile
getFilePointer
in interface ReadOnlyFile
getFilePointer
in class DecoratingReadOnlyFile
IOException
- On any I/O failure.public long length() throws IOException
ReadOnlyFile
length
in interface ReadOnlyFile
length
in class DecoratingReadOnlyFile
IOException
- On any I/O failure.public int read() throws IOException
ReadOnlyFile
read
in interface ReadOnlyFile
read
in class DecoratingReadOnlyFile
IOException
- On any I/O failure.public int read(byte[] dst, int offset, int remaining) throws IOException
ReadOnlyFile
len
bytes of data from this read only file into
the given array.
This method blocks until at least one byte of input is available unless
len
is zero.read
in interface ReadOnlyFile
read
in class DecoratingReadOnlyFile
dst
- The buffer to fill with data.offset
- The start offset of the data.remaining
- The maximum number of bytes to read.-1
if there is
no more data because the end of the file has been reached.IOException
- On any I/O failure.public void seek(long pos) throws IOException
ReadOnlyFile
DefaultReadOnlyFile
passes "r"
as a parameter to the constructor of its super-class
RandomAccessFile
.
With Oracle's JSE implementation, on the Windows platform this
implementation allows to seek past the end of file, but on the Linux
platform it doesn't.seek
in interface ReadOnlyFile
seek
in class DecoratingReadOnlyFile
pos
- The current byte position as a zero-based index.IOException
- If pos
is less than 0
or on any
I/O failure.Copyright © 2005–2018 Schlichtherle IT Services. All rights reserved.