@NotThreadSafe @DefaultAnnotation(value=edu.umd.cs.findbugs.annotations.NonNull.class) 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 |
|---|---|
private long |
fp
The virtual file pointer in the file data.
|
private long |
length |
private byte[] |
window
The buffer window to the file data.
|
static int |
WINDOW_LEN
The default buffer length of the window to the file.
|
private long |
windowOff
The current offset in the read only file where the buffer window starts.
|
delegate| Modifier | Constructor and Description |
|---|---|
|
BufferedReadOnlyFile(File file)
Constructs a new buffered read only file.
|
|
BufferedReadOnlyFile(File file,
int windowLen)
Constructs a new buffered read only file.
|
|
BufferedReadOnlyFile(ReadOnlyFile rof)
Constructs a new buffered read only file.
|
private |
BufferedReadOnlyFile(ReadOnlyFile rof,
File file,
int windowLen) |
|
BufferedReadOnlyFile(ReadOnlyFile rof,
int windowLen)
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.
|
private void |
invalidateWindow()
Forces the window to be reloaded on the next call to
positionWindow(). |
long |
length()
Returns the length of the file in bytes.
|
protected static long |
max(long a,
long b)
Returns the greater parameter.
|
protected static long |
min(long a,
long b)
Returns the smaller parameter.
|
private void |
positionWindow()
Positions the window so that the block containing the current virtual
file pointer in the encrypted file is entirely contained in it.
|
int |
read()
Reads and returns the next byte or -1 if the end of the file has been
reached.
|
int |
read(byte[] buf,
int off,
int len)
Reads up to
len bytes of data from this read only file into
the given array. |
void |
seek(long fp)
Sets the current byte position in the file as a zero-based index at
which the next read occurs.
|
toStringread, readFully, readFullyprivate long fp
private long length
private final byte[] window
public static final int WINDOW_LEN
private long windowOff
public BufferedReadOnlyFile(File file) throws IOException
file - The file to read.FileNotFoundException - If the file cannot get opened for reading.IOException - On any other I/O related issue.public BufferedReadOnlyFile(File file, int windowLen) throws IOException
file - The file to read.windowLen - The size of the buffer window in bytes.FileNotFoundException - If the file cannot get opened for reading.IOException - On any other I/O related issue.public BufferedReadOnlyFile(ReadOnlyFile rof) throws IOException
rof - The read only file to read.FileNotFoundException - If the file cannot get opened for reading.IOException - On any other I/O related issue.private BufferedReadOnlyFile(@CheckForNull ReadOnlyFile rof, @CheckForNull File file, int windowLen) throws IOException
IOExceptionpublic BufferedReadOnlyFile(ReadOnlyFile rof, int windowLen) throws IOException
rof - The read only file to read.windowLen - The size of the buffer window in bytes.FileNotFoundException - If the file cannot get opened for reading.IOException - On any other I/O related issue.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 ReadOnlyFileclose in interface Closeableclose in interface AutoCloseableclose in class DecoratingReadOnlyFileIOExceptionpublic long getFilePointer()
throws IOException
ReadOnlyFilegetFilePointer in interface ReadOnlyFilegetFilePointer in class DecoratingReadOnlyFileIOExceptionprivate void invalidateWindow()
positionWindow().public long length()
throws IOException
ReadOnlyFilelength in interface ReadOnlyFilelength in class DecoratingReadOnlyFileIOExceptionprotected static long max(long a,
long b)
protected static long min(long a,
long b)
private void positionWindow()
throws IOException
IOException - On any I/O related issue.
The window is invalidated in this case.public int read()
throws IOException
ReadOnlyFileread in interface ReadOnlyFileread in class DecoratingReadOnlyFileIOExceptionpublic int read(byte[] buf,
int off,
int len)
throws IOException
ReadOnlyFilelen 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 ReadOnlyFileread in class DecoratingReadOnlyFilebuf - The buffer to fill with data.off - The start offset of the data.len - 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 related issue.public void seek(long fp)
throws IOException
ReadOnlyFileDefaultReadOnlyFile
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 ReadOnlyFileseek in class DecoratingReadOnlyFilefp - The current byte position as a zero-based index.IOException - If pos is less than 0 or if
an I/O error occurs.Copyright © 2004-2011 Schlichtherle IT Services. All Rights Reserved.