ewe.io
Class RandomAccessFile

java.lang.Object
  extended byewe.util.Errorable
      extended byewe.io.BasicStreamObject
          extended byewe.io.StreamObject
              extended byewe.io.RandomStreamObject
                  extended byewe.io.RandomAccessFile
All Implemented Interfaces:
BasicRandomAccessStream, BasicStream, DataInput, DataOutput, FastStream, OverridesClose, RandomAccessStream, Stream, Streamable

public class RandomAccessFile
extends RandomStreamObject
implements OverridesClose, DataOutput, DataInput, FastStream

RandomAccessFile is what you should use for file I/O.


Field Summary
static int CREATE
          Deprecated. READ_WRITE or WRITE_ONLY mode will create the file if it does not exist.
protected  DataInput dis
           
protected  DataOutput dos
           
static int READ_ONLY
          Read-only open mode.
static int READ_WRITE
          Read-write open mode.
static int WRITE_ONLY
          Write-only open mode.
 
Fields inherited from class ewe.io.StreamObject
napIterations, napTime, READWRITE_CLOSED, READWRITE_ERROR, READWRITE_WOULDBLOCK
 
Fields inherited from class ewe.io.BasicStreamObject
closed
 
Fields inherited from class ewe.util.Errorable
error
 
Constructor Summary
protected RandomAccessFile()
           
  RandomAccessFile(File file, int mode)
          Opens a file with the given name and mode.
  RandomAccessFile(File file, String mode)
          Open a new RandomAccessFile in read or read-write mode.
  RandomAccessFile(String file, String mode)
          Open a new RandomAccessFile in read or read-write mode.
 
Method Summary
 boolean canWrite()
          Tests if the RandomAccessStream was opened in read-write mode as opposed to read-ony mode.
 boolean close()
          Closes the file.
 boolean flushStream()
          This is non-blocking.
 long getFilePointer()
           
 int getFilePosition()
          Returns the current file pointer position.
 int getLength()
          Returns the length of the file in bytes.
 boolean isOpen()
          Returns true if the file is open for reading or writing and false otherwise.
 long length()
          Get the current length of the stream.
 int nonBlockingRead(byte[] buf, int start, int count)
          This returns: >0 = Number of bytes read.
 int nonBlockingWrite(byte[] buf, int start, int count)
          This returns: >0 = Number of bytes written.
 int quickRead(byte[] data, int offset, int length, boolean readAll)
          This attempts read using a fast native method.
 void quickWrite(byte[] data, int offset, int length)
          This attempts write using a fast native method.
 boolean readBoolean()
          This method reads a Java boolean value from an input stream.
 byte readByte()
          This method reads a Java byte value from an input stream.
 char readChar()
          This method reads a Java char value from an input stream.
 double readDouble()
          This method reads a Java double value from an input stream.
 float readFloat()
          This method reads a Java float value from an input stream.
 void readFully(byte[] buf)
          This method reads raw bytes into the passed array until the array is full.
 void readFully(byte[] buf, int offset, int len)
          This method reads raw bytes into the passed array buf starting offset bytes into the buffer.
 int readInt()
          This method reads a Java int value from an input stream It operates by reading four bytes from the stream and converting them to a single Java int.
 long readLong()
          This method reads a Java long value from an input stream It operates by reading eight bytes from the stream and converting them to a single Java long.
 short readShort()
          This method reads a signed 16-bit value into a Java in from the stream.
 int readUnsignedByte()
          This method reads 8 unsigned bits into a Java int value from the stream.
 int readUnsignedShort()
          This method reads 16 unsigned bits into a Java int value from the stream.
 String readUTF()
          This method reads a String from an input stream that is encoded in a modified UTF-8 format.
 boolean seek(int pos)
          Sets the file pointer for read and write operations to the given position.
 void seek(long position)
          Set the stream position.
 boolean setStreamLength(long length)
          Set the length of the RandomAccessStream if possible.
 int skipBytes(int numBytes)
          This method skips and discards the specified number of bytes in an input stream.
 void writeBoolean(boolean value)
          This method writes a Java boolean value to an output stream.
 void writeByte(int value)
          This method writes a Java byte value to an output stream.
 void writeBytes(String value)
          This method writes all the bytes in a String out to the stream.
 void writeChar(int value)
          This method writes a Java char value to an output stream.
 void writeChars(String value)
          This method writes all the characters of a String to an output stream as an array of char's.
 void writeDouble(double value)
          This method writes a Java double value to the stream.
 void writeFloat(float value)
          This method writes a Java float value to the stream.
 void writeInt(int value)
          This method writes a Java int value to an output stream.
 void writeLong(long value)
          This method writes a Java long value to an output stream.
 void writeShort(int value)
          This method writes a Java short value to an output stream.
 void writeUTF(String value)
          This method writes a Java String to the stream in a modified UTF-8 format.
 
Methods inherited from class ewe.io.RandomStreamObject
getStreamLength, nonBlockingRead, nonBlockingWrite, seekPosition, setLength, tell, tellPosition, toRandomStream
 
Methods inherited from class ewe.io.StreamObject
closeStream, doRead, doWrite, flush, nap, nap, read, readAByte, readBytes, readBytes, write, writeAByte, writeBytes, writeBytes
 
Methods inherited from class ewe.io.BasicStreamObject
getException, getName, read, read, throwIOException, toInputStream, toOutputStream, toStream, write, write
 
Methods inherited from class ewe.util.Errorable
returnError, returnError, returnError
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, toString
 
Methods inherited from interface ewe.io.DataOutput
write, write, write
 
Methods inherited from interface ewe.io.BasicStream
closeStream
 
Methods inherited from interface ewe.io.Stream
flush, read, read, read, readBytes, readBytes, toInputStream, toOutputStream, write, write, write, writeBytes, writeBytes
 

Field Detail

READ_ONLY

public static final int READ_ONLY
Read-only open mode.

See Also:
Constant Field Values

WRITE_ONLY

public static final int WRITE_ONLY
Write-only open mode.

See Also:
Constant Field Values

READ_WRITE

public static final int READ_WRITE
Read-write open mode.

See Also:
Constant Field Values

CREATE

public static final int CREATE
Deprecated. READ_WRITE or WRITE_ONLY mode will create the file if it does not exist.

Create open mode. Used to create a file if one does not exist.

See Also:
Constant Field Values

dos

protected DataOutput dos

dis

protected DataInput dis
Constructor Detail

RandomAccessFile

public RandomAccessFile(File file,
                        int mode)
Opens a file with the given name and mode. If mode is CREATE, the file will be created if it does not exist.

Parameters:
file - the file to open.
mode - one of DONT_OPEN, READ_ONLY, WRITE_ONLY, READ_WRITE or CREATE

RandomAccessFile

public RandomAccessFile(File file,
                        String mode)
                 throws IOException
Open a new RandomAccessFile in read or read-write mode.

Parameters:
file - the file to open. This must be a disk based File.
mode - must be "r" for Read-only mode, or "rw" for Read-Write mode. The "rw" mode will attempt to create a file if one does not exist.
Throws:
IOException - if the file could not be opened.

RandomAccessFile

public RandomAccessFile(String file,
                        String mode)
                 throws IOException
Open a new RandomAccessFile in read or read-write mode.

Parameters:
file - the name of the file to open. This must refer to a file on a disk.
mode - must be "r" for Read-only mode, or "rw" for Read-Write mode. The "rw" mode will attempt to create a file if one does not exist.
Throws:
IOException - if the file could not be opened.

RandomAccessFile

protected RandomAccessFile()
Method Detail

canWrite

public boolean canWrite()
Description copied from interface: BasicRandomAccessStream
Tests if the RandomAccessStream was opened in read-write mode as opposed to read-ony mode.

Specified by:
canWrite in interface BasicRandomAccessStream

close

public boolean close()
Closes the file. Returns true if the operation is successful and false otherwise.

Specified by:
close in interface BasicStream
Overrides:
close in class StreamObject

isOpen

public boolean isOpen()
Returns true if the file is open for reading or writing and false otherwise. This can be used to check if opening or creating a file was successful.

Specified by:
isOpen in interface BasicStream
Overrides:
isOpen in class StreamObject

getLength

public int getLength()
Returns the length of the file in bytes. If the file is not open 0 will be returned. This keeps up to date with data written to the file. The File.getLength() method does not always keep up to date if the file length is growing while it is open.

Specified by:
getLength in interface RandomAccessStream
Overrides:
getLength in class RandomStreamObject

seek

public boolean seek(int pos)
Sets the file pointer for read and write operations to the given position. The position passed is an absolute position, in bytes, from the beginning of the file. To set the position to just after the end of the file, you can call:
 raf.seek(file.getLength());
 
True is returned if the operation is successful and false otherwise.

Specified by:
seek in interface RandomAccessStream
Overrides:
seek in class RandomStreamObject

getFilePosition

public int getFilePosition()
Returns the current file pointer position.

Specified by:
getFilePosition in interface RandomAccessStream
Overrides:
getFilePosition in class RandomStreamObject

seek

public void seek(long position)
          throws IOException
Description copied from interface: RandomAccessStream
Set the stream position. This is a blocking call.

Specified by:
seek in interface RandomAccessStream
Overrides:
seek in class RandomStreamObject
Throws:
IOException

getFilePointer

public long getFilePointer()
                    throws IOException
Throws:
IOException

length

public long length()
            throws IOException
Description copied from interface: RandomAccessStream
Get the current length of the stream. This is a blocking call.

Specified by:
length in interface RandomAccessStream
Overrides:
length in class RandomStreamObject
Throws:
IOException

nonBlockingRead

public int nonBlockingRead(byte[] buf,
                           int start,
                           int count)
This returns: >0 = Number of bytes read. 0 = No bytes ready to read. -1 = End of file. -2 = IO error.

Specified by:
nonBlockingRead in interface BasicStream
Overrides:
nonBlockingRead in class StreamObject
Parameters:
buf - Destination byte array to hold incoming data.
start - Starting index in buff for incoming data.
count - Maximum number of bytes to read - should never be zero.
Returns:
greater than 0 = Number of bytes read.
0 = No bytes available to read at this time.
-1 = Stream end reached no further bytes to read.
-2 = IO Error.
Note that these values are different to the Stream.readBytes method.

nonBlockingWrite

public int nonBlockingWrite(byte[] buf,
                            int start,
                            int count)
This returns: >0 = Number of bytes written. 0 = No bytes could be written yet. -1 = File Closed -2 = IO error.

Specified by:
nonBlockingWrite in interface BasicStream
Overrides:
nonBlockingWrite in class StreamObject
Parameters:
buf - Source byte array holding data to be written.
start - Starting index in buff for data to be written.
count - Number of bytes to write - should never be zero.
Returns:
greater than 0 = Number of bytes actually written.
0 = No bytes could be written yet - but the stream is still open.
-1 = Stream has been closed - no further writes are possible.
-2 = IO error - something went wrong.
Note that these values are different to the Stream.writeBytes method.

flushStream

public boolean flushStream()
                    throws IOException
Description copied from interface: BasicStream
This is non-blocking. It returns true if the flush completed, false if it did not, or throws an exception on error.

Specified by:
flushStream in interface BasicStream
Throws:
IOException

setStreamLength

public boolean setStreamLength(long length)
                        throws IOException
Description copied from interface: BasicRandomAccessStream
Set the length of the RandomAccessStream if possible. This is non-blocking, it will return false if the operation is still continuing and true if the operation completed. If the operation could not be performed at all an IOException will be thrown.

Make no assumptions about the success of this method. Not all RAS objects will support setStreamLength() or setLength() - not even all Files on all systems will support this. For example, PersonalJava/Java 1.1 does not support this feature and will throw an IOException.

What happens to the file position pointer after this method is called is unpredictable, especially if you are truncating the file. You should ALWAYS reset the file position pointer after calling this method to be where you wish it to be.

Specified by:
setStreamLength in interface BasicRandomAccessStream
Overrides:
setStreamLength in class RandomStreamObject
Throws:
IOException

writeBoolean

public void writeBoolean(boolean value)
                  throws IOException
Description copied from interface: DataOutput
This method writes a Java boolean value to an output stream. If value is true, a byte with the value of 1 will be written, otherwise a byte with the value of 0 will be written. The value written can be read using the readBoolean method in DataInput.

Specified by:
writeBoolean in interface DataOutput
Parameters:
value - The boolean value to write
Throws:
IOException - If an error occurs
See Also:
DataInput.readBoolean()

writeByte

public void writeByte(int value)
               throws IOException
Description copied from interface: DataOutput
This method writes a Java byte value to an output stream. The byte to be written will be in the lowest 8 bits of the int value passed. The value written can be read using the readByte or readUnsignedByte methods in DataInput.

Specified by:
writeByte in interface DataOutput
Parameters:
value - The int value to write
Throws:
IOException - If an error occurs
See Also:
DataInput.readByte(), DataInput.readUnsignedByte()

writeFloat

public void writeFloat(float value)
                throws IOException
Description copied from interface: DataOutput
This method writes a Java float value to the stream. This value is written by first calling the method Float.floatToIntBits to retrieve an int representing the floating point number, then writing this int value to the stream exactly the same as the writeInt() method does. The value written can be read using the readFloat method in DataInput.

Specified by:
writeFloat in interface DataOutput
Parameters:
value - The float value to write
Throws:
IOException - If an error occurs
See Also:
writeInt, DataInput.readFloat(), Float.floatToIntBits(float)

writeLong

public void writeLong(long value)
               throws IOException
Description copied from interface: DataOutput
This method writes a Java long value to an output stream. The 8 bytes of the passed value will be written "big endian". That is, with the high byte written first in the following manner:

byte0 = (byte)((value & 0xFF00000000000000L) >> 56);
byte1 = (byte)((value & 0x00FF000000000000L) >> 48);
byte2 = (byte)((value & 0x0000FF0000000000L) >> 40);
byte3 = (byte)((value & 0x000000FF00000000L) >> 32);
byte4 = (byte)((value & 0x00000000FF000000L) >> 24);
byte5 = (byte)((value & 0x0000000000FF0000L) >> 16);
byte6 = (byte)((value & 0x000000000000FF00L) >> 8);
byte7 = (byte)(value & 0x00000000000000FFL);

The value written can be read using the readLong method in DataInput.

Specified by:
writeLong in interface DataOutput
Parameters:
value - The long value to write
Throws:
IOException - If an error occurs
See Also:
DataInput.readLong()

writeShort

public void writeShort(int value)
                throws IOException
Description copied from interface: DataOutput
This method writes a Java short value to an output stream. The char to be written will be in the lowest 16 bits of the int value passed. These bytes will be written "big endian". That is, with the high byte written first in the following manner:

byte0 = (byte)((value & 0xFF00) >> 8);
byte1 = (byte)(value & 0x00FF);

The value written can be read using the readShort and readUnsignedShort methods in DataInput.

Specified by:
writeShort in interface DataOutput
Parameters:
value - The int value to write as a 16-bit value
Throws:
IOException - If an error occurs
See Also:
DataInput.readShort(), DataInput.readUnsignedShort()

writeDouble

public void writeDouble(double value)
                 throws IOException
Description copied from interface: DataOutput
This method writes a Java double value to the stream. This value is written by first calling the method Double.doubleToLongBits to retrieve an long representing the floating point number, then writing this long value to the stream exactly the same as the writeLong() method does. The value written can be read using the readDouble method in DataInput.

Specified by:
writeDouble in interface DataOutput
Parameters:
value - The double value to write
Throws:
IOException - If any other error occurs
See Also:
writeLong, DataInput.readDouble(), Double.doubleToLongBits(double)

writeBytes

public void writeBytes(String value)
                throws IOException
Description copied from interface: DataOutput
This method writes all the bytes in a String out to the stream. One byte is written for each character in the String. The high eight bits of each character are discarded, thus this method is inappropriate for completely representing Unicode characters.

Specified by:
writeBytes in interface DataOutput
Parameters:
value - The String to write
Throws:
IOException - If an error occurs

writeChars

public void writeChars(String value)
                throws IOException
Description copied from interface: DataOutput
This method writes all the characters of a String to an output stream as an array of char's. Each character is written using the method specified in the writeChar method.

Specified by:
writeChars in interface DataOutput
Parameters:
value - The String to write
Throws:
IOException - If an error occurs
See Also:
writeChar

writeChar

public void writeChar(int value)
               throws IOException
Description copied from interface: DataOutput
This method writes a Java char value to an output stream. The char to be written will be in the lowest 16 bits of the int value passed. These bytes will be written "big endian". That is, with the high byte written first in the following manner:

byte0 = (byte)((value & 0xFF00) >> 8);
byte1 = (byte)(value & 0x00FF);

The value written can be read using the readChar method in DataInput.

Specified by:
writeChar in interface DataOutput
Parameters:
value - The char value to write
Throws:
IOException - If an error occurs
See Also:
DataInput.readChar()

writeUTF

public void writeUTF(String value)
              throws IOException
Description copied from interface: DataOutput
This method writes a Java String to the stream in a modified UTF-8 format. First, two bytes are written to the stream indicating the number of bytes to follow. This is written in the form of a Java short value in the same manner used by the writeShort method. Note that this is the number of bytes in the encoded String not the String length. Next come the encoded characters. Each character in the String is encoded as either one, two or three bytes. For characters in the range of  to , one byte is used. The character value goes into bits 0-7 and bit eight is 0. For characters in the range of ? to F, two bytes are used. Bits 6-10 of the character value are encoded bits 0-4 of the first byte, with the high bytes having a value of "110". Bits 0-5 of the character value are stored in bits 0-5 of the second byte, with the high bits set to "10". This type of encoding is also done for the null character . This eliminates any C style NUL character values in the output. All remaining characters are stored as three bytes. Bits 12-15 of the character value are stored in bits 0-3 of the first byte. The high bits of the first bytes are set to "1110". Bits 6-11 of the character value are stored in bits 0-5 of the second byte. The high bits of the second byte are set to "10". And bits 0-5 of the character value are stored in bits 0-5 of byte three, with the high bits of that byte set to "10". The value written can be read using the readUTF method in DataInput.

Specified by:
writeUTF in interface DataOutput
Parameters:
value - The String to write
Throws:
IOException - If an error occurs
See Also:
DataInput.readUTF()

writeInt

public void writeInt(int value)
              throws IOException
Description copied from interface: DataOutput
This method writes a Java int value to an output stream. The 4 bytes of the passed value will be written "big endian". That is, with the high byte written first in the following manner:

byte0 = (byte)((value & 0xFF000000) >> 24);
byte1 = (byte)((value & 0x00FF0000) >> 16);
byte2 = (byte)((value & 0x0000FF00) >> 8);
byte3 = (byte)(value & 0x000000FF);

The value written can be read using the readInt method in DataInput.

Specified by:
writeInt in interface DataOutput
Parameters:
value - The int value to write
Throws:
IOException - If an error occurs
See Also:
DataInput.readInt()

readBoolean

public boolean readBoolean()
                    throws EOFException,
                           IOException
Description copied from interface: DataInput
This method reads a Java boolean value from an input stream. It does so by reading a single byte of data. If that byte is zero, then the value returned is false. If the byte is non-zero, then the value returned is true.

This method can read a boolean written by an object implementing the writeBoolean() method in the DataOutput interface.

Specified by:
readBoolean in interface DataInput
Returns:
The boolean value read
Throws:
EOFException - If end of file is reached before reading the boolean
IOException - If any other error occurs
See Also:
DataOutput.writeBoolean(boolean)

readByte

public byte readByte()
              throws EOFException,
                     IOException
Description copied from interface: DataInput
This method reads a Java byte value from an input stream. The value is in the range of -128 to 127.

This method can read a byte written by an object implementing the writeByte() method in the DataOutput interface.

Specified by:
readByte in interface DataInput
Returns:
The byte value read
Throws:
EOFException - If end of file is reached before reading the byte
IOException - If any other error occurs
See Also:
DataOutput.writeByte(int)

readUnsignedByte

public int readUnsignedByte()
                     throws EOFException,
                            IOException
Description copied from interface: DataInput
This method reads 8 unsigned bits into a Java int value from the stream. The value returned is in the range of 0 to 255.

This method can read an unsigned byte written by an object implementing the writeByte() method in the DataOutput interface.

Specified by:
readUnsignedByte in interface DataInput
Returns:
The unsigned bytes value read as a Java int.
Throws:
IOException - If any other error occurs
EOFException - If end of file is reached before reading the value
See Also:
DataOutput.writeByte(int)

readChar

public char readChar()
              throws EOFException,
                     IOException
Description copied from interface: DataInput
This method reads a Java char value from an input stream. It operates by reading two bytes from the stream and converting them to a single 16-bit Java char. The two bytes are stored most significant byte first (i.e., "big endian") regardless of the native host byte ordering.

As an example, if byte1 and byte2 represent the first and second byte read from the stream respectively, they will be transformed to a char in the following manner:

(char)((byte1 << 8) + byte2)

This method can read a char written by an object implementing the writeChar() method in the DataOutput interface.

Specified by:
readChar in interface DataInput
Returns:
The char value read
Throws:
IOException - If any other error occurs
EOFException - If end of file is reached before reading the char
See Also:
DataOutput.writeChar(int)

readShort

public short readShort()
                throws EOFException,
                       IOException
Description copied from interface: DataInput
This method reads a signed 16-bit value into a Java in from the stream. It operates by reading two bytes from the stream and converting them to a single 16-bit Java short. The two bytes are stored most significant byte first (i.e., "big endian") regardless of the native host byte ordering.

As an example, if byte1 and byte2 represent the first and second byte read from the stream respectively, they will be transformed to a short in the following manner:

(short)(((byte1 & 0xFF) << 8) + (byte2 & 0xFF))

The value returned is in the range of -32768 to 32767.

This method can read a short written by an object implementing the writeShort() method in the DataOutput interface.

Specified by:
readShort in interface DataInput
Returns:
The short value read
Throws:
EOFException - If end of file is reached before reading the value
IOException - If any other error occurs
See Also:
DataOutput.writeShort(int)

readUnsignedShort

public int readUnsignedShort()
                      throws EOFException,
                             IOException
Description copied from interface: DataInput
This method reads 16 unsigned bits into a Java int value from the stream. It operates by reading two bytes from the stream and converting them to a single Java int. The two bytes are stored most significant byte first (i.e., "big endian") regardless of the native host byte ordering.

As an example, if byte1 and byte2 represent the first and second byte read from the stream respectively, they will be transformed to an int in the following manner:

(int)(((byte1 0xFF) << 8) + (byte2 & 0xFF))

The value returned is in the range of 0 to 65535.

This method can read an unsigned short written by an object implementing the writeShort() method in the DataOutput interface.

Specified by:
readUnsignedShort in interface DataInput
Returns:
The unsigned short value read as a Java int.
Throws:
EOFException - If end of file is reached before reading the value
IOException - If any other error occurs
See Also:
DataOutput.writeShort(int)

readInt

public int readInt()
            throws EOFException,
                   IOException
Description copied from interface: DataInput
This method reads a Java int value from an input stream It operates by reading four bytes from the stream and converting them to a single Java int. The bytes are stored most significant byte first (i.e., "big endian") regardless of the native host byte ordering.

As an example, if byte1 through byte4 represent the first four bytes read from the stream, they will be transformed to an int in the following manner:

(int)(((byte1 & 0xFF) << 24) + ((byte2 & 0xFF) << 16) + ((byte3 & 0xFF)<< 8) + (byte4 & 0xFF)))

The value returned is in the range of -2147483648 to 2147483647.

This method can read an int written by an object implementing the writeInt() method in the DataOutput interface.

Specified by:
readInt in interface DataInput
Returns:
The int value read
Throws:
IOException - If any other error occurs
EOFException - If end of file is reached before reading the int
See Also:
DataOutput.writeInt(int)

readLong

public long readLong()
              throws EOFException,
                     IOException
Description copied from interface: DataInput
This method reads a Java long value from an input stream It operates by reading eight bytes from the stream and converting them to a single Java long. The bytes are stored most significant byte first (i.e., "big endian") regardless of the native host byte ordering.

As an example, if byte1 through byte8 represent the first eight bytes read from the stream, they will be transformed to an long in the following manner:

(long)(((byte1 & 0xFF) << 56) + ((byte2 & 0xFF) << 48) + ((byte3 & 0xFF) << 40) + ((byte4 & 0xFF) << 32) + ((byte5 & 0xFF) << 24) + ((byte6 & 0xFF) << 16) + ((byte7 & 0xFF) << 8) + (byte8 & 0xFF)))

The value returned is in the range of -9223372036854775808 to 9223372036854775807.

This method can read an long written by an object implementing the writeLong() method in the DataOutput interface.

Specified by:
readLong in interface DataInput
Returns:
The long value read
Throws:
IOException - If any other error occurs
EOFException - If end of file is reached before reading the long
See Also:
DataOutput.writeLong(long)

readFloat

public float readFloat()
                throws EOFException,
                       IOException
Description copied from interface: DataInput
This method reads a Java float value from an input stream. It operates by first reading an int value from the stream by calling the readInt() method in this interface, then converts that int to a float using the intBitsToFloat method in the class java.lang.Float.

This method can read a float written by an object implementing the writeFloat() method in the DataOutput interface.

Specified by:
readFloat in interface DataInput
Returns:
The float value read
Throws:
EOFException - If end of file is reached before reading the float
IOException - If any other error occurs
See Also:
DataOutput.writeFloat(float), Float.intBitsToFloat(int)

readDouble

public double readDouble()
                  throws EOFException,
                         IOException
Description copied from interface: DataInput
This method reads a Java double value from an input stream. It operates by first reading a long value from the stream by calling the readLong() method in this interface, then converts that long to a double using the longBitsToDouble method in the class java.lang.Double.

This method can read a double written by an object implementing the writeDouble() method in the DataOutput interface.

Specified by:
readDouble in interface DataInput
Returns:
The double value read
Throws:
IOException - If any other error occurs
EOFException - If end of file is reached before reading the double
See Also:
DataOutput.writeDouble(double), Double.longBitsToDouble(long)

readUTF

public String readUTF()
               throws EOFException,
                      UTFDataFormatException,
                      IOException
Description copied from interface: DataInput
This method reads a String from an input stream that is encoded in a modified UTF-8 format. This format has a leading two byte sequence that contains the remaining number of bytes to read. This two byte sequence is read using the readUnsignedShort() method of this interface. After the number of remaining bytes have been determined, these bytes are read an transformed into char values. These char values are encoded in the stream using either a one, two, or three byte format. The particular format in use can be determined by examining the first byte read.

If the first byte has a high order bit of 0, then that character consists on only one byte. This character value consists of seven bits that are at positions 0 through 6 of the byte. As an example, if byte1 is the byte read from the stream, it would be converted to a char like so:

(char)byte1

If the first byte has 110 as its high order bits, then the character consists of two bytes. The bits that make up the character value are in positions 0 through 4 of the first byte and bit positions 0 through 5 of the second byte. (The second byte should have 10 as its high order bits). These values are in most significant byte first (i.e., "big endian") order.

As an example, if byte1 and byte2 are the first two bytes read respectively, and the high order bits of them match the patterns which indicate a two byte character encoding, then they would be converted to a Java char like so:

(char)(((byte1 & 0x1F) << 6) + (byte2 & 0x3F))

If the first byte has a 1110 as its high order bits, then the character consists of three bytes. The bits that make up the character value are in positions 0 through 3 of the first byte and bit positions 0 through 5 of the other two bytes. (The second and third bytes should have 10 as their high order bits). These values are in most significant byte first (i.e., "big endian") order.

As an example, if byte1, byte2, and byte3 are the three bytes read, and the high order bits of them match the patterns which indicate a three byte character encoding, then they would be converted to a Java char like so: (char)(((byte1 & 0x0F) << 12) + ((byte2 & 0x3F) + (byte3 & 0x3F)) Note that all characters are encoded in the method that requires the fewest number of bytes with the exception of the character with the value of \u0000 which is encoded as two bytes. This is a modification of the UTF standard used to prevent C language style NUL values from appearing in the byte stream.

This method can read data that was written by an object implementing the writeUTF() method in DataOutput.

Specified by:
readUTF in interface DataInput
Throws:
EOFException - If end of file is reached before reading the String
IOException - If any other error occurs
UTFDataFormatException - If the data is not in UTF-8 format
See Also:
DataOutput.writeUTF(java.lang.String)

readFully

public void readFully(byte[] buf)
               throws EOFException,
                      IOException
Description copied from interface: DataInput
This method reads raw bytes into the passed array until the array is full. Note that this method blocks until the data is available and throws an exception if there is not enough data left in the stream to fill the buffer. Note also that zero length buffers are permitted. In this case, the method will return immediately without reading any bytes from the stream.

Specified by:
readFully in interface DataInput
Parameters:
buf - The buffer into which to read the data
Throws:
EOFException - If end of file is reached before filling the buffer
IOException - If any other error occurs

readFully

public void readFully(byte[] buf,
                      int offset,
                      int len)
               throws EOFException,
                      IOException
Description copied from interface: DataInput
This method reads raw bytes into the passed array buf starting offset bytes into the buffer. The number of bytes read will be exactly len. Note that this method blocks until the data is available and throws an exception if there is not enough data left in the stream to read len bytes. Note also that zero length buffers are permitted. In this case, the method will return immediately without reading any bytes from the stream.

Specified by:
readFully in interface DataInput
Parameters:
buf - The buffer into which to read the data
offset - The offset into the buffer to start storing data
len - The number of bytes to read into the buffer
Throws:
IOException - If any other error occurs
EOFException - If end of file is reached before filling the buffer

skipBytes

public int skipBytes(int numBytes)
              throws EOFException,
                     IOException
Description copied from interface: DataInput
This method skips and discards the specified number of bytes in an input stream. Note that this method may skip less than the requested number of bytes. The actual number of bytes skipped is returned. No bytes are skipped if a negative number is passed to this method.

Specified by:
skipBytes in interface DataInput
Parameters:
numBytes - The number of bytes to skip
Returns:
The number of bytes actually skipped, which will always be numBytes
Throws:
IOException - If any other error occurs
EOFException - If end of file is reached before all bytes can be skipped

quickRead

public int quickRead(byte[] data,
                     int offset,
                     int length,
                     boolean readAll)
              throws IOException
This attempts read using a fast native method. Failing that a normal read is done. If a native method is used the method will block the entire VM until the data is read, so you should use this with care.

Specified by:
quickRead in interface FastStream
Parameters:
data - The destination for the data.
offset - The index in the destination for the data.
length - The number of bytes to read.
readAll - if this is true then the method will not return until a full length number of bytes have been read. If the stream ends before this then an IOException is thrown.
Returns:
the number of bytes read or -1 if the stream ended with no bytes read.
Throws:
IOException

quickWrite

public void quickWrite(byte[] data,
                       int offset,
                       int length)
                throws IOException
This attempts write using a fast native method. Failing that a normal write is done. If the native method is used the method will block the entire VM until the data is written, so you should use this with care.

Specified by:
quickWrite in interface FastStream
Parameters:
data - The source of the data bytes.
offset - The index in the source of the data.
length - The number of bytes to write.
Throws:
IOException