Servertec
Foundation Classes
1.3.3 09/04/2005

stec.sfc.Win32
Class RandomAccessFile

java.lang.Object
  extended by stec.sfc.Win32.RandomAccessFile

public final class RandomAccessFile
extends java.lang.Object

Used to create and open random access files, to lock and unlock regions within the file, to perform seek, read and write operation on the file and to get and set the file's length.

Since:
1.0.0 01/07/2001

Constructor Summary
RandomAccessFile(File file, int access_mode, int share_mode, int operation, int attributes)
          Constructs a new RandomAcessFile object for the specified file.
RandomAccessFile(java.lang.String filename, int access_mode, int share_mode, int operation, int attributes)
          Constructs a new RandomAcessFile object for the specified file.
 
Method Summary
 void close()
          Closes the file.
 void flush()
          Writes buffered output to the file.
 long getFilePointer()
          Returns the file's current offset.
 long length()
          Return's the file's current length.
 boolean lock(long offset, long bytes)
          Locks the specified region.
 boolean lock(long offset, long bytes, int lock_mode)
          Locks the specified region.
 int read()
          Reads the next byte from the file.
 int read(byte[] buffer)
          Reads as many bytes as will fit into the specified byte buffer.
 int read(byte[] buffer, int bytes)
          Reads the next n bytes into the specified byte buffer.
 int read(byte[] buffer, int offset, int length)
          Reads the next n bytes into the specified byte buffer starting at the given offset.
 boolean readBoolean()
          Returns the next boolean value from the file.
 byte readByte()
          Returns the next byte value from the file.
 char readChar()
          Returns the next character value from the file.
 java.lang.String readCString()
          Reads the next C string from the file.
 double readDouble()
          Returns the next long floating point value from the file.
 float readFloat()
          Returns the next floating point value from the file.
 void readFully(byte[] buffer)
          Reads from the specified file until the specified buffer is filled or end of file is reached.
 void readFully(byte[] buffer, int offset, int length)
          Reads the specified number of bytes into the given buffer from the specified file.
 int readInt()
          Returns the next integer value from the file.
 java.lang.String readLine()
          Returns the next line from the file.
 long readLong()
          Returns the next long integer value from the file.
 java.lang.Object readObject()
          Reads the next Object from the file.
 short readShort()
          Returns the next short value from the file.
 int readUnsignedByte()
          Reads the next unsigned byte from the file.
 long readUnsignedInt()
          Reads the next unsigned integer from the file.
 int readUnsignedShort()
          Reads the next unsigned short from the file.
 java.lang.String readUTF()
          Reads the UTF-8 encoded string from the file.
 long seek(long offset)
          Sets the file's current offset to the specified offset relative to the begining of the file.
 long seek(long offset, int origin_method)
          Change the file's current offset.
 void seekEOF()
          Sets the file pointer to end of file (EOF).
 long setLength(long length)
          Sets the file's length.
 long skipBytes(long bytes)
          Skips the specified number of bytes and returns the file's current offset.
 boolean unlock(long offset, long bytes)
          Unlocks the specified region.
 int write(byte[] buffer)
          Writes the specified byte buffer to the file.
 int write(byte[] buffer, int bytes)
          Writes n bytes from the specified byte buffer to the file.
 int write(byte[] buffer, int offset, int length)
          Writes n bytes from the specified byte buffer to the file starting at the given offset.
 void write(int byt)
          Writes the specified byte to the file.
 void writeBoolean(boolean b)
          Writes the specified boolean to the file.
 void writeByte(int b)
          Writes the specified byte to the file.
 void writeBytes(java.lang.String s)
          Writes the specified string as an array of bytes to the file.
 void writeChar(int c)
          Writes the specified character to the file.
 void writeChars(java.lang.String s)
          Writes the specified string as an array of characters to the file.
 void writeCString(java.lang.String s)
          Writes the specified string as C string to the file.
 void writeDouble(double d)
          Writes the specified long floating point to the file.
 void writeFloat(float f)
          Writes the specified floating point to the file.
 void writeInt(int i)
          Writes the specified integer to the file.
 void writeLine()
          Writes an empty line to the file.
 void writeLine(java.lang.String s)
          Writes the specified line to the file.
 void writeLong(long l)
          Writes the specified long integer to the file.
 void writeObject(java.lang.Object o)
          Writes the specified Object to the file.
 void writeShort(int s)
          Writes the specified short to the file.
 void writeUTF(java.lang.String s)
          Writes the specified string as an UTF-8 encoded string to the file.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RandomAccessFile

public RandomAccessFile(File file,
                        int access_mode,
                        int share_mode,
                        int operation,
                        int attributes)
                 throws java.io.IOException
Constructs a new RandomAcessFile object for the specified file.

Parameters:
file - the file to create or open for random acess.
access_mode - the FileAccessMode.
share_mode - the FileShareMode.
operation - the FileOperation.
attributes - the CreateFileAttribute.
Throws:
java.io.IOException - if any error occurs.

RandomAccessFile

public RandomAccessFile(java.lang.String filename,
                        int access_mode,
                        int share_mode,
                        int operation,
                        int attributes)
                 throws java.io.IOException
Constructs a new RandomAcessFile object for the specified file.

Parameters:
filename - the file to create or open for random acess.
access_mode - the FileAccessMode.
share_mode - the FileShareMode.
operation - the FileOperation.
attributes - the CreateFileAttribute.
Throws:
java.io.IOException - if any error occurs.
Method Detail

close

public final void close()
                 throws java.io.IOException
Closes the file.

Throws:
java.io.IOException - if any error occurs.

flush

public final void flush()
                 throws java.io.IOException
Writes buffered output to the file.

Throws:
java.io.IOException - if any error occurs.

lock

public final boolean lock(long offset,
                          long bytes)
                   throws java.io.IOException
Locks the specified region.

Parameters:
offset - the starting offset
bytes - the number of bytes to lock
Returns:
whether the region was locked.
Throws:
java.io.IOException - if any error occurs.

lock

public final boolean lock(long offset,
                          long bytes,
                          int lock_mode)
                   throws java.io.IOException
Locks the specified region.

Parameters:
offset - the starting offset
bytes - the number of bytes to lock
lock_mode - the lock mode. Defined in FileLockMode.
Returns:
whether the region was locked.
Throws:
java.io.IOException - if any error occurs.
Since:
1.2.0 12/03/2002

unlock

public final boolean unlock(long offset,
                            long bytes)
                     throws java.io.IOException
Unlocks the specified region.

Parameters:
offset - the starting offset
bytes - the number of bytes to unlock
Returns:
whether the region was unlocked.
Throws:
java.io.IOException - if any error occurs.

read

public final int read()
               throws java.io.IOException
Reads the next byte from the file.

Returns:
the byte read.

-1 if at end of file (EOF).

Throws:
java.io.IOException - if any error occurs.

read

public final int read(byte[] buffer,
                      int offset,
                      int length)
               throws java.io.IOException,
                      java.lang.IllegalArgumentException
Reads the next n bytes into the specified byte buffer starting at the given offset.

Parameters:
buffer - the byte buffer to read into
offset - the starting offset
length - the number of bytes to read
Returns:
the number of bytes read.

-1 if at end of file (EOF).

Throws:
java.lang.IllegalArgumentException - if the specified offset is less than zero or greater than or equal to the given byte array size or if the specified number of bytes to read is larger than the remaining space in the specified given byte array.
java.io.IOException - if any error occurs.
Since:
1.1.0 11/18/2001

read

public final int read(byte[] buffer,
                      int bytes)
               throws java.io.IOException,
                      java.lang.IllegalArgumentException
Reads the next n bytes into the specified byte buffer.

Parameters:
buffer - the byte buffer to read into
bytes - the number of bytes to read
Returns:
the number of bytes read.

-1 if at end of file (EOF).

Throws:
java.lang.IllegalArgumentException - if the specified offset is less than zero or greater than or equal to the given byte array size or if the specified number of bytes to read is larger than the remaining space in the specified given byte array.
java.io.IOException - if any error occurs.

write

public final void write(int byt)
                 throws java.io.IOException
Writes the specified byte to the file.

Parameters:
byt - the byte to write.
Throws:
java.io.IOException - if any error occurs.

write

public final int write(byte[] buffer,
                       int offset,
                       int length)
                throws java.io.IOException,
                       java.lang.IllegalArgumentException
Writes n bytes from the specified byte buffer to the file starting at the given offset.

Parameters:
buffer - the byte buffer to write.
offset - the starting offset
length - the number of bytes to write.
Returns:
the number of bytes written.
Throws:
java.lang.IllegalArgumentException - if the specified offset is less than zero or greater than or equal to the given byte array size or if the specified number of bytes to write is larger than the remaining space in the specified given byte array.
java.io.IOException - if any error occurs.
Since:
1.1.0 11/18/2001

write

public final int write(byte[] buffer,
                       int bytes)
                throws java.io.IOException,
                       java.lang.IllegalArgumentException
Writes n bytes from the specified byte buffer to the file.

Parameters:
buffer - the byte buffer to write.
bytes - the number of bytes to write.
Returns:
the number of bytes written.
Throws:
java.lang.IllegalArgumentException - if the specified offset is less than zero or greater than or equal to the given byte array size or if the specified number of bytes to write is larger than the remaining space in the specified given byte array.
java.io.IOException - if any error occurs.

seek

public final long seek(long offset,
                       int origin_method)
                throws java.io.IOException
Change the file's current offset.

Parameters:
offset - the relative offset.
origin_method - the FileSeekMethod.
Returns:
the file's new current offset.
Throws:
java.io.IOException - if any error occurs.

seek

public final long seek(long offset)
                throws java.io.IOException
Sets the file's current offset to the specified offset relative to the begining of the file.

Parameters:
offset - the offset relative to the begining of the file.
Returns:
the file's new current offset.
Throws:
java.io.IOException - if any error occurs.
Since:
1.1.0 11/18/2001

getFilePointer

public final long getFilePointer()
                          throws java.io.IOException
Returns the file's current offset.

Returns:
the file's current offset.
Throws:
java.io.IOException - if any error occurs.
Since:
1.1.0 11/18/2001

skipBytes

public final long skipBytes(long bytes)
                     throws java.io.IOException
Skips the specified number of bytes and returns the file's current offset.

Parameters:
bytes - the number of bytes to skep.
Returns:
the file's new current offset.
Throws:
java.io.IOException - if any error occurs.
Since:
1.1.0 11/18/2001

length

public final long length()
                  throws java.io.IOException
Return's the file's current length.

Returns:
the file's current legnth.
Throws:
java.io.IOException - if any error occurs.

setLength

public final long setLength(long length)
                     throws java.io.IOException
Sets the file's length.

Parameters:
length - the new file length.
Returns:
the file's new length.
Throws:
java.io.IOException - if any error occurs.

read

public final int read(byte[] buffer)
               throws java.io.IOException
Reads as many bytes as will fit into the specified byte buffer.

Parameters:
buffer - the byte buffer to read into
Returns:
the number of bytes read.

-1 if at end of file (EOF).

Throws:
java.io.IOException - if any error occurs.

write

public final int write(byte[] buffer)
                throws java.io.IOException
Writes the specified byte buffer to the file.

Parameters:
buffer - the byte buffer to write.
Returns:
the number of bytes written.
Throws:
java.io.IOException - if any error occurs.

readBoolean

public final boolean readBoolean()
                          throws java.io.IOException,
                                 java.io.EOFException
Returns the next boolean value from the file.

Returns:
the boolean value read.
Throws:
java.io.EOFException - if the end of file has been reached.
java.io.IOException - if the file has been closed or if any error occurs.
Since:
1.1.0 11/18/2001

readByte

public final byte readByte()
                    throws java.io.IOException,
                           java.io.EOFException
Returns the next byte value from the file.

Returns:
the byte value read.
Throws:
java.io.EOFException - if the end of file has been reached.
java.io.IOException - if the file has been closed or if any error occurs.
Since:
1.1.0 11/18/2001

readShort

public final short readShort()
                      throws java.io.IOException,
                             java.io.EOFException
Returns the next short value from the file.

Returns:
the short value read.
Throws:
java.io.EOFException - if the end of file has been reached.
java.io.IOException - if the file has been closed or if any error occurs.
Since:
1.1.0 11/18/2001

readChar

public final char readChar()
                    throws java.io.IOException,
                           java.io.EOFException
Returns the next character value from the file.

Returns:
the character value read.
Throws:
java.io.EOFException - if the end of file has been reached.
java.io.IOException - if the file has been closed or if any error occurs.
Since:
1.1.0 11/18/2001

readInt

public final int readInt()
                  throws java.io.IOException,
                         java.io.EOFException
Returns the next integer value from the file.

Returns:
the integer value read.
Throws:
java.io.EOFException - if the end of file has been reached.
java.io.IOException - if the file has been closed or if any error occurs.
Since:
1.1.0 11/18/2001

readLong

public final long readLong()
                    throws java.io.IOException,
                           java.io.EOFException
Returns the next long integer value from the file.

Returns:
the short value read.
Throws:
java.io.EOFException - if the end of file has been reached.
java.io.IOException - if the file has been closed or if any error occurs.
Since:
1.1.0 11/18/2001

readFloat

public final float readFloat()
                      throws java.io.IOException,
                             java.io.EOFException
Returns the next floating point value from the file.

Returns:
the floating point value read.
Throws:
java.io.EOFException - if the end of file has been reached.
java.io.IOException - if the file has been closed or if any error occurs.
Since:
1.1.0 11/18/2001

readDouble

public final double readDouble()
                        throws java.io.IOException,
                               java.io.EOFException
Returns the next long floating point value from the file.

Returns:
the long floating point value read.
Throws:
java.io.EOFException - if the end of file has been reached.
java.io.IOException - if the file has been closed or if any error occurs.
Since:
1.1.0 11/18/2001

readLine

public final java.lang.String readLine()
                                throws java.io.IOException
Returns the next line from the file.

Returns:
the line value read.
Throws:
java.io.EOFException - if the end of file has been reached.
java.io.IOException - if the file has been closed or if any error occurs.
Since:
1.1.0 11/18/2001

writeLine

public final void writeLine()
                     throws java.io.IOException
Writes an empty line to the file.

Throws:
java.io.IOException - if the file has been closed or if any error occurs.
Since:
1.1.0 11/18/2001

writeLine

public final void writeLine(java.lang.String s)
                     throws java.io.IOException
Writes the specified line to the file.

Parameters:
s - the line to write.
Throws:
java.io.IOException - if the file has been closed or if any error occurs.
Since:
1.1.0 11/18/2001

readFully

public final void readFully(byte[] buffer,
                            int offset,
                            int length)
                     throws java.io.IOException,
                            java.io.EOFException,
                            java.lang.IllegalArgumentException
Reads the specified number of bytes into the given buffer from the specified file. This method blocks until the specified number of bytes is read or an exception is thrown.

Parameters:
buffer - the buffer to read into.
offset - the starting offset into the specified buffer.
length - the number of bytes to read.
Throws:
java.lang.IllegalArgumentException - if the specified offset is less than zero or greater than or equal to the given byte array size or if the specified number of bytes to get is larger than the remaining space in the specified given byte array.
java.io.EOFException - if the end of file has been reached.
java.io.IOException - if the file has been closed or if any error occurs.
Since:
1.1.0 11/18/2001

readFully

public final void readFully(byte[] buffer)
                     throws java.io.IOException,
                            java.io.EOFException
Reads from the specified file until the specified buffer is filled or end of file is reached. This method blocks until the specified number of bytes is read or an exception is thrown.

Parameters:
buffer - the buffer to read into.
Throws:
java.io.EOFException - if the end of file has been reached.
java.io.IOException - if the file has been closed or if any error occurs.
Since:
1.1.0 11/18/2001

readUTF

public final java.lang.String readUTF()
                               throws java.io.IOException,
                                      java.io.EOFException,
                                      java.io.UTFDataFormatException
Reads the UTF-8 encoded string from the file.

Throws:
java.io.UTFDataFormatException - if an encoding error occurs.
java.io.EOFException - if the end of file has been reached.
java.io.IOException - if the file has been closed or if any error occurs.
Since:
1.1.0 11/18/2001

writeBoolean

public final void writeBoolean(boolean b)
                        throws java.io.IOException
Writes the specified boolean to the file.

Parameters:
b - the boolean to write.
Throws:
java.io.IOException - if the file has been closed or if any error occurs.
Since:
1.1.0 11/18/2001

writeByte

public final void writeByte(int b)
                     throws java.io.IOException
Writes the specified byte to the file.

Parameters:
b - the byte to write.
Throws:
java.io.IOException - if the file has been closed or if any error occurs.
Since:
1.1.0 11/18/2001

writeShort

public final void writeShort(int s)
                      throws java.io.IOException
Writes the specified short to the file.

Parameters:
s - the short to write.
Throws:
java.io.IOException - if the file has been closed or if any error occurs.
Since:
1.1.0 11/18/2001

writeChar

public final void writeChar(int c)
                     throws java.io.IOException
Writes the specified character to the file.

Parameters:
c - the character to write.
Throws:
java.io.IOException - if the file has been closed or if any error occurs.
Since:
1.1.0 11/18/2001

writeInt

public final void writeInt(int i)
                    throws java.io.IOException
Writes the specified integer to the file.

Parameters:
i - the integer to write.
Throws:
java.io.IOException - if the file has been closed or if any error occurs.
Since:
1.1.0 11/18/2001

writeLong

public final void writeLong(long l)
                     throws java.io.IOException
Writes the specified long integer to the file.

Parameters:
l - the long integer to write.
Throws:
java.io.IOException - if the file has been closed or if any error occurs.
Since:
1.1.0 11/18/2001

writeFloat

public final void writeFloat(float f)
                      throws java.io.IOException
Writes the specified floating point to the file.

Parameters:
f - the floating point to write.
Throws:
java.io.IOException - if the file has been closed or if any error occurs.
Since:
1.1.0 11/18/2001

writeDouble

public final void writeDouble(double d)
                       throws java.io.IOException
Writes the specified long floating point to the file.

Parameters:
d - the long floating point to write.
Throws:
java.io.IOException - if the file has been closed or if any error occurs.
Since:
1.1.0 11/18/2001

writeBytes

public final void writeBytes(java.lang.String s)
                      throws java.io.IOException
Writes the specified string as an array of bytes to the file.

Parameters:
s - the string to write.
Throws:
java.io.IOException - if the file has been closed or if any error occurs.
Since:
1.1.0 11/18/2001

writeChars

public final void writeChars(java.lang.String s)
                      throws java.io.IOException
Writes the specified string as an array of characters to the file.

Parameters:
s - the string to write.
Throws:
java.io.IOException - if the file has been closed or if any error occurs.
Since:
1.1.0 11/18/2001

writeUTF

public final void writeUTF(java.lang.String s)
                    throws java.io.IOException,
                           java.io.UTFDataFormatException
Writes the specified string as an UTF-8 encoded string to the file.

Parameters:
s - the string to write.
Throws:
java.io.IOException - if the file has been closed or if any error occurs.
java.io.UTFDataFormatException - if an encoding error occurs.
Since:
1.1.0 11/18/2001

readUnsignedByte

public final int readUnsignedByte()
                           throws java.io.IOException,
                                  java.io.EOFException
Reads the next unsigned byte from the file.

Returns:
the unsigned byte.
Throws:
java.io.EOFException - if the end of file has been reached.
java.io.IOException - if the file has been closed or if any error occurs.

readUnsignedShort

public final int readUnsignedShort()
                            throws java.io.IOException,
                                   java.io.EOFException
Reads the next unsigned short from the file.

Returns:
the unsigned short.
Throws:
java.io.EOFException - if the end of file has been reached.
java.io.IOException - if the file has been closed or if any error occurs.

readUnsignedInt

public final long readUnsignedInt()
                           throws java.io.IOException,
                                  java.io.EOFException
Reads the next unsigned integer from the file.

Returns:
the unsigned integer.
Throws:
java.io.EOFException - if the end of file has been reached.
java.io.IOException - if the file has been closed or if any error occurs.

readObject

public final java.lang.Object readObject()
                                  throws java.lang.ClassNotFoundException,
                                         java.io.IOException,
                                         java.io.EOFException
Reads the next Object from the file.

Returns:
the Object.
Throws:
java.lang.ClassNotFoundException - is the class of the serialized object was not found.
java.io.EOFException - if the end of file has been reached.
java.io.IOException - if the file has been closed or if any error occurs.

writeObject

public final void writeObject(java.lang.Object o)
                       throws java.io.IOException
Writes the specified Object to the file.

Parameters:
o - the object to write.
Throws:
java.io.IOException - if the file has been closed or if any error occurs.
Since:
1.1.0 11/18/2001

writeCString

public final void writeCString(java.lang.String s)
                        throws java.io.IOException
Writes the specified string as C string to the file. Writes the specified string as a byte array and adds a terminating 0x00.

Parameters:
s - the string to write.
Throws:
java.io.IOException - if the file has been closed or if any error occurs.
Since:
1.1.0 11/18/2001

readCString

public final java.lang.String readCString()
                                   throws java.io.IOException,
                                          java.io.EOFException
Reads the next C string from the file. Reads a byte at a time until the terminating 0x00.

Returns:
the string.
Throws:
java.io.EOFException - if the end of file has been reached.
java.io.IOException - if the file has been closed or if any error occurs.

seekEOF

public final void seekEOF()
                   throws java.io.IOException
Sets the file pointer to end of file (EOF).

Throws:
java.io.IOException - if any error occurs.
Since:
1.2.0 12/03/2002

Servertec
Foundation Classes
1.3.3 09/04/2005

Copyright © 2001-2005 Servertec. All rights reserved.