de.schlichtherle.io.rof
Class BufferedReadOnlyFile

java.lang.Object
  extended by de.schlichtherle.io.rof.AbstractReadOnlyFile
      extended by de.schlichtherle.io.rof.FilterReadOnlyFile
          extended by de.schlichtherle.io.rof.BufferedReadOnlyFile
All Implemented Interfaces:
ReadOnlyFile

public class BufferedReadOnlyFile
extends FilterReadOnlyFile

A ReadOnlyFile implementation which provides buffered random read only access to another ReadOnlyFile.

Note: This class implements its own virtual file pointer. Thus, if you would like to access the underlying ReadOnlyFile again after you have finished working with an instance of this class, you should synchronize their file pointers using the pattern as described in FilterReadOnlyFile.

Version:
TrueZIP 6.7
Author:
Christian Schlichtherle

Field Summary
static int WINDOW_LEN
          The default buffer length of the window to the file.
 
Fields inherited from class de.schlichtherle.io.rof.FilterReadOnlyFile
rof
 
Constructor Summary
BufferedReadOnlyFile(File file)
          Creates a new instance of BufferedReadOnlyFile.
BufferedReadOnlyFile(File file, int windowLen)
          Creates a new instance of BufferedReadOnlyFile.
BufferedReadOnlyFile(ReadOnlyFile rof)
          Creates a new instance of BufferedReadOnlyFile.
BufferedReadOnlyFile(ReadOnlyFile rof, int windowLen)
          Creates a new instance of BufferedReadOnlyFile.
 
Method Summary
 void close()
          Closes this read only file.
protected  ReadOnlyFile createReadOnlyFile(File file)
          A factory method called by the constructor to get a read only file to access the contents of the read only file.
 long getFilePointer()
           
 long length()
           
protected static long max(long a, long b)
          Returns the greater parameter.
protected static long min(long a, long b)
          Returns the smaller parameter.
 int read()
           
 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 file pointer offset, measured from the beginning of this file, at which the next read occurs.
 
Methods inherited from class de.schlichtherle.io.rof.AbstractReadOnlyFile
read, readFully, readFully, skipBytes
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

WINDOW_LEN

public static final int WINDOW_LEN
The default buffer length of the window to the file.

See Also:
Constant Field Values
Constructor Detail

BufferedReadOnlyFile

public BufferedReadOnlyFile(File file)
                     throws NullPointerException,
                            FileNotFoundException,
                            IOException
Creates a new instance of BufferedReadOnlyFile.

Parameters:
file - The file to read.
Throws:
NullPointerException - If any of the parameters is null.
FileNotFoundException - If the file cannot get opened for reading.
IOException - On any other I/O related issue.

BufferedReadOnlyFile

public BufferedReadOnlyFile(File file,
                            int windowLen)
                     throws NullPointerException,
                            FileNotFoundException,
                            IOException
Creates a new instance of BufferedReadOnlyFile.

Parameters:
file - The file to read.
windowLen - The size of the buffer window in bytes.
Throws:
NullPointerException - If any of the parameters is null.
FileNotFoundException - If the file cannot get opened for reading.
IOException - On any other I/O related issue.

BufferedReadOnlyFile

public BufferedReadOnlyFile(ReadOnlyFile rof)
                     throws NullPointerException,
                            FileNotFoundException,
                            IOException
Creates a new instance of BufferedReadOnlyFile.

Parameters:
rof - The read only file to read.
Throws:
NullPointerException - If any of the parameters is null.
FileNotFoundException - If the file cannot get opened for reading.
IOException - On any other I/O related issue.

BufferedReadOnlyFile

public BufferedReadOnlyFile(ReadOnlyFile rof,
                            int windowLen)
                     throws NullPointerException,
                            FileNotFoundException,
                            IOException
Creates a new instance of BufferedReadOnlyFile.

Parameters:
rof - The read only file to read.
windowLen - The size of the buffer window in bytes.
Throws:
NullPointerException - If any of the parameters is null.
FileNotFoundException - If the file cannot get opened for reading.
IOException - On any other I/O related issue.
Method Detail

min

protected static final long min(long a,
                                long b)
Returns the smaller parameter.


max

protected static final long max(long a,
                                long b)
Returns the greater parameter.


createReadOnlyFile

protected ReadOnlyFile createReadOnlyFile(File file)
                                   throws IOException
A factory method called by the constructor to get a read only file to access the contents of the read only file. This method is only used if the constructor isn't called with a read only file as its parameter.

Throws:
FileNotFoundException - If the file cannot get opened for reading.
IOException - On any other I/O related issue.

length

public long length()
            throws IOException
Specified by:
length in interface ReadOnlyFile
Overrides:
length in class FilterReadOnlyFile
Throws:
IOException

getFilePointer

public long getFilePointer()
                    throws IOException
Specified by:
getFilePointer in interface ReadOnlyFile
Overrides:
getFilePointer in class FilterReadOnlyFile
Throws:
IOException

seek

public void seek(long fp)
          throws IOException
Description copied from interface: ReadOnlyFile
Sets the file pointer offset, measured from the beginning of this file, at which the next read occurs. Whether the offset may be set beyond the end of the file is up to the implementor. For example, the SimpleReadOnlyFile subclasses RandomAccessFile and passes "r" as a parameter to the superclass constructor. On the Windows platform, this implementation allows to seek past the end of file, but on the Linux platform it doesn't.

Specified by:
seek in interface ReadOnlyFile
Overrides:
seek in class FilterReadOnlyFile
Parameters:
fp - The offset position, measured in bytes from the beginning of the file, at which to set the file pointer.
Throws:
IOException - If pos is less than 0 or if an I/O error occurs.

read

public int read()
         throws IOException
Specified by:
read in interface ReadOnlyFile
Overrides:
read in class FilterReadOnlyFile
Throws:
IOException

read

public int read(byte[] buf,
                int off,
                int len)
         throws IOException
Description copied from interface: ReadOnlyFile
Reads up to 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.

Specified by:
read in interface ReadOnlyFile
Overrides:
read in class FilterReadOnlyFile
Parameters:
buf - The buffer to fill with data.
off - The start offset of the data.
len - The maximum number of bytes to read.
Returns:
The total number of bytes read, or -1 if there is no more data because the end of the file has been reached.
Throws:
IOException - On any I/O related issue.

close

public void close()
           throws IOException
Closes this read only file. As a side effect, this will set the reference to the underlying read only file (FilterReadOnlyFile.rof to null.

Specified by:
close in interface ReadOnlyFile
Overrides:
close in class FilterReadOnlyFile
Throws:
IOException