edu.sdsc.grid.io.local
Class LocalRandomAccessFile

java.lang.Object
  extended by edu.sdsc.grid.io.GeneralRandomAccessFile
      extended by edu.sdsc.grid.io.local.LocalRandomAccessFile
All Implemented Interfaces:
java.io.DataInput, java.io.DataOutput

public class LocalRandomAccessFile
extends GeneralRandomAccessFile

A wrapper class for java.io.RandomAccessFile.

Since:
GridTools1.0
See Also:
RandomAccessFile, GeneralRandomAccessFile

Field Summary
 
Fields inherited from class edu.sdsc.grid.io.GeneralRandomAccessFile
fd, file, fileFormat, isClosed, mode, rw, SEEK_CURRENT, SEEK_END, SEEK_START, swapNeeded
 
Constructor Summary
LocalRandomAccessFile(java.io.File file, java.lang.String mode)
          Creates a random access file stream to read from, and optionally to write to, the file specified by the File argument.
LocalRandomAccessFile(LocalFile file, java.lang.String mode)
          Creates a random access file stream to read from, and optionally to write to, the file specified by the LocalFile argument.
LocalRandomAccessFile(java.lang.String name, java.lang.String mode)
          Creates a random access file stream to read from, and optionally to write to, the file specified by the String argument.
 
Method Summary
 void close()
          Closes this random access file stream and releases any system resources associated with the stream.
 long getFilePointer()
          Returns the current offset in this file.
 java.io.RandomAccessFile getRandomAccessFile()
          Returns this object as a standard java.io.RandomAccessFile.
 long length()
          Returns the length of this file.
protected  void open(GeneralFile file)
          Opens a file and returns the file descriptor.
 int read()
          Reads a byte of data from this file.
protected  int readBytes(byte[] b, int offset, int len)
          Reads a sub array as a sequence of bytes.
 void seek(long position)
          Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.
 void seek(long position, int origin)
          Sets the file-pointer offset at which the next read or write occurs.
 void setLength(long newLength)
          Sets the length of this file.
 int skipBytes(int n)
          Attempts to skip over n bytes of input discarding the skipped bytes.
 void write(int b)
          Writes the specified byte to this file.
protected  void writeBytes(byte[] b, int offset, int len)
          Writes a sub array as a sequence of bytes.
 
Methods inherited from class edu.sdsc.grid.io.GeneralRandomAccessFile
finalize, getBinaryDataFormat, getFD, getFile, getFileSystem, isClosed, read, read, readBoolean, readBooleans, readByte, readChar, readDouble, readDoubles, readFloat, readFloats, readFully, readFully, readInt, readInts, readLine, readLong, readLongDouble, readLongDoubles, readLongLong, readLongLongs, readLongs, readShort, readShorts, readUnsignedByte, readUnsignedInt, readUnsignedInts, readUnsignedLong, readUnsignedLongLong, readUnsignedLongLongs, readUnsignedLongs, readUnsignedShort, readUnsignedShorts, readUTF, rwCheck, setBinaryDataFormat, toString, write, write, write, writeBoolean, writeBooleans, writeByte, writeBytes, writeChar, writeChars, writeDouble, writeDoubles, writeFloat, writeFloats, writeInt, writeInts, writeLong, writeLongDouble, writeLongDoubles, writeLongLong, writeLongLongs, writeLongs, writeShort, writeShorts, writeUTF
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LocalRandomAccessFile

public LocalRandomAccessFile(java.lang.String name,
                             java.lang.String mode)
                      throws java.io.IOException
Creates a random access file stream to read from, and optionally to write to, the file specified by the String argument. A new file descriptor is obtained which represents the connection to the file.

The mode argument specifies the access mode in which the file is to be opened. The permitted values and their meanings are:

"r" Open for reading only. Invoking any of the write methods of the resulting object will cause an IOException to be thrown.
"rw" Open for reading and writing. If the file does not already exist then an attempt will be made to create it.
"rws" Open for reading and writing, as with "rw", and also require that every update to the file's content or metadata be written synchronously to the underlying storage device.
"rwd"   Open for reading and writing, as with "rw", and also require that every update to the file's content be written synchronously to the underlying storage device.
On construction a check is made to see if read access to the file is allowed. If the mode allows writing, write access to the file is also checked.

Parameters:
file - the file object
mode - the access mode, as described above
Throws:
java.io.IOException - If an I/O error occurs
java.lang.IllegalArgumentException - if the mode argument is not equal to one of "r", "rw", "rws", or "rwd"
java.io.FileNotFoundException - If the file exists but is a directory rather than a regular file, or cannot be opened or created for any other reason
java.lang.SecurityException - If denied read access to the file or the mode is "rw" and denied write access to the file.

LocalRandomAccessFile

public LocalRandomAccessFile(java.io.File file,
                             java.lang.String mode)
                      throws java.io.IOException
Creates a random access file stream to read from, and optionally to write to, the file specified by the File argument. A new file descriptor is obtained which represents the connection to the file.

The mode argument specifies the access mode in which the file is to be opened. The permitted values and their meanings are:

"r" Open for reading only. Invoking any of the write methods of the resulting object will cause an IOException to be thrown.
"rw" Open for reading and writing. If the file does not already exist then an attempt will be made to create it.
"rws" Open for reading and writing, as with "rw", and also require that every update to the file's content or metadata be written synchronously to the underlying storage device.
"rwd"   Open for reading and writing, as with "rw", and also require that every update to the file's content be written synchronously to the underlying storage device.
On construction a check is made to see if read access to the file is allowed. If the mode allows writing, write access to the file is also checked.

Parameters:
file - the file object
mode - the access mode, as described above
Throws:
java.io.IOException - If an I/O error occurs
java.lang.IllegalArgumentException - if the mode argument is not equal to one of "r", "rw", "rws", or "rwd"
java.io.FileNotFoundException - If the file exists but is a directory rather than a regular file, or cannot be opened or created for any other reason
java.lang.SecurityException - If denied read access to the file or the mode is "rw" and denied write access to the file.

LocalRandomAccessFile

public LocalRandomAccessFile(LocalFile file,
                             java.lang.String mode)
                      throws java.io.IOException
Creates a random access file stream to read from, and optionally to write to, the file specified by the LocalFile argument. A new file descriptor is obtained which represents the connection to the file.

The mode argument specifies the access mode in which the file is to be opened. The permitted values and their meanings are:

"r" Open for reading only. Invoking any of the write methods of the resulting object will cause an IOException to be thrown.
"rw" Open for reading and writing. If the file does not already exist then an attempt will be made to create it.
"rws" Open for reading and writing, as with "rw", and also require that every update to the file's content or metadata be written synchronously to the underlying storage device.
"rwd"   Open for reading and writing, as with "rw", and also require that every update to the file's content be written synchronously to the underlying storage device.
On construction a check is made to see if read access to the file is allowed. If the mode allows writing, write access to the file is also checked.

Parameters:
file - the file object
mode - the access mode, as described above
Throws:
java.io.IOException - If an I/O error occurs
java.lang.IllegalArgumentException - if the mode argument is not equal to one of "r", "rw", "rws", or "rwd"
java.io.FileNotFoundException - If the file exists but is a directory rather than a regular file, or cannot be opened or created for any other reason
java.lang.SecurityException - If denied read access to the file or the mode is "rw" and denied write access to the file.
Method Detail

getRandomAccessFile

public java.io.RandomAccessFile getRandomAccessFile()
                                             throws java.io.IOException
Returns this object as a standard java.io.RandomAccessFile.

Throws:
java.io.IOException - if an I/O error occurs.
See Also:
RandomAccessFile

open

protected void open(GeneralFile file)
             throws java.io.FileNotFoundException
Description copied from class: GeneralRandomAccessFile
Opens a file and returns the file descriptor. The file is opened in read-write mode if writeable is true, else the file is opened as read-only. If the name refers to a directory, an IOException is thrown.

Specified by:
open in class GeneralRandomAccessFile
Throws:
java.io.FileNotFoundException

read

public int read()
         throws java.io.IOException
Description copied from class: GeneralRandomAccessFile
Reads a byte of data from this file. The byte is returned as an integer in the range 0 to 255 (0x00-0x0ff). This method blocks if no input is yet available.

Although GeneralRandomAccessFile is not a subclass of InputStream, this method behaves in exactly the same way as java.io.InputStream.read().

Specified by:
read in class GeneralRandomAccessFile
Returns:
the next byte of data, or -1 if the end of the file has been reached.
Throws:
java.io.IOException - if an I/O error occurs. Not thrown if end-of-file has been reached.

readBytes

protected int readBytes(byte[] b,
                        int offset,
                        int len)
                 throws java.io.IOException
Description copied from class: GeneralRandomAccessFile
Reads a sub array as a sequence of bytes.

Specified by:
readBytes in class GeneralRandomAccessFile
Parameters:
b - the data to be written
len - the number of bytes that are written
Throws:
java.io.IOException - If an I/O error has occurred.

skipBytes

public int skipBytes(int n)
              throws java.io.IOException
Description copied from class: GeneralRandomAccessFile
Attempts to skip over n bytes of input discarding the skipped bytes.

This method may skip over some smaller number of bytes, possibly zero. This may result from any of a number of conditions; reaching end of file before n bytes have been skipped is only one possibility. This method never throws an EOFException. The actual number of bytes skipped is returned. If n is negative, no bytes are skipped.

Specified by:
skipBytes in interface java.io.DataInput
Overrides:
skipBytes in class GeneralRandomAccessFile
Parameters:
n - the number of bytes to be skipped.
Returns:
the actual number of bytes skipped.
Throws:
java.io.IOException - if an I/O error occurs.

write

public void write(int b)
           throws java.io.IOException
Description copied from class: GeneralRandomAccessFile
Writes the specified byte to this file. The write starts at the current file pointer.

Specified by:
write in interface java.io.DataOutput
Overrides:
write in class GeneralRandomAccessFile
Parameters:
b - the byte to be written.
Throws:
java.io.IOException - if an I/O error occurs.

writeBytes

protected void writeBytes(byte[] b,
                          int offset,
                          int len)
                   throws java.io.IOException
Description copied from class: GeneralRandomAccessFile
Writes a sub array as a sequence of bytes.

Specified by:
writeBytes in class GeneralRandomAccessFile
Parameters:
b - the data to be written
len - the number of bytes that are written
Throws:
java.io.IOException - If an I/O error has occurred.

getFilePointer

public long getFilePointer()
                    throws java.io.IOException
Returns the current offset in this file.

Specified by:
getFilePointer in class GeneralRandomAccessFile
Returns:
the offset from the beginning of the file, in bytes, at which the next read or write occurs.
Throws:
java.io.IOException - if an I/O error occurs.

seek

public void seek(long position)
          throws java.io.IOException
Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs. The offset may be set beyond the end of the file. Setting the offset beyond the end of the file does not change the file length. The file length will change only by writing after the offset has been set beyond the end of the file.

Overrides:
seek in class GeneralRandomAccessFile
Parameters:
pos - the offset position, measured in bytes from the beginning of the file, at which to set the file pointer.
Throws:
java.io.IOException - if pos is less than 0 or if an I/O error occurs.

seek

public void seek(long position,
                 int origin)
          throws java.io.IOException
Sets the file-pointer offset at which the next read or write occurs. The offset may be set beyond the end of the file. Setting the offset beyond the end of the file does not change the file length. The file length will change only by writing after the offset has been set beyond the end of the file.

Specified by:
seek in class GeneralRandomAccessFile
Parameters:
pos - the offset position, measured in bytes from the at which to set the file pointer.
origin - a Sets offset for the beginning of the seek.
SEEK_START - sets the offset from the beginning of the file. SEEK_CURRENT - sets the offset from the current position of the filePointer.
SEEK_END - sets the offset from the end of the file.
Throws:
java.io.IOException - if pos is less than 0 or if an I/O error occurs.

length

public long length()
            throws java.io.IOException
Returns the length of this file.

Specified by:
length in class GeneralRandomAccessFile
Returns:
the length of this file, measured in bytes.
Throws:
java.io.IOException - if an I/O error occurs.

setLength

public void setLength(long newLength)
               throws java.io.IOException
Sets the length of this file.

If the present length of the file as returned by the length method is greater than the newLength argument then the file will be truncated. In this case, if the file offset as returned by the getFilePointer method is greater then newLength then after this method returns the offset will be equal to newLength.

If the present length of the file as returned by the length method is smaller than the newLength argument then the file will be extended. In this case, the contents of the extended portion of the file are not defined.

Specified by:
setLength in class GeneralRandomAccessFile
Parameters:
newLength - The desired length of the file
Throws:
java.io.IOException - If an I/O error occurs

close

public void close()
           throws java.io.IOException
Closes this random access file stream and releases any system resources associated with the stream. A closed random access file cannot perform input or output operations and cannot be reopened.

Specified by:
close in class GeneralRandomAccessFile
Throws:
java.io.IOException - if an I/O error occurs.