Package loci.common

Class FileHandle

java.lang.Object
loci.common.FileHandle
All Implemented Interfaces:
java.io.DataInput, java.io.DataOutput, IRandomAccess

public class FileHandle
extends java.lang.Object
implements IRandomAccess
A wrapper for RandomAccessFile that implements the IRandomAccess interface.
Author:
Curtis Rueden ctrueden at wisc.edu
See Also:
IRandomAccess, RandomAccessFile
  • Field Summary

    Fields 
    Modifier and Type Field Description
    protected java.io.RandomAccessFile raf
    The random access file object backing this FileHandle.
  • Constructor Summary

    Constructors 
    Constructor Description
    FileHandle​(java.io.File file, java.lang.String mode)
    Creates a random access file stream to read from, and optionally to write to, the file specified by the File argument.
    FileHandle​(java.lang.String name, java.lang.String mode)
    Creates a random access file stream to read from, and optionally to write to, a file with the specified name.
  • Method Summary

    Modifier and Type Method Description
    void close()
    Closes this random access stream and releases any system resources associated with the stream.
    boolean exists()
    Returns whether this refers to a valid object
    long getFilePointer()
    Returns the current offset in this stream.
    java.nio.ByteOrder getOrder()
    Returns the current order (endianness) of the stream.
    java.io.RandomAccessFile getRandomAccessFile()  
    long length()
    Returns the length of this stream.
    int read​(byte[] b)
    Reads up to b.length bytes of data from this stream into an array of bytes.
    int read​(byte[] b, int off, int len)
    Reads up to len bytes of data from this stream into an array of bytes.
    int read​(java.nio.ByteBuffer buffer)
    Reads up to buffer.capacity() bytes of data from this stream into a ByteBuffer.
    int read​(java.nio.ByteBuffer buffer, int off, int len)
    Reads up to len bytes of data from this stream into a ByteBuffer.
    boolean readBoolean()  
    byte readByte()  
    char readChar()  
    double readDouble()  
    float readFloat()  
    void readFully​(byte[] b)  
    void readFully​(byte[] b, int off, int len)  
    int readInt()  
    java.lang.String readLine()  
    long readLong()  
    short readShort()  
    int readUnsignedByte()  
    int readUnsignedShort()  
    java.lang.String readUTF()  
    void seek​(long pos)
    Sets the stream pointer offset, measured from the beginning of this stream, at which the next read or write occurs.
    void setOrder​(java.nio.ByteOrder order)
    Sets the byte order (endianness) of the stream.
    int skipBytes​(int n)  
    long skipBytes​(long n)
    A long variant of DataInput.skipBytes(int).
    void write​(byte[] b)  
    void write​(byte[] b, int off, int len)  
    void write​(int b)  
    void write​(java.nio.ByteBuffer buf)
    Writes up to buffer.capacity() bytes of data from the given ByteBuffer to this stream.
    void write​(java.nio.ByteBuffer buf, int off, int len)
    Writes up to len bytes of data from the given ByteBuffer to this stream.
    void writeBoolean​(boolean v)  
    void writeByte​(int v)  
    void writeBytes​(java.lang.String s)  
    void writeChar​(int v)  
    void writeChars​(java.lang.String s)  
    void writeDouble​(double v)  
    void writeFloat​(float v)  
    void writeInt​(int v)  
    void writeLong​(long v)  
    void writeShort​(int v)  
    void writeUTF​(java.lang.String str)  

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • raf

      protected java.io.RandomAccessFile raf
      The random access file object backing this FileHandle.
  • Constructor Details

    • FileHandle

      public FileHandle​(java.io.File file, java.lang.String mode) throws java.io.FileNotFoundException
      Creates a random access file stream to read from, and optionally to write to, the file specified by the File argument.
      Parameters:
      file - a File representing a file on disk
      mode - a valid access mode as defined in RandomAccessFile
      Throws:
      java.io.FileNotFoundException - if the file does not exist
    • FileHandle

      public FileHandle​(java.lang.String name, java.lang.String mode) throws java.io.FileNotFoundException
      Creates a random access file stream to read from, and optionally to write to, a file with the specified name.
      Parameters:
      name - the path to a file on disk
      mode - a valid access mode as defined in RandomAccessFile
      Throws:
      java.io.FileNotFoundException - if the file does not exist
  • Method Details

    • getRandomAccessFile

      public java.io.RandomAccessFile getRandomAccessFile()
      Returns:
      the RandomAccessFile object backing this FileHandle.
    • close

      public void close() throws java.io.IOException
      Description copied from interface: IRandomAccess
      Closes this random access stream and releases any system resources associated with the stream.
      Specified by:
      close in interface IRandomAccess
      Throws:
      java.io.IOException - if the underlying stream(s) could not be closed
    • getFilePointer

      public long getFilePointer() throws java.io.IOException
      Description copied from interface: IRandomAccess
      Returns the current offset in this stream.
      Specified by:
      getFilePointer in interface IRandomAccess
      Returns:
      the current byte offset within the file; expected to be non-negative and less than the value of #length()
      Throws:
      java.io.IOException - if the offset cannot be retrieved
    • exists

      public boolean exists() throws java.io.IOException
      Description copied from interface: IRandomAccess
      Returns whether this refers to a valid object
      Specified by:
      exists in interface IRandomAccess
      Returns:
      true if this refers to a valid object
      Throws:
      java.io.IOException - if unable to determine whether the object is valid
    • length

      public long length() throws java.io.IOException
      Description copied from interface: IRandomAccess
      Returns the length of this stream.
      Specified by:
      length in interface IRandomAccess
      Returns:
      the length in bytes of the stream
      Throws:
      java.io.IOException - if the length cannot be retrieved
    • read

      public int read​(byte[] b) throws java.io.IOException
      Description copied from interface: IRandomAccess
      Reads up to b.length bytes of data from this stream into an array of bytes.
      Specified by:
      read in interface IRandomAccess
      Parameters:
      b - the array to fill from this stream
      Returns:
      the total number of bytes read into the buffer.
      Throws:
      java.io.IOException - if reading is not possible
    • read

      public int read​(byte[] b, int off, int len) throws java.io.IOException
      Description copied from interface: IRandomAccess
      Reads up to len bytes of data from this stream into an array of bytes.
      Specified by:
      read in interface IRandomAccess
      Parameters:
      b - the array to fill from this stream
      off - the offset in b from which to start filling; expected to be non-negative and no greater than b.length - len
      len - the number of bytes to read; expected to be positive and no greater than b.length - offset
      Returns:
      the total number of bytes read into the buffer.
      Throws:
      java.io.IOException - if reading is not possible
    • read

      public int read​(java.nio.ByteBuffer buffer) throws java.io.IOException
      Description copied from interface: IRandomAccess
      Reads up to buffer.capacity() bytes of data from this stream into a ByteBuffer.
      Specified by:
      read in interface IRandomAccess
      Parameters:
      buffer - the ByteBuffer to fill from this stream
      Returns:
      the total number of bytes read into the buffer.
      Throws:
      java.io.IOException - if reading is not possible
    • read

      public int read​(java.nio.ByteBuffer buffer, int off, int len) throws java.io.IOException
      Description copied from interface: IRandomAccess
      Reads up to len bytes of data from this stream into a ByteBuffer.
      Specified by:
      read in interface IRandomAccess
      Parameters:
      buffer - the ByteBuffer to fill from this stream
      off - the offset in b from which to start filling; expected to be non-negative and no greater than buffer.capacity() - len
      len - the number of bytes to read; expected to be positive and no greater than buffer.capacity() - offset
      Returns:
      the total number of bytes read into the buffer.
      Throws:
      java.io.IOException - if reading is not possible
    • seek

      public void seek​(long pos) throws java.io.IOException
      Description copied from interface: IRandomAccess
      Sets the stream pointer offset, measured from the beginning of this stream, at which the next read or write occurs.
      Specified by:
      seek in interface IRandomAccess
      Parameters:
      pos - new byte offset (pointer) in the current stream. Unless otherwise noted, may be larger or smaller than the current pointer, but must be non-negative and less than the value of #length()
      Throws:
      java.io.IOException - if pos is invalid or the seek fails
      See Also:
      IRandomAccess.getFilePointer()
    • write

      public void write​(java.nio.ByteBuffer buf) throws java.io.IOException
      Description copied from interface: IRandomAccess
      Writes up to buffer.capacity() bytes of data from the given ByteBuffer to this stream.
      Specified by:
      write in interface IRandomAccess
      Parameters:
      buf - the ByteBuffer containing bytes to write to this stream
      Throws:
      java.io.IOException - if writing is not possible
    • write

      public void write​(java.nio.ByteBuffer buf, int off, int len) throws java.io.IOException
      Description copied from interface: IRandomAccess
      Writes up to len bytes of data from the given ByteBuffer to this stream.
      Specified by:
      write in interface IRandomAccess
      Parameters:
      buf - the ByteBuffer containing bytes to write to this stream
      off - the offset in b from which to start writing; expected to be non-negative and no greater than buf.capacity() - len
      len - the number of bytes to write; expected to be positive and no greater than buf.capacity() - offset
      Throws:
      java.io.IOException - if writing is not possible
    • readBoolean

      public boolean readBoolean() throws java.io.IOException
      Specified by:
      readBoolean in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readByte

      public byte readByte() throws java.io.IOException
      Specified by:
      readByte in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readChar

      public char readChar() throws java.io.IOException
      Specified by:
      readChar in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readDouble

      public double readDouble() throws java.io.IOException
      Specified by:
      readDouble in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readFloat

      public float readFloat() throws java.io.IOException
      Specified by:
      readFloat in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readFully

      public void readFully​(byte[] b) throws java.io.IOException
      Specified by:
      readFully in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readFully

      public void readFully​(byte[] b, int off, int len) throws java.io.IOException
      Specified by:
      readFully in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readInt

      public int readInt() throws java.io.IOException
      Specified by:
      readInt in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readLine

      public java.lang.String readLine() throws java.io.IOException
      Specified by:
      readLine in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readLong

      public long readLong() throws java.io.IOException
      Specified by:
      readLong in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readShort

      public short readShort() throws java.io.IOException
      Specified by:
      readShort in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readUnsignedByte

      public int readUnsignedByte() throws java.io.IOException
      Specified by:
      readUnsignedByte in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readUnsignedShort

      public int readUnsignedShort() throws java.io.IOException
      Specified by:
      readUnsignedShort in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readUTF

      public java.lang.String readUTF() throws java.io.IOException
      Specified by:
      readUTF in interface java.io.DataInput
      Throws:
      java.io.IOException
    • skipBytes

      public int skipBytes​(int n) throws java.io.IOException
      Specified by:
      skipBytes in interface java.io.DataInput
      Throws:
      java.io.IOException
    • skipBytes

      public long skipBytes​(long n) throws java.io.IOException
      Description copied from interface: IRandomAccess
      A long variant of DataInput.skipBytes(int).
      Specified by:
      skipBytes in interface IRandomAccess
      Parameters:
      n - the number of bytes to skip
      Returns:
      the number of bytes skipped
      Throws:
      java.io.IOException - if the operation failed
    • write

      public void write​(byte[] b) throws java.io.IOException
      Specified by:
      write in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • write

      public void write​(byte[] b, int off, int len) throws java.io.IOException
      Specified by:
      write in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • write

      public void write​(int b) throws java.io.IOException
      Specified by:
      write in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeBoolean

      public void writeBoolean​(boolean v) throws java.io.IOException
      Specified by:
      writeBoolean in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeByte

      public void writeByte​(int v) throws java.io.IOException
      Specified by:
      writeByte in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeBytes

      public void writeBytes​(java.lang.String s) throws java.io.IOException
      Specified by:
      writeBytes in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeChar

      public void writeChar​(int v) throws java.io.IOException
      Specified by:
      writeChar in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeChars

      public void writeChars​(java.lang.String s) throws java.io.IOException
      Specified by:
      writeChars in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeDouble

      public void writeDouble​(double v) throws java.io.IOException
      Specified by:
      writeDouble in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeFloat

      public void writeFloat​(float v) throws java.io.IOException
      Specified by:
      writeFloat in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeInt

      public void writeInt​(int v) throws java.io.IOException
      Specified by:
      writeInt in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeLong

      public void writeLong​(long v) throws java.io.IOException
      Specified by:
      writeLong in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeShort

      public void writeShort​(int v) throws java.io.IOException
      Specified by:
      writeShort in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeUTF

      public void writeUTF​(java.lang.String str) throws java.io.IOException
      Specified by:
      writeUTF in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • getOrder

      public java.nio.ByteOrder getOrder()
      Description copied from interface: IRandomAccess
      Returns the current order (endianness) of the stream.
      Specified by:
      getOrder in interface IRandomAccess
      Returns:
      See above.
    • setOrder

      public void setOrder​(java.nio.ByteOrder order)
      Description copied from interface: IRandomAccess
      Sets the byte order (endianness) of the stream.
      Specified by:
      setOrder in interface IRandomAccess
      Parameters:
      order - Order to set.