Class MappedFile

All Implemented Interfaces:
Closeable, DataInput, DataOutput, AutoCloseable, nom.tam.util.ArrayDataInput, nom.tam.util.ArrayDataOutput, nom.tam.util.FitsIO, nom.tam.util.RandomAccess

public class MappedFile extends AbstractArrayDataIO implements nom.tam.util.RandomAccess
Provides mapped access to a data buffer using a single mapped byte buffer, compatible with nom.tam.util classes.

This class implements the nom.tam.util ArrayDataInput, ArrayDataOutput and RandomAccess interfaces in the same way that nom.tam.util.BufferedFile does. Hence it can be used as a drop-in replacement for BufferedFile. Unlike BufferedFile however, it does mapped access to files (using java.nio.Buffer objects). This may be moderately more efficient for sequential access to a file, but is dramatically more efficient if very random access is required. This is because BufferedFile effectively always assumes that you are going to read sequentially, so that accessing a single datum distant from (or before) the last datum accessed always results in filling a whole buffer.

Limitations:

  • Files larger than Integer.MAX_VALUE bytes may not currently be accessed
  • Access to very large files may fail if virtual memory runs out
Since:
30 Aug 2002
Author:
Mark Taylor (Starlink)
  • Constructor Details

    • MappedFile

      public MappedFile(ByteBuffer buf)
      Constructs a MappedFile object from a byte buffer.
      Parameters:
      buf - byte buffer
    • MappedFile

      public MappedFile(String filename) throws IOException
      Constructs a MappedFile object by mapping the whole of an existing file using read-only mode.
      Parameters:
      filename - name of the file to map
      Throws:
      MappedFile.FileTooLongException - if the file is too long to map
      IOException
    • MappedFile

      public MappedFile(String filename, String mode) throws IOException
      Constructs a MappedFile object by mapping the whole of an existing file with a given mode.
      Parameters:
      filename - name of the file to map
      mode - mode
      Throws:
      MappedFile.FileTooLongException - if the file is too long to map
      IOException
    • MappedFile

      public MappedFile(String filename, String mode, long start, int size) throws IOException
      Constructs a MappedFile object by mapping part of an existing file with a given mode.
      Parameters:
      filename - name of the file to map
      mode - mode
      start - offset of region to map
      size - length of region to map
      Throws:
      IOException
  • Method Details

    • seek

      public void seek(long offsetFromStart) throws IOException
      Specified by:
      seek in interface nom.tam.util.RandomAccess
      Throws:
      IOException
    • skip

      public long skip(long offset)
      Specified by:
      skip in interface nom.tam.util.ArrayDataInput
    • getFilePointer

      public long getFilePointer()
      Specified by:
      getFilePointer in interface nom.tam.util.RandomAccess
    • skipBytes

      public int skipBytes(int toSkip)
      Specified by:
      skipBytes in interface DataInput
    • skipAllBytes

      public void skipAllBytes(long toSkip) throws IOException
      Specified by:
      skipAllBytes in interface nom.tam.util.ArrayDataInput
      Throws:
      IOException
    • skipAllBytes

      public void skipAllBytes(int toSkip) throws IOException
      Specified by:
      skipAllBytes in interface nom.tam.util.ArrayDataInput
      Throws:
      IOException
    • markSupported

      public boolean markSupported()
    • mark

      public void mark(int readlimit)
      Specified by:
      mark in interface nom.tam.util.ArrayDataInput
    • reset

      public void reset() throws IOException
      Specified by:
      reset in interface nom.tam.util.ArrayDataInput
      Throws:
      IOException
    • get

      protected byte get() throws IOException
      Description copied from class: AbstractArrayDataIO
      Reads one byte from the current position.
      Specified by:
      get in class AbstractArrayDataIO
      Returns:
      next byte
      Throws:
      IOException
    • get

      protected void get(byte[] buf, int offset, int length) throws IOException
      Description copied from class: AbstractArrayDataIO
      Reads bytes into a buffer from the current position.
      Specified by:
      get in class AbstractArrayDataIO
      Parameters:
      buf - destination buffer
      offset - offset of first byte in buf to be written
      length - maximum number of bytes to be written to buf
      Throws:
      IOException
    • put

      protected void put(byte b) throws IOException
      Description copied from class: AbstractArrayDataIO
      Writes a single byte at the current position.
      Specified by:
      put in class AbstractArrayDataIO
      Parameters:
      b - output byte
      Throws:
      IOException
    • put

      protected void put(byte[] buf, int offset, int length) throws IOException
      Description copied from class: AbstractArrayDataIO
      Writes bytes from a buffer to the current position.
      Specified by:
      put in class AbstractArrayDataIO
      Parameters:
      buf - source buffer
      offset - offset of first byte in buf to be read
      length - number of bytes from buf to be read
      Throws:
      IOException
    • length

      public long length()
      Description copied from class: AbstractArrayDataIO
      Returns the size of this buffer. May be -1 if not known/unlimited.
      Specified by:
      length in class AbstractArrayDataIO
      Returns:
      length or -1
    • remaining

      protected long remaining()
      Description copied from class: AbstractArrayDataIO
      Returns the number of bytes remaining between the current position and the end of the file. If there is no end to the file, it is permissible to return Long.MAX_VALUE;
      Specified by:
      remaining in class AbstractArrayDataIO
      Returns:
      number of bytes left in file
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface nom.tam.util.FitsIO
    • flush

      public void flush()
      Specified by:
      flush in interface nom.tam.util.ArrayDataOutput