Package nom.tam.util

Class FitsInputStream

All Implemented Interfaces:
Closeable, DataInput, AutoCloseable, ArrayDataInput, FitsIO, InputReader
Direct Known Subclasses:
BufferedDataInputStream

public class FitsInputStream extends ArrayInputStream implements ArrayDataInput
For reading FITS files through an InputStream.

Testing and timing routines are provided in the nom.tam.util.test.BufferedFileTester class.

Prior versions under the old BufferedDataInputStream:

  • Version 1.1 -- October 12, 2000: Fixed handling of EOF to return partially read arrays when EOF is detected
  • Version 1.2 -- July 20, 2009: Added handling of very large Object arrays. Additional work is required to handle very large arrays generally.

Version 2.0 -- October 30, 2021: Completely overhauled, with new name and hierarchy. Performance is 2-4 times better than before (Attila Kovacs)

See Also:
  • Constructor Details

    • FitsInputStream

      public FitsInputStream(InputStream i, int bufLength)
      Create a BufferedInputStream based on a input stream with a specified buffer size.
      Parameters:
      i - the input stream to use for reading.
      bufLength - the buffer length to use.
    • FitsInputStream

      public FitsInputStream(InputStream i)
      Create a BufferedInputStream based on an input stream.
      Parameters:
      i - the input stream to use for reading.
  • Method Details

    • read

      public int read() throws IOException
      Description copied from interface: InputReader
      Reads a byte. See the general contract of FilterInputStream.read().
      Specified by:
      read in interface InputReader
      Overrides:
      read in class BufferedInputStream
      Returns:
      the (unsigned) byte value or -1 if there is nothing left to read.
      Throws:
      IOException - if there was an underlying IO error
      See Also:
    • read

      public int read(byte[] b, int from, int len) throws IOException
      Description copied from interface: InputReader
      Reads up to the specified number of bytes into a buffer. See the general contract of DataInputStream.read(byte[], int, int).
      Specified by:
      read in interface InputReader
      Overrides:
      read in class BufferedInputStream
      Parameters:
      b - the buffer
      from - the starting buffer index
      len - the number of bytes to read.
      Returns:
      the number of bytes actually read, or -1 if there is nothing left to read.
      Throws:
      IOException - if there was an underlying IO error
      See Also:
    • nextChecksum

      public final long nextChecksum()
      (for internal use) Returns the aggregated checksum for this stream since the last call to this method, or else since instantiation. Checksums for a block of data thus may be obtained by calling this method both before and after reading the data block -- the first call resets the checksum and the block checksum is returned on the second call. The checksummed block must be a multiple of 2880 bytes (the FITS block size) for the result to be valid.
      Returns:
      the aggregated checksum since the last call to this method, or else since instantiation, on an integer number of FITS blocks read in the meantime.
      Since:
      1.18.1
    • readFully

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

      public void readFully(byte[] b, int off, int len) throws IOException
      Specified by:
      readFully in interface ArrayDataInput
      Specified by:
      readFully in interface DataInput
      Throws:
      IOException
    • read

      public int read(boolean[] b, int start, int length) throws IOException
      Description copied from interface: ArrayDataInput
      Read a segment of an array of boolean's.
      Specified by:
      read in interface ArrayDataInput
      Parameters:
      b - array of boolean's.
      start - start index in the array
      length - number of array elements to read
      Returns:
      number of bytes read.
      Throws:
      EOFException - if already at the end of file.
      IOException - if one of the underlying read operations failed
    • read

      public int read(Boolean[] b, int start, int length) throws IOException
      Description copied from interface: ArrayDataInput
      Reads into an array of booleans, possibly including legal null values. The method has a default implementation, calls DataInput.readBoolean() element by element. Classes that implement this interface might want to replace that with a more efficient block read implementation and/or to add the desired translation for null values.
      Specified by:
      read in interface ArrayDataInput
      Parameters:
      b - array of boolean's.
      start - start index in the array
      length - number of array elements to read
      Returns:
      number of bytes read.
      Throws:
      EOFException - if already at the end of file.
      IOException - if one of the underlying read operations failed
    • read

      public int read(char[] c, int start, int length) throws IOException
      Description copied from interface: ArrayDataInput
      Read a segment of an array of char's.
      Specified by:
      read in interface ArrayDataInput
      Parameters:
      c - array of char's.
      start - start index in the array
      length - number of array elements to read
      Returns:
      number of bytes read.
      Throws:
      EOFException - if already at the end of file.
      IOException - if one of the underlying read operations failed
    • read

      public int read(short[] s, int start, int length) throws IOException
      Description copied from interface: ArrayDataInput
      Read a segment of an array of short's.
      Specified by:
      read in interface ArrayDataInput
      Parameters:
      s - array of short's.
      start - start index in the array
      length - number of array elements to read
      Returns:
      number of bytes read.
      Throws:
      EOFException - if already at the end of file.
      IOException - if one of the underlying read operations failed
    • read

      public int read(int[] i, int start, int length) throws IOException
      Description copied from interface: ArrayDataInput
      Read a segment of an array of int's.
      Specified by:
      read in interface ArrayDataInput
      Parameters:
      i - array of int's.
      start - start index in the array
      length - number of array elements to read
      Returns:
      number of bytes read.
      Throws:
      EOFException - if already at the end of file.
      IOException - if one of the underlying read operations failed
    • read

      public int read(long[] l, int start, int length) throws IOException
      Description copied from interface: ArrayDataInput
      Read a segment of an array of long's.
      Specified by:
      read in interface ArrayDataInput
      Parameters:
      l - array of long's.
      start - start index in the array
      length - number of array elements to read
      Returns:
      number of bytes read.
      Throws:
      EOFException - if already at the end of file.
      IOException - if one of the underlying read operations failed
    • read

      public int read(float[] f, int start, int length) throws IOException
      Description copied from interface: ArrayDataInput
      Read a segment of an array of float's.
      Specified by:
      read in interface ArrayDataInput
      Parameters:
      f - array of float's.
      start - start index in the array
      length - number of array elements to read
      Returns:
      number of bytes read.
      Throws:
      EOFException - if already at the end of file.
      IOException - if one of the underlying read operations failed
    • read

      public int read(double[] d, int start, int length) throws IOException
      Description copied from interface: ArrayDataInput
      Read a segment of an array of double's.
      Specified by:
      read in interface ArrayDataInput
      Parameters:
      d - array of double's.
      start - start index in the array
      length - number of array elements to read
      Returns:
      number of bytes read, or -1 if at the end of file/stream
      Throws:
      EOFException - if already at the end of file.
      IOException - if one of the underlying read operations failed
    • readPrimitiveArray

      @Deprecated public final int readPrimitiveArray(Object o) throws IOException
      Deprecated.
      This routine provides efficient reading of arrays of any primitive type. It is an error to invoke this method with an object that is not an array of some primitive type. Note that there is no corresponding capability to writePrimitiveArray in BufferedDataOutputStream to read in an array of Strings.
      Parameters:
      o - The object to be read. It must be an array of a primitive type, or an array of Object's.
      Returns:
      number of bytes read.
      Throws:
      IOException - if the underlying read operation fails
    • skip

      public long skip(long n) throws IOException
      Description copied from interface: ArrayDataInput
      Skip the number of bytes. This differs from the skip method in that it will throw an EOF if a forward skip cannot be fully accomplished... (However that isn't supposed to happen with a random access file, so there is probably no operational difference).
      Specified by:
      skip in interface ArrayDataInput
      Overrides:
      skip in class BufferedInputStream
      Parameters:
      n - the number of bytes to skip. Negative arguments are generally allowed, and subclass implementations may support it or else return 0 when a negative distance is specified.
      Returns:
      the number of bytes actually skipped
      Throws:
      EOFException - if the end (or beginning) of the stream was reached before skipping the required number of bytes. This does not happen typically with forward skips on random access files, where positioning beyond the EOF is generally allowed for writing.
      IOException - if the underlying stream failed
      See Also:
    • skipBytes

      public int skipBytes(int n) throws IOException
      Specified by:
      skipBytes in interface DataInput
      Throws:
      IOException
    • skipAllBytes

      public void skipAllBytes(long toSkip) throws EOFException, IOException
      Description copied from interface: ArrayDataInput
      Skips a number of bytes from the input. This differs from the ArrayDataInput.skip(long) method in that it will throw an EOF if the skip cannot be fully accomplished as requested...
      Specified by:
      skipAllBytes in interface ArrayDataInput
      Parameters:
      toSkip - the number of bytes to skip forward. Subclass implementations may support negative valued arguments for a backward skip also.
      Throws:
      EOFException - if the end (or beginning) of the stream was reached before skipping the required number of bytes. This does not happen typically with forward skips on random access files, where positioning beyond the EOF is generally allowed for writing.
      IOException - if there was an underlying IO failure.
      See Also:
    • readBoolean

      public boolean readBoolean() throws IOException
      Specified by:
      readBoolean in interface DataInput
      Throws:
      IOException
    • readUnsignedByte

      public int readUnsignedByte() throws IOException
      Specified by:
      readUnsignedByte in interface DataInput
      Throws:
      IOException
    • readByte

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

      public char readChar() throws IOException
      Specified by:
      readChar in interface DataInput
      Throws:
      IOException
    • readUnsignedShort

      public int readUnsignedShort() throws IOException
      Specified by:
      readUnsignedShort in interface DataInput
      Throws:
      IOException
    • readShort

      public short readShort() throws IOException
      Specified by:
      readShort in interface DataInput
      Throws:
      IOException
    • readInt

      public int readInt() throws IOException
      Specified by:
      readInt in interface DataInput
      Throws:
      IOException
    • readLong

      public long readLong() throws IOException
      Specified by:
      readLong in interface DataInput
      Throws:
      IOException
    • readFloat

      public float readFloat() throws IOException
      Specified by:
      readFloat in interface DataInput
      Throws:
      IOException
    • readDouble

      public double readDouble() throws IOException
      Specified by:
      readDouble in interface DataInput
      Throws:
      IOException
    • readUTF

      public String readUTF() throws IOException
      Specified by:
      readUTF in interface DataInput
      Throws:
      IOException
    • readLine

      public final String readLine() throws IOException
      Specified by:
      readLine in interface DataInput
      Throws:
      IOException
    • toString

      public String toString()
      Overrides:
      toString in class Object