loci.formats
Class RandomAccessStream

java.lang.Object
  extended by java.io.InputStream
      extended by loci.formats.RandomAccessStream
All Implemented Interfaces:
Closeable, DataInput

public class RandomAccessStream
extends InputStream
implements DataInput

RandomAccessStream provides methods for "intelligent" reading of files and byte arrays. It also automagically deals with closing and reopening files to prevent an IOException caused by too many open files.

Source code:
Trac, SVN

Author:
Melissa Linkert linkert at wisc.edu

Field Summary
protected  long afp
          The "absolute" file pointer.
protected static int ARRAY
           
protected  byte[] buf
          Starting buffer.
protected  boolean compressed
          Flag indicating this file has been compressed.
protected  DataInputStream dis
           
protected static int DIS
          Indicators for most efficient method of reading.
protected  int ext
          Number of bytes by which to extend the stream.
protected  String file
          The file name.
protected  long fp
          The file pointer within the DIS.
protected  int lastValid
          Number of valid entries in the buffer size array.
protected  long length
          Length of the file.
protected  boolean littleEndian
          Endianness of the stream.
protected  long mark
          Most recent mark.
protected static int MAX_FILES
          Maximum number of open files.
protected static int MAX_HISTORY
          Maximum number of buffer sizes to keep.
protected static int MAX_OVERHEAD
          Maximum size of the buffer used by the DataInputStream.
protected  long nextMark
          Next place to mark.
protected  IRandomAccess raf
           
protected static int RAF
           
 
Constructor Summary
RandomAccessStream(byte[] array)
          Constructs a random access stream around the given byte array.
RandomAccessStream(String file)
          Constructs a hybrid RandomAccessFile/DataInputStream around the given file.
 
Method Summary
 int available()
           
protected  int checkEfficiency(int toRead)
          Determine whether it is more efficient to use the DataInputStream or RandomAccessFile for reading (based on the current file pointers).
 void close()
          Closes the streams.
protected  int determineBuffer()
          Naive heuristic for determining a "good" buffer size for the DIS.
 long getFilePointer()
          Gets the current (absolute) file pointer.
 DataInputStream getInputStream()
          Returns the underlying InputStream.
 boolean isLittleEndian()
          Gets the endianness of the stream.
 long length()
          Gets the number of bytes in the file.
 void mark(int readLimit)
           
 boolean markSupported()
           
 void order(boolean little)
          Sets the endianness of the stream.
 int read()
          Alias for readByte().
 int read(byte[] array)
          Read bytes from the stream into the given array.
 int read(byte[] array, int offset, int n)
          Read n bytes from the stream into the given array at the specified offset.
 boolean readBoolean()
          Read an input byte and return true if the byte is nonzero.
 byte readByte()
          Read one byte and return it.
 char readChar()
          Read an input char.
 double readDouble()
          Read eight bytes and return a double value.
 float readFloat()
          Read four bytes and return a float value.
 void readFully(byte[] array)
          Read bytes from the stream into the given array.
 void readFully(byte[] array, int offset, int n)
          Read n bytes from the stream into the given array at the specified offset.
 int readInt()
          Read four input bytes and return an int value.
 String readLine()
          Read the next line of text from the input stream.
 long readLong()
          Read eight input bytes and return a long value.
 short readShort()
          Read two input bytes and return a short value.
 String readString(int n)
          Read a string of length n.
 int readUnsignedByte()
          Read an input byte and zero extend it appropriately.
 int readUnsignedShort()
          Read two bytes and return an int in the range 0 through 65535.
 String readUTF()
          Read a string that has been encoded using a modified UTF-8 format.
 void reset()
           
 void seek(long pos)
          Seeks to the given offset within the stream.
 void setExtend(int extend)
          Sets the number of bytes by which to extend the stream.
 int skipBytes(int n)
          Skip n bytes within the stream.
 
Methods inherited from class java.io.InputStream
skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_OVERHEAD

protected static final int MAX_OVERHEAD
Maximum size of the buffer used by the DataInputStream.

See Also:
Constant Field Values

MAX_HISTORY

protected static final int MAX_HISTORY
Maximum number of buffer sizes to keep.

See Also:
Constant Field Values

MAX_FILES

protected static final int MAX_FILES
Maximum number of open files.

See Also:
Constant Field Values

DIS

protected static final int DIS
Indicators for most efficient method of reading.

See Also:
Constant Field Values

RAF

protected static final int RAF
See Also:
Constant Field Values

ARRAY

protected static final int ARRAY
See Also:
Constant Field Values

raf

protected IRandomAccess raf

dis

protected DataInputStream dis

length

protected long length
Length of the file.


fp

protected long fp
The file pointer within the DIS.


afp

protected long afp
The "absolute" file pointer.


mark

protected long mark
Most recent mark.


nextMark

protected long nextMark
Next place to mark.


file

protected String file
The file name.


buf

protected byte[] buf
Starting buffer.


littleEndian

protected boolean littleEndian
Endianness of the stream.


ext

protected int ext
Number of bytes by which to extend the stream.


lastValid

protected int lastValid
Number of valid entries in the buffer size array.


compressed

protected boolean compressed
Flag indicating this file has been compressed.

Constructor Detail

RandomAccessStream

public RandomAccessStream(String file)
                   throws IOException
Constructs a hybrid RandomAccessFile/DataInputStream around the given file.

Throws:
IOException

RandomAccessStream

public RandomAccessStream(byte[] array)
                   throws IOException
Constructs a random access stream around the given byte array.

Throws:
IOException
Method Detail

getInputStream

public DataInputStream getInputStream()
Returns the underlying InputStream.


setExtend

public void setExtend(int extend)
Sets the number of bytes by which to extend the stream. This only applies to InputStream API methods.


seek

public void seek(long pos)
          throws IOException
Seeks to the given offset within the stream.

Throws:
IOException

read

public int read()
         throws IOException
Alias for readByte().

Specified by:
read in class InputStream
Throws:
IOException

length

public long length()
            throws IOException
Gets the number of bytes in the file.

Throws:
IOException

getFilePointer

public long getFilePointer()
Gets the current (absolute) file pointer.


close

public void close()
           throws IOException
Closes the streams.

Specified by:
close in interface Closeable
Overrides:
close in class InputStream
Throws:
IOException

order

public void order(boolean little)
Sets the endianness of the stream.


isLittleEndian

public boolean isLittleEndian()
Gets the endianness of the stream.


readBoolean

public boolean readBoolean()
                    throws IOException
Read an input byte and return true if the byte is nonzero.

Specified by:
readBoolean in interface DataInput
Throws:
IOException

readByte

public byte readByte()
              throws IOException
Read one byte and return it.

Specified by:
readByte in interface DataInput
Throws:
IOException

readChar

public char readChar()
              throws IOException
Read an input char.

Specified by:
readChar in interface DataInput
Throws:
IOException

readDouble

public double readDouble()
                  throws IOException
Read eight bytes and return a double value.

Specified by:
readDouble in interface DataInput
Throws:
IOException

readFloat

public float readFloat()
                throws IOException
Read four bytes and return a float value.

Specified by:
readFloat in interface DataInput
Throws:
IOException

readInt

public int readInt()
            throws IOException
Read four input bytes and return an int value.

Specified by:
readInt in interface DataInput
Throws:
IOException

readLine

public String readLine()
                throws IOException
Read the next line of text from the input stream.

Specified by:
readLine in interface DataInput
Throws:
IOException

readString

public String readString(int n)
                  throws IOException
Read a string of length n.

Throws:
IOException

readLong

public long readLong()
              throws IOException
Read eight input bytes and return a long value.

Specified by:
readLong in interface DataInput
Throws:
IOException

readShort

public short readShort()
                throws IOException
Read two input bytes and return a short value.

Specified by:
readShort in interface DataInput
Throws:
IOException

readUnsignedByte

public int readUnsignedByte()
                     throws IOException
Read an input byte and zero extend it appropriately.

Specified by:
readUnsignedByte in interface DataInput
Throws:
IOException

readUnsignedShort

public int readUnsignedShort()
                      throws IOException
Read two bytes and return an int in the range 0 through 65535.

Specified by:
readUnsignedShort in interface DataInput
Throws:
IOException

readUTF

public String readUTF()
               throws IOException
Read a string that has been encoded using a modified UTF-8 format.

Specified by:
readUTF in interface DataInput
Throws:
IOException

skipBytes

public int skipBytes(int n)
              throws IOException
Skip n bytes within the stream.

Specified by:
skipBytes in interface DataInput
Throws:
IOException

read

public int read(byte[] array)
         throws IOException
Read bytes from the stream into the given array.

Overrides:
read in class InputStream
Throws:
IOException

read

public int read(byte[] array,
                int offset,
                int n)
         throws IOException
Read n bytes from the stream into the given array at the specified offset.

Overrides:
read in class InputStream
Throws:
IOException

readFully

public void readFully(byte[] array)
               throws IOException
Read bytes from the stream into the given array.

Specified by:
readFully in interface DataInput
Throws:
IOException

readFully

public void readFully(byte[] array,
                      int offset,
                      int n)
               throws IOException
Read n bytes from the stream into the given array at the specified offset.

Specified by:
readFully in interface DataInput
Throws:
IOException

available

public int available()
              throws IOException
Overrides:
available in class InputStream
Throws:
IOException

mark

public void mark(int readLimit)
Overrides:
mark in class InputStream

markSupported

public boolean markSupported()
Overrides:
markSupported in class InputStream

reset

public void reset()
           throws IOException
Overrides:
reset in class InputStream
Throws:
IOException

determineBuffer

protected int determineBuffer()
Naive heuristic for determining a "good" buffer size for the DIS.


checkEfficiency

protected int checkEfficiency(int toRead)
                       throws IOException
Determine whether it is more efficient to use the DataInputStream or RandomAccessFile for reading (based on the current file pointers). Returns 0 if we should use the DataInputStream, 1 if we should use the RandomAccessFile, and 2 for a direct array access.

Throws:
IOException