|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectewe.util.Errorable
ewe.io.BasicStreamObject
ewe.io.StreamObject
ewe.io.RandomStreamObject
ewe.io.RandomAccessFile
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 |
public static final int READ_ONLY
public static final int WRITE_ONLY
public static final int READ_WRITE
public static final int CREATE
protected DataOutput dos
protected DataInput dis
| Constructor Detail |
public RandomAccessFile(File file,
int mode)
file - the file to open.mode - one of DONT_OPEN, READ_ONLY, WRITE_ONLY, READ_WRITE or CREATE
public RandomAccessFile(File file,
String mode)
throws IOException
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.
IOException - if the file could not be opened.
public RandomAccessFile(String file,
String mode)
throws IOException
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.
IOException - if the file could not be opened.protected RandomAccessFile()
| Method Detail |
public boolean canWrite()
BasicRandomAccessStream
canWrite in interface BasicRandomAccessStreampublic boolean close()
close in interface BasicStreamclose in class StreamObjectpublic boolean isOpen()
isOpen in interface BasicStreamisOpen in class StreamObjectpublic int getLength()
getLength in interface RandomAccessStreamgetLength in class RandomStreamObjectpublic boolean seek(int pos)
raf.seek(file.getLength());True is returned if the operation is successful and false otherwise.
seek in interface RandomAccessStreamseek in class RandomStreamObjectpublic int getFilePosition()
getFilePosition in interface RandomAccessStreamgetFilePosition in class RandomStreamObject
public void seek(long position)
throws IOException
RandomAccessStream
seek in interface RandomAccessStreamseek in class RandomStreamObjectIOException
public long getFilePointer()
throws IOException
IOException
public long length()
throws IOException
RandomAccessStream
length in interface RandomAccessStreamlength in class RandomStreamObjectIOException
public int nonBlockingRead(byte[] buf,
int start,
int count)
nonBlockingRead in interface BasicStreamnonBlockingRead in class StreamObjectbuf - 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.
public int nonBlockingWrite(byte[] buf,
int start,
int count)
nonBlockingWrite in interface BasicStreamnonBlockingWrite in class StreamObjectbuf - 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.
public boolean flushStream()
throws IOException
BasicStream
flushStream in interface BasicStreamIOException
public boolean setStreamLength(long length)
throws IOException
BasicRandomAccessStreamMake 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.
setStreamLength in interface BasicRandomAccessStreamsetStreamLength in class RandomStreamObjectIOException
public void writeBoolean(boolean value)
throws IOException
DataOutputvalue 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.
writeBoolean in interface DataOutputvalue - The boolean value to write
IOException - If an error occursDataInput.readBoolean()
public void writeByte(int value)
throws IOException
DataOutputint value passed.
The value written can be read using the readByte or
readUnsignedByte methods in DataInput.
writeByte in interface DataOutputvalue - The int value to write
IOException - If an error occursDataInput.readByte(),
DataInput.readUnsignedByte()
public void writeFloat(float value)
throws IOException
DataOutputfloat 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.
writeFloat in interface DataOutputvalue - The float value to write
IOException - If an error occurswriteInt,
DataInput.readFloat(),
Float.floatToIntBits(float)
public void writeLong(long value)
throws IOException
DataOutput
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.
writeLong in interface DataOutputvalue - The long value to write
IOException - If an error occursDataInput.readLong()
public void writeShort(int value)
throws IOException
DataOutputint
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.
writeShort in interface DataOutputvalue - The int value to write as a 16-bit value
IOException - If an error occursDataInput.readShort(),
DataInput.readUnsignedShort()
public void writeDouble(double value)
throws IOException
DataOutputdouble 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.
writeDouble in interface DataOutputvalue - The double value to write
IOException - If any other error occurswriteLong,
DataInput.readDouble(),
Double.doubleToLongBits(double)
public void writeBytes(String value)
throws IOException
DataOutputString 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.
writeBytes in interface DataOutputvalue - The String to write
IOException - If an error occurs
public void writeChars(String value)
throws IOException
DataOutputString to an
output stream as an array of char's. Each character
is written using the method specified in the writeChar
method.
writeChars in interface DataOutputvalue - The String to write
IOException - If an error occurswriteChar
public void writeChar(int value)
throws IOException
DataOutputint
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.
writeChar in interface DataOutputvalue - The char value to write
IOException - If an error occursDataInput.readChar()
public void writeUTF(String value)
throws IOException
DataOutputString 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