com.novell.java.io
Class RandomAccess

java.lang.Object
  |
  +--com.novell.java.io.RandomAccess

public class RandomAccess
extends java.lang.Object
implements java.io.DataInput, java.io.DataOutput

Provides random access functionality to various data device objects that implements the DataAccessable interface.

This interface extends java.io.DataInput and java.io.DataOuput, thereby adding the type filtered methods of these interfaces to this interface.

The various constructors open access on (or with) the DataAccessable object passed into the constructors. The DataAccessable object might have container capabilities and support subordinate access, or it may support only access to itself. The constructors with a name parameter (which must be atomic) will attempt to open subordinate access through the given DataAccessable object. The constructors without a name parameter attempt to open access on the given DataAcessable object.

There is also a set of constructors that support provider-based custom parameters. A given implementation of a DataAccessable object might require custom parameters to open the access. For example, a file system provider might require open flags, fork selectors, name space information, size, and so forth, to open the stream. The custom Object parameter allows for the passing of known provider-supplied objects to their DataAccessable implementations, allowing the user full control over the opening of the stream if needed. The various providers must implement reasonable defaults for any custom parameters if the non-custom constructors are used (open flags == READ). See Novell's DataAccessableParameters and NFileInputStream for an example of how a provider might implement this support.

NoSubordinateAccessException will be thrown if the given DataAccessable object does not support subordinate access (subordinate constructors). SubordinateAccessOnlyException will be thrown if the given DataAccessable object does not support access on itself.

See Also:
DataAccessable, NInputStream, NOutputStream, NRandomAccessFile, DataAccessableParameters

Constructor Summary
RandomAccess(DataAccessable accessor)
          Creates an instance of RandomAccess with a DataAccessable parameter.
RandomAccess(DataAccessable accessor, java.lang.Object custom)
          Creates an instance of RandomAccess with the custom parameter.
RandomAccess(java.lang.String name, DataAccessable accessor)
          Creates a subordinate instance of RandomAccess.
RandomAccess(java.lang.String name, DataAccessable accessor, java.lang.Object custom)
          Creates a subordinate instance of RandomAccess with the custom parameter.
 
Method Summary
 void close()
          Closes this random access source and releases any system resources associated with it.
 long getPosition()
          Returns the current offset in bytes from the beginning of this source at which the next read or write will occur.
 long length()
          Returns the length of this file.
 int read()
          Reads a byte of data from this accessor source.
 int read(byte[] b)
          Reads up to b.length bytes of data from this accessor source into an array of bytes.
 int read(byte[] b, int off, int len)
          Reads up to len bytes of data from this accessor source into an array of bytes.
 boolean readBoolean()
          Reads a boolean value from this file.
 byte readByte()
          Reads a signed 8-bit value from this file.
 char readChar()
          Reads a Unicode character in two bytes from this file.
 double readDouble()
          Reads a double from this file.
 float readFloat()
          Reads a float from this file.
 void readFully(byte[] b)
          Reads b.length bytes from this source into the byte array.
 void readFully(byte[] b, int off, int len)
          Reads exactly len bytes from this source into the byte array.
 int readInt()
          Reads a signed 32-bit integer in 4 bytes from this file.
 java.lang.String readLine()
          Reads the next line of text from this file.
 long readLong()
          Reads a signed 64-bit integer in 8 bytes from this file.
 short readShort()
          Reads a signed 16-bit number in 2 bytes from this file.
 int readUnsignedByte()
          Reads and returns an unsigned 8-bit byte from this file.
 int readUnsignedShort()
          Reads an unsigned 16-bit number in two bytes from this file.
 java.lang.String readUTF()
          Reads in a string from this file.
 void setLength(long length)
          Sets the file length.
 void setPosition(long pos)
          Sets the offset from the beginning of this source at which the next read or write will occur.
 int skipBytes(int n)
          Skips exactly n bytes of input.
 void write(byte[] b)
          Writes b.length bytes to this source from the specified byte array starting at offset off.
 void write(byte[] b, int off, int len)
          Writes len bytes to this source from the specified byte array starting at offset off.
 void write(int b)
          Writes the specified byte to this source.
 void writeBoolean(boolean v)
          Writes a boolean to the file as a 1-byte value.
 void writeByte(int v)
          Writes a byte to the file as a 1-byte value.
 void writeBytes(java.lang.String s)
          Writes the specified string to the file as a sequence of bytes.
 void writeChar(int v)
          Writes a char to the file as a 2-byte value, high byte first.
 void writeChars(java.lang.String s)
          Writes the specified string to the file as a sequence of characters.
 void writeDouble(double v)
          Converts the specified double argument to a long using the java.lang.Double.doubleToLongBits(double) method, and then writes that long value to the file as an 8-byte quantity, high byte first.
 void writeFloat(float v)
          Converts the specified float argument to an int using the java.lang.Float.floatToIntBits(float) method, and then writes that int value to the file as a 4-byte quantity, high byte first.
 void writeInt(int v)
          Writes the specified int to the file as four bytes, high byte first.
 void writeLong(long v)
          Writes the specified long to the file as eight bytes, high byte first.
 void writeShort(int v)
          Writes a short to the file as two bytes, high byte first.
 void writeUTF(java.lang.String str)
          Writes the specified string to the file using UTF-8 encoding in a machine-independent manner.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RandomAccess

public RandomAccess(DataAccessable accessor)
             throws java.io.IOException
Creates an instance of RandomAccess with a DataAccessable parameter.

Opens random access to the DataAccessable accessor parameter.

Parameters:
accessor - The DataAccessable object to use.
Throws:
java.io.IOException - If the accessor cannot be opened.
SubordinateAccessOnlyException - If the accessor does not support access to itself, for example a directory.
RandomAccessNotSupportedException - If the accessor does not support random access.

RandomAccess

public RandomAccess(DataAccessable accessor,
                    java.lang.Object custom)
             throws java.io.IOException
Creates an instance of RandomAccess with the custom parameter.

Opens a random access to the DataAccessable accessor parameter, passing the provider-based custom parameter to the accessor for the open.

Parameters:
accessor - The DataAccessable object to use.
custom - Provider-specific custom object containing any needed parameters to open the access.
Throws:
SubordinateAccessOnlyException - If the accessor does not support access to itself, for example a directory.
RandomAccessNotSupportedException - If the accessor does not support random access.
java.lang.IllegalArgumentException - If the custom object is not of the expected type for the given provider.

RandomAccess

public RandomAccess(java.lang.String name,
                    DataAccessable accessor)
             throws java.io.IOException
Creates a subordinate instance of RandomAccess.

Opens a subordinate random access to the DataAccessable accessor parameter.

Parameters:
name - The atomic subordinate access name to open.
accessor - The DataAccessable object to use.
Throws:
java.io.IOException - If the access cannot be opened.
NoSubordinateAccessException - If the accessor does not support opening a subordinate access, for example, a file.
RandomAccessNotSupportedException - If the accessor does not support random access.

RandomAccess

public RandomAccess(java.lang.String name,
                    DataAccessable accessor,
                    java.lang.Object custom)
             throws java.io.IOException
Creates a subordinate instance of RandomAccess with the custom parameter.

Opens a subordinate random access to the DataAccessable accessor parameter, passing the provider-based custom parameter to the accessor for the open.

Parameters:
name - The atomic subordinate access name to open.
accessor - The DataAccessable object to use.
custom - Provider-specific custom object containing any needed parameters to open the access.
Throws:
java.io.IOException - If the access cannot be opened.
NoSubordinateAccessException - If the accessor does not support opening a subordinate access, for example, a file.
RandomAccessNotSupportedException - If the accessor does not support random access.
java.lang.IllegalArgumentException - If the custom object is not of the expected type for the given provider.
Method Detail

read

public int read()
         throws java.io.IOException
Reads a byte of data from this accessor source. This method could block if no input is yet available.
Returns:
The next byte of data, or -1 if the end of the data is reached.
Throws:
java.io.IOException - If an I/O error occurs.

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Reads up to len bytes of data from this accessor source into an array of bytes. This method could block until at least one byte of input is available.
Parameters:
b - The buffer into which the data is read.
off - The start offset of the data to read.
len - The maximum number of bytes read.
Returns:
The total number of bytes read into the buffer, or -1 if there is no more data because the end of the source has been reached.
Throws:
java.io.IOException - If an I/O error occurs.

read

public int read(byte[] b)
         throws java.io.IOException
Reads up to b.length bytes of data from this accessor source into an array of bytes. This method could block until at least one byte of input is available.
Parameters:
b - The buffer into which the data is read.
Returns:
The total number of bytes read into the buffer, or -1 if there is no more data because the end of this source has been reached.
Throws:
java.io.IOException - If an I/O error occurs.

readFully

public void readFully(byte[] b)
               throws java.io.IOException
Reads b.length bytes from this source into the byte array. This method reads repeatedly from the source until all the bytes are read. This method could block until all the bytes are read, the end of the stream is detected, or an exception is thrown.
Specified by:
readFully in interface java.io.DataInput
Parameters:
b - The buffer into which the data is read.
Throws:
java.io.EOFException - If this source reaches the end before reading all the bytes.
java.io.IOException - If an I/O error occurs.

readFully

public void readFully(byte[] b,
                      int off,
                      int len)
               throws java.io.IOException
Reads exactly len bytes from this source into the byte array. This method reads repeatedly from the source until all the bytes are read. This method could block until all the bytes are read, the end of the stream is detected, or an exception is thrown.
Specified by:
readFully in interface java.io.DataInput
Parameters:
b - The buffer into which the data is read.
off - The start offset of the data.
len - The number of bytes to read.
Throws:
java.io.EOFException - This source reaches the end before reading all the bytes.
java.io.IOException - If an I/O error occurs.

write

public void write(int b)
           throws java.io.IOException
Writes the specified byte to this source.
Specified by:
write in interface java.io.DataOutput
Parameters:
b - The byte to be written.
Throws:
java.io.IOException - If an I/O error occurs.

write

public void write(byte[] b)
           throws java.io.IOException
Writes b.length bytes to this source from the specified byte array starting at offset off.
Specified by:
write in interface java.io.DataOutput
Parameters:
b - The bytes of data to write.
Throws:
java.io.IOException - If an I/O error occurs.

write

public void write(byte[] b,
                  int off,
                  int len)
           throws java.io.IOException
Writes len bytes to this source from the specified byte array starting at offset off.
Specified by:
write in interface java.io.DataOutput
Parameters:
b - The bytes of data to write.
off - The start offset in the data.
len - The number of bytes to write.
Throws:
java.io.IOException - If an I/O error occurs.

getPosition

public long getPosition()
                 throws java.io.IOException
Returns the current offset in bytes from the beginning of this source at which the next read or write will occur.
Returns:
The current offset in this source.
Throws:
java.io.IOException - If an I/O error occurs.

setPosition

public void setPosition(long pos)
                 throws java.io.IOException
Sets the offset from the beginning of this source at which the next read or write will occur.
Parameters:
pos - The absolute offset position.
Throws:
java.io.IOException - If an I/O error occurs.

close

public void close()
           throws java.io.IOException
Closes this random access source and releases any system resources associated with it.
Throws:
java.io.IOException - If an I/O error occurs.

length

public long length()
            throws java.io.IOException
Returns the length of this file.
Returns:
The length of this file.
Throws:
java.io.IOException - If an I/O error occurs.

setLength

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

If the file is smaller than the desired length, the file is extended to the desired length. Extra space is filled using a byte value of 0.

If the file is larger than the desired length, the file is truncated at the desired length. If the current position before truncating the file is non-existent after truncation, the current position should be reset to a valid file position.

Parameters:
length - The desired absolute length of the file.
Throws:
java.io.IOException - If an I/O error occurs.

readBoolean

public final boolean readBoolean()
                          throws java.io.IOException
Reads a boolean value from this file. A single byte is read from the file. A value of 0 represents FALSE, and any other value represents TRUE. This method blocks until the byte is read, the end of the stream is detected, or an exception is thrown.
Specified by:
readBoolean in interface java.io.DataInput
Returns:
The boolean value read.
Throws:
java.io.EOFException - If this file has reached the end.
java.io.IOException - If an I/O error occurs.

readByte

public final byte readByte()
                    throws java.io.IOException
Reads a signed 8-bit value from this file. If the byte read is b, where 0 <= b <= 255, then the result is

This method blocks until the byte is read, the end of the stream is detected, or an exception is thrown.

Specified by:
readByte in interface java.io.DataInput
Returns:
The next byte of this file as a signed 8-bit byte.
Throws:
java.io.EOFException - If this file has reached the end.
java.io.IOException - If an I/O error occurs.

readUnsignedByte

public final int readUnsignedByte()
                           throws java.io.IOException
Reads and returns an unsigned 8-bit byte from this file. This method blocks until the byte is read, the end of the stream is detected, or an exception is thrown.
Specified by:
readUnsignedByte in interface java.io.DataInput
Returns:
The next byte of this file, interpreted as an unsigned 8-bit number.
Throws:
java.io.EOFException - If this file has reached the end.
java.io.IOException - If an I/O error occurs.

readShort

public final short readShort()
                      throws java.io.IOException
Reads a signed 16-bit number in 2 bytes from this file. If the two bytes read, in order, are b1 and b2, where each of the two values is between 0 and 255, inclusive, then the result is equal to

This method blocks until the two bytes are read, the end of the stream is detected, or an exception is thrown.

Specified by:
readShort in interface java.io.DataInput
Returns:
The next two bytes of this file, interpreted as a signed 16-bit number.
Throws:
java.io.EOFException - If this file reaches the end before reading the two bytes.
java.io.IOException - If an I/O error occurs.

readUnsignedShort

public final int readUnsignedShort()
                            throws java.io.IOException
Reads an unsigned 16-bit number in two bytes from this file. If the bytes read, in order, are b1 and b2, where 0 <= b1, b2 <= 255, then the result is equal to

This method blocks until the two bytes are read, the end of the stream is detected, or an exception is thrown.

Specified by:
readUnsignedShort in interface java.io.DataInput
Returns:
The next two bytes of this file, interpreted as an unsigned 16-bit integer.
Throws:
java.io.EOFException - If this file reaches the end before reading two bytes.
java.io.IOException - If an I/O error occurs.

readChar

public final char readChar()
                    throws java.io.IOException
Reads a Unicode character in two bytes from this file. If the bytes read, in order, are b1 and b2, where 0 <= b1, b2 <= 255, then the result is equal to

This method blocks until the two bytes are read, the end of the stream is detected, or an exception is thrown.

Specified by:
readChar in interface java.io.DataInput
Returns:
The next two bytes of this file as a Unicode character.
Throws:
java.io.EOFException - If this file reaches the end before reading two bytes.
java.io.IOException - If an I/O error occurs.

readInt

public final int readInt()
                  throws java.io.IOException
Reads a signed 32-bit integer in 4 bytes from this file. If the bytes read, in order, are b1, b2, b3, and b4, where 0 <= b1, b2, b3, b4 <= 255, then the result is equal to

This method blocks until the four bytes are read, the end of the stream is detected, or an exception is thrown.

Specified by:
readInt in interface java.io.DataInput
Returns:
The next four bytes of this file, interpreted as an int.
Throws:
java.io.EOFException - If this file reaches the end before reading four bytes.
java.io.IOException - If an I/O error occurs.

readLong

public final long readLong()
                    throws java.io.IOException
Reads a signed 64-bit integer in 8 bytes from this file. If the bytes read, in order, are b1, b2, b3, b4, b5, b6, b7, and b8, where

then the result is equal to

     ((long)b1 << 56) + ((long)b2 << 48)
     + ((long)b3 << 40) + ((long)b4 << 32)
     + ((long)b5 << 24) + ((long)b6 << 16)
     + ((long)b7 << 8) + b8
 

This method blocks until the eight bytes are read, the end of the stream is detected, or an exception is thrown.

Specified by:
readLong in interface java.io.DataInput
Returns:
The next eight bytes of this file, interpreted as a long.
Throws:
java.io.EOFException - If this file reaches the end before reading eight bytes.
java.io.IOException - If an I/O error occurs.

readFloat

public final float readFloat()
                      throws java.io.IOException
Reads a float from this file. This method reads an int value as if by the readInt() method and then converts that int to a float using the java.lang.Float.intBitsToFloat(int) method.

This method blocks until the four bytes are read, the end of the stream is detected, or an exception is thrown.

Specified by:
readFloat in interface java.io.DataInput
Returns:
The next four bytes of this file, interpreted as a float.
Throws:
java.io.EOFException - If this file reaches the end before reading four bytes.
java.io.IOException - If an I/O error occurs.
See Also:
readInt()

readDouble

public final double readDouble()
                        throws java.io.IOException
Reads a double from this file. This method reads a long value as if by the readLong() method and then converts that long to a double using the java.lang.Double.longBitsToDouble(long) method.

This method blocks until the eight bytes are read, the end of the stream is detected, or an exception is thrown.

Specified by:
readDouble in interface java.io.DataInput
Returns:
The next eight bytes of this file, interpreted as a double.
Throws:
java.io.EOFException - If this source reaches the end before reading eight bytes.
java.io.IOException - If an I/O error occurs.
See Also:
readLong()

readLine

public final java.lang.String readLine()
                                throws java.io.IOException
Reads the next line of text from this file. This method successively reads bytes from the file until it reaches the end of a line of text.

A line of text is terminated by a carriage-return character ('\r'), a newline character ('\n'), a carriage-return character immediately followed by a newline character, or the end of the input stream. The line-terminating character(s), if any, are included as part of the string returned.

This method blocks until a newline character is read, a carriage return and the byte following it are read (to see if it is a newline), the end of the stream is detected, or an exception is thrown.

Specified by:
readLine in interface java.io.DataInput
Returns:
the next line of text from this file.
Throws:
java.io.IOException - if an I/O error occurs.

readUTF

public final java.lang.String readUTF()
                               throws java.io.IOException
Reads in a string from this file. The string has been encoded using a modified UTF-8 format.

The first two bytes are read as if by readUnsignedShort. This value gives the number of following bytes that are in the encoded string, not the length of the resulting string. The following bytes are then interpreted as bytes encoding characters in the UTF-8 format and are converted into characters.

This method blocks until all the bytes are read, the end of the stream is detected, or an exception is thrown.

Specified by:
readUTF in interface java.io.DataInput
Returns:
a Unicode string.
Throws:
java.io.EOFException - if this file reaches the end before reading all the bytes.
java.io.IOException - if an I/O error occurs.
java.io.UTFDataFormatException - if the bytes do not represent valid UTF-8 encoding of a Unicode string.
See Also:
readUnsignedShort()

skipBytes

public int skipBytes(int n)
              throws java.io.IOException
Skips exactly n bytes of input.

This method blocks until all the bytes are skipped, the end of the stream is detected, or an exception is thrown.

Specified by:
skipBytes in interface java.io.DataInput
Parameters:
n - The number of bytes to be skipped.
Returns:
The number of bytes skipped, which is always n.
Throws:
java.io.EOFException - If this file reaches the end before skipping all the bytes.
java.io.IOException - If an I/O error occurs.

writeBoolean

public final void writeBoolean(boolean v)
                        throws java.io.IOException
Writes a boolean to the file as a 1-byte value. The value TRUE is written out as the value(byte)1; the value FALSE> is written out as the value (byte)0.
Specified by:
writeBoolean in interface java.io.DataOutput
Parameters:
v - A boolean value to be written.
Throws:
java.io.IOException - If an I/O error occurs.

writeByte

public final void writeByte(int v)
                     throws java.io.IOException
Writes a byte to the file as a 1-byte value.
Specified by:
writeByte in interface java.io.DataOutput
Parameters:
v - A byte value to be written.
Throws:
java.io.IOException - If an I/O error occurs.

writeShort

public final void writeShort(int v)
                      throws java.io.IOException
Writes a short to the file as two bytes, high byte first.
Specified by:
writeShort in interface java.io.DataOutput
Parameters:
v - A short to be written.
Throws:
java.io.IOException - If an I/O error occurs.

writeChar

public final void writeChar(int v)
                     throws java.io.IOException
Writes a char to the file as a 2-byte value, high byte first.
Specified by:
writeChar in interface java.io.DataOutput
Parameters:
v - A char value to be written.
Throws:
java.io.IOException - If an I/O error occurs.

writeInt

public final void writeInt(int v)
                    throws java.io.IOException
Writes the specified int to the file as four bytes, high byte first.
Specified by:
writeInt in interface java.io.DataOutput
Parameters:
v - An int to be written.
Throws:
java.io.IOException - If an I/O error occurs.

writeLong

public final void writeLong(long v)
                     throws java.io.IOException
Writes the specified long to the file as eight bytes, high byte first.
Specified by:
writeLong in interface java.io.DataOutput
Parameters:
v - A long to be written.
Throws:
java.io.IOException - If an I/O error occurs.

writeFloat

public final void writeFloat(float v)
                      throws java.io.IOException
Converts the specified float argument to an int using the java.lang.Float.floatToIntBits(float) method, and then writes that int value to the file as a 4-byte quantity, high byte first.
Specified by:
writeFloat in interface java.io.DataOutput
Parameters:
v - A float value to be written.
Throws:
java.io.IOException - If an I/O error occurs.

writeDouble

public final void writeDouble(double v)
                       throws java.io.IOException
Converts the specified double argument to a long using the java.lang.Double.doubleToLongBits(double) method, and then writes that long value to the file as an 8-byte quantity, high byte first.
Specified by:
writeDouble in interface java.io.DataOutput
Parameters:
v - A double value to be written.
Throws:
java.io.IOException - If an I/O error occurs.

writeBytes

public final void writeBytes(java.lang.String s)
                      throws java.io.IOException
Writes the specified string to the file as a sequence of bytes. Each character in the string is written out, in sequence, by discarding its high eight bits.
Specified by:
writeBytes in interface java.io.DataOutput
Parameters:
s - A string of bytes to be written.
Throws:
java.io.IOException - If an I/O error occurs.

writeChars

public final void writeChars(java.lang.String s)
                      throws java.io.IOException
Writes the specified string to the file as a sequence of characters. Each character is written to the data output stream as if by the java.io.RandomAccessFile.writeChar(int) method.
Specified by:
writeChars in interface java.io.DataOutput
Parameters:
s - A String value to be written.
Throws:
java.io.IOException - If an I/O error occurs.

writeUTF

public final void writeUTF(java.lang.String str)
                    throws java.io.IOException
Writes the specified string to the file using UTF-8 encoding in a machine-independent manner.

First, two bytes are written to the file as if by the writeShort() method giving the number of bytes to follow. This value is the number of bytes actually written out, not the length of the string. Following the length, each character of the string is output, in sequence, using the UTF-8 encoding for each character.

Specified by:
writeUTF in interface java.io.DataOutput
Parameters:
str - A string to be written.
Throws:
java.io.IOException - If an I/O error occurs.