de.schlichtherle.crypto.io
Class CipherReadOnlyFile

java.lang.Object
  extended by de.schlichtherle.io.rof.AbstractReadOnlyFile
      extended by de.schlichtherle.io.rof.FilterReadOnlyFile
          extended by de.schlichtherle.crypto.io.CipherReadOnlyFile
All Implemented Interfaces:
ReadOnlyFile
Direct Known Subclasses:
RaesReadOnlyFile

public abstract class CipherReadOnlyFile
extends FilterReadOnlyFile

A read only file for transparent random read access to an encrypted file.

The client must call init(SeekableBlockCipher, long, long) before it can actually read anything!

Note that 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 the base class FilterReadOnlyFile.

Since:
TrueZIP 6.0
Version:
TrueZIP 6.7
Author:
Christian Schlichtherle

Field Summary
 
Fields inherited from class de.schlichtherle.io.rof.FilterReadOnlyFile
rof
 
Constructor Summary
CipherReadOnlyFile(ReadOnlyFile rof)
          Creates a read only file for transparent random read access to an encrypted file.
 
Method Summary
 void close()
          Closes this read only file and releases any resources associated with it.
protected  byte[] computeMac(Mac mac)
          Returns the authentication code of the encrypted data in this cipher read only file using the given Message Authentication Code (MAC) object.
 long getFilePointer()
           
 void init(SeekableBlockCipher cipher, long start, long length)
          Initializes this cipher read only file - must be called before first read access!
 long length()
           
 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
 

Constructor Detail

CipherReadOnlyFile

public CipherReadOnlyFile(ReadOnlyFile rof)
Creates a read only file for transparent random read access to an encrypted file. The client must call init(SeekableBlockCipher, long, long) before it can actually read anything!

Parameters:
rof - A read-only file. This may be null, but must be properly init before the call to init().
Method Detail

init

public void init(SeekableBlockCipher cipher,
                 long start,
                 long length)
          throws IOException
Initializes this cipher read only file - must be called before first read access!

Parameters:
start - The start offset of the encrypted data in this file.
length - The length of the encrypted data in this file.
Throws:
IOException - If this read only file has already been closed. This exception is not recoverable.
IllegalStateException - If this object has already been initialized. This exception is not recoverable.
NullPointerException - If FilterReadOnlyFile.rof is null or cipher is null. This exception is recoverable.

computeMac

protected byte[] computeMac(Mac mac)
                     throws IOException
Returns the authentication code of the encrypted data in this cipher read only file using the given Message Authentication Code (MAC) object. It is safe to call this method multiple times to detect if the file has been tampered with meanwhile.

Parameters:
mac - A properly initialized MAC object.
Throws:
IOException - On any 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 and releases any resources associated with it. This method invalidates the state of this object, causing any subsequent calls to a public method to fail with an IOException.

Specified by:
close in interface ReadOnlyFile
Overrides:
close in class FilterReadOnlyFile
Throws:
IOException - If an I/O error occurs.