org.ka2ddo.yaac.io
Class MultiBufferedRandomAccessFile

java.lang.Object
  extended by org.ka2ddo.yaac.io.MultiBufferedRandomAccessFile
All Implemented Interfaces:
java.io.Closeable, java.io.DataInput

public final class MultiBufferedRandomAccessFile
extends java.lang.Object
implements java.io.DataInput, java.io.Closeable

This class provides a cached, buffered variant of RandomAccessFile, so that sequential reads from a starting point can be done with minimal JNI calls and actual kernel calls. It also supports multiple buffers for Ways with "fixed" vertices that are out of sequence with the other vertices.


Constructor Summary
MultiBufferedRandomAccessFile(java.io.File file, int bufSize, int maxBuffers)
          Create a read-only random access file with the specified maximum number of buffers of the specified size.
 
Method Summary
 void clearCache()
          Clear the cache buffers.
 void close()
          Closes this file and releases any system resources associated with the buffering.
 int read()
          Reads the next byte in the file at the current seek position.
 boolean readBoolean()
          See the general contract of the readBoolean method of DataInput.
 byte readByte()
          See the general contract of the readByte method of DataInput.
 char readChar()
          See the general contract of the readChar method of DataInput.
 double readDouble()
          See the general contract of the readDouble method of DataInput.
 float readFloat()
          See the general contract of the readFloat method of DataInput.
 void readFully(byte[] b)
          See the general contract of the readFully method of DataInput.
 void readFully(byte[] b, int off, int len)
          See the general contract of the readFully method of DataInput.
 int readInt()
          See the general contract of the readInt method of DataInput.
 java.lang.String readLine()
          See the general contract of the readLine method of DataInput.
 long readLong()
          See the general contract of the readLong method of DataInput.
 int readNextInt()
          This is similar to readInt(), but the caller must guarantee that no seek() occurred between the last call to readInt() or readNextInt() and this one.
 short readShort()
          See the general contract of the readShort method of DataInput.
 int readUnsignedByte()
          See the general contract of the readUnsignedByte method of DataInput.
 int readUnsignedShort()
          See the general contract of the readUnsignedShort method of DataInput.
 java.lang.String readUTF()
          See the general contract of the readUTF method of DataInput.
 void resetStatistics()
          Reset the statistics counters that are reported by the toString() method.
 void seek(long seekPos)
          Sets the file-pointer offset, measured from the beginning of this file, at which the next read occurs.
 int skipBytes(int n)
          See the general contract of the skipBytes method of DataInput.
 java.lang.String toString()
          Returns a string representation of the object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MultiBufferedRandomAccessFile

public MultiBufferedRandomAccessFile(java.io.File file,
                                     int bufSize,
                                     int maxBuffers)
                              throws java.io.IOException
Create a read-only random access file with the specified maximum number of buffers of the specified size.

Parameters:
file - File pointing at the disk file to read
bufSize - size of each buffer in bytes (must be a power of 2)
maxBuffers - maximum number of buffers to use
Throws:
java.io.IOException - if file cannot be opened for read-only access
Method Detail

read

public int read()
         throws java.io.IOException
Reads the next byte in the file at the current seek position.

Returns:
the next byte of data, or -1 if the end of the stream is reached.
Throws:
java.io.IOException - if this input stream has been closed by invoking its close() method, or an I/O error occurs.
See Also:
seek(long)

readBoolean

public boolean readBoolean()
                    throws java.io.IOException
See the general contract of the readBoolean method of DataInput.

Bytes for this operation are read from the contained file.

Specified by:
readBoolean in interface java.io.DataInput
Returns:
the boolean value read.
Throws:
java.io.EOFException - if this input stream has reached the end.
java.io.IOException - the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs.

readByte

public byte readByte()
              throws java.io.IOException
See the general contract of the readByte method of DataInput.

Bytes for this operation are read from the contained file.

Specified by:
readByte in interface java.io.DataInput
Returns:
the next byte of this input stream as a signed 8-bit byte.
Throws:
java.io.EOFException - if this input stream has reached the end.
java.io.IOException - the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs.

readChar

public char readChar()
              throws java.io.IOException
See the general contract of the readChar method of DataInput.

Bytes for this operation are read from the contained input stream.

Specified by:
readChar in interface java.io.DataInput
Returns:
the next two bytes of this input stream, interpreted as a char.
Throws:
java.io.EOFException - if this input stream reaches the end before reading two bytes.
java.io.IOException - the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs.

readDouble

public double readDouble()
                  throws java.io.IOException
See the general contract of the readDouble method of DataInput.

Bytes for this operation are read from the contained file.

Specified by:
readDouble in interface java.io.DataInput
Returns:
the next eight bytes of this input stream, interpreted as a double.
Throws:
java.io.EOFException - if this input stream reaches the end before reading eight bytes.
java.io.IOException - the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs.
See Also:
DataInputStream.readLong(), Double.longBitsToDouble(long)

readFloat

public float readFloat()
                throws java.io.IOException
See the general contract of the readFloat method of DataInput.

Bytes for this operation are read from the contained file.

Specified by:
readFloat in interface java.io.DataInput
Returns:
the next four bytes of this input stream, interpreted as a float.
Throws:
java.io.EOFException - if this input stream reaches the end before reading four bytes.
java.io.IOException - the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs.
See Also:
DataInputStream.readInt(), Float.intBitsToFloat(int)

readFully

public void readFully(byte[] b)
               throws java.io.IOException
See the general contract of the readFully method of DataInput.

Bytes for this operation are read from the contained file.

Specified by:
readFully in interface java.io.DataInput
Parameters:
b - the buffer into which the data is read.
Throws:
java.io.EOFException - if this input stream reaches the end before reading all the bytes.
java.io.IOException - the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs.

readFully

public void readFully(byte[] b,
                      int off,
                      int len)
               throws java.io.IOException
See the general contract of the readFully method of DataInput.

Bytes for this operation are read from the contained file.

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 - if this input stream reaches the end before reading all the bytes.
java.io.IOException - the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs.

readInt

public int readInt()
            throws java.io.IOException
See the general contract of the readInt method of DataInput.

Bytes for this operation are read from the contained file.

Specified by:
readInt in interface java.io.DataInput
Returns:
the next four bytes of this input stream, interpreted as an int.
Throws:
java.io.EOFException - if this input stream reaches the end before reading four bytes.
java.io.IOException - the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs.

readLine

public java.lang.String readLine()
                          throws java.io.IOException
See the general contract of the readLine method of DataInput.

Bytes for this operation are read from the contained file. Note that this method only supports extended 8-bit ASCII; it does not support UTF-8 or other locale-specific character sets.

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

readLong

public long readLong()
              throws java.io.IOException
See the general contract of the readLong method of DataInput.

Bytes for this operation are read from the contained file.

Specified by:
readLong in interface java.io.DataInput
Returns:
the next eight bytes of this input stream, interpreted as a long.
Throws:
java.io.EOFException - if this input stream reaches the end before reading eight bytes.
java.io.IOException - the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs.

readShort

public short readShort()
                throws java.io.IOException
See the general contract of the readShort method of DataInput.

Bytes for this operation are read from the contained file.

Specified by:
readShort in interface java.io.DataInput
Returns:
the next two bytes of this input stream, interpreted as a signed 16-bit number.
Throws:
java.io.EOFException - if this input stream reaches the end before reading two bytes.
java.io.IOException - the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs.

readUnsignedByte

public int readUnsignedByte()
                     throws java.io.IOException
See the general contract of the readUnsignedByte method of DataInput.

Bytes for this operation are read from the contained file.

Specified by:
readUnsignedByte in interface java.io.DataInput
Returns:
the next byte of this input stream, interpreted as an unsigned 8-bit number.
Throws:
java.io.EOFException - if this input stream has reached the end.
java.io.IOException - the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs.

readUnsignedShort

public int readUnsignedShort()
                      throws java.io.IOException
See the general contract of the readUnsignedShort method of DataInput.

Bytes for this operation are read from the contained file.

Specified by:
readUnsignedShort in interface java.io.DataInput
Returns:
the next two bytes of this input stream, interpreted as an unsigned 16-bit integer.
Throws:
java.io.EOFException - if this input stream reaches the end before reading two bytes.
java.io.IOException - the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs.

readUTF

public java.lang.String readUTF()
                         throws java.io.IOException
See the general contract of the readUTF method of DataInput.

Bytes for this operation are read from the contained file.

Specified by:
readUTF in interface java.io.DataInput
Returns:
a Unicode string.
Throws:
java.io.EOFException - if this input stream reaches the end before reading all the bytes.
java.io.IOException - the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs.
java.io.UTFDataFormatException - if the bytes do not represent a valid modified UTF-8 encoding of a string.

skipBytes

public int skipBytes(int n)
              throws java.io.IOException
See the general contract of the skipBytes method of DataInput.

Bytes for this operation are read from the contained file.

Specified by:
skipBytes in interface java.io.DataInput
Parameters:
n - the number of bytes to be skipped.
Returns:
the actual number of bytes skipped.
Throws:
java.io.IOException - if the contained input stream does not support seek, or the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs.

seek

public void seek(long seekPos)
Sets the file-pointer offset, measured from the beginning of this file, at which the next read occurs. The offset may be set beyond the end of the file. Setting the offset beyond the end of the file does not change the file length.

Parameters:
seekPos - the offset position, measured in bytes from the beginning of the file, at which to set the file pointer.

close

public void close()
           throws java.io.IOException
Closes this file and releases any system resources associated with the buffering. Once the file has been closed, further read(), available(), or skipBytes() invocations will throw an IOException. Closing a previously closed stream has no effect.

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

toString

public java.lang.String toString()
Returns a string representation of the object.

Overrides:
toString in class java.lang.Object
Returns:
a string representation of the object.

resetStatistics

public void resetStatistics()
Reset the statistics counters that are reported by the toString() method.

See Also:
toString()

readNextInt

public int readNextInt()
                throws java.io.IOException
This is similar to readInt(), but the caller must guarantee that no seek() occurred between the last call to readInt() or readNextInt() and this one.

Returns:
the next four bytes of this input stream, interpreted as an int.
Throws:
java.io.EOFException - if this input stream reaches the end before reading four bytes.
java.io.IOException - the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs.
See Also:
readInt()

clearCache

public void clearCache()
Clear the cache buffers. Useful if the file is changed through another file descriptor such that this caching RandomAccessFile's caches might be stale.