Package nom.tam.util

Class InputDecoder.InputBuffer

java.lang.Object
nom.tam.util.InputDecoder.InputBuffer
Enclosing class:
InputDecoder

protected final class InputDecoder.InputBuffer extends Object

The conversion buffer for decoding binary data representation into Java arrays (objects).

The buffering is most efficient, if we fist specify how many bytes of input maybe be consumed first (buffered from the input), via loadBytes(long, int). After that, we can call the get routines of this class to return binary data converted to Java format until we exhaust the specified alotment of bytes.

 // The data we want to retrieve
 double d;
 int i;
 short[] shortArray = new short[100];
 float[] floaTarray = new float[48];

 // We convert from the binary format to Java format using
 // the local conversion buffer
 ConversionBuffer buf = getBuffer();

 // We can allow the conversion buffer to read enough bytes for all
 // data we want to retrieve:
 buf.loadBytes(FitsIO.BYTES_IN_DOUBLE + FitsIO.BYTES_IN_INT + FitsIO.BYTES_IN_SHORT * shortArray.length
         + FitsIO.BYTES_IN_FLOAT * floatArray.length);

 // Now we can get the data with minimal underlying IO calls...
 d = buf.getDouble();
 i = buf.getInt();

 for (int i = 0; i < shortArray.length; i++) {
     shortArray[i] = buf.getShort();
 }

 for (int i = 0; i < floatArray.length; i++) {
     floatArray[i] = buf.getFloat();
 }
 

In the special case that one needs just a single element (or a few single elements) from the input, rather than lots of elements or arrays, one may use loadOne(int) instead of loadBytes(long, int) to read just enough bytes for a single data element from the input before each conversion. For example:

 ConversionBuffer buf = getBuffer();

 buf.loadOne(FitsIO.BYTES_IN_FLOAT);
 float f = buf.getFloat();
 
Author:
Attila Kovacs
  • Method Summary

    Modifier and Type
    Method
    Description
    protected ByteOrder
    Returns the current byte order of the binary data representation from which we are decoding.
    protected int
    get()
    Retrieves a single byte from the buffer.
    protected int
    get(byte[] dst, int from, int n)
    Retrieves a sequence of signed bytes from the buffer.
    protected int
    get(double[] dst, int from, int n)
    Retrieves a sequence of big-endian 64-bit floating-point values from the buffer.
    protected int
    get(float[] dst, int from, int n)
    Retrieves a sequence of big-endian 32-bit floating-point values from the buffer.
    protected int
    get(int[] dst, int from, int n)
    Retrieves a sequence of big-endian 32-bit signed integers from the buffer.
    protected int
    get(long[] dst, int from, int n)
    Retrieves a sequence of big-endian 64-bit signed integers from the buffer.
    protected int
    get(short[] dst, int from, int n)
    Retrieves a sequence of big-endian 16-bit signed integers from the buffer.
    protected double
    Retrieves a 8-byte double-precision floating point value from the buffer.
    protected float
    Retrieves a 4-byte single-precision floating point value from the buffer.
    protected int
    Retrieves a 4-byte integer from the buffer.
    protected long
    Retrieves a 8-byte integer from the buffer.
    protected int
    Retrieves a 2-byte unsigned integer from the buffer.
    protected void
    loadBytes(long n, int size)
    Set the number of bytes we can buffer from the input for subsequent retrieval from this buffer.
    protected boolean
    loadOne(int size)
    Loads just a single element of the specified byte size.
    protected void
    Sets the byte order of the binary data representation from which we are decoding data.

    Methods inherited from class java.lang.Object

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

    • setByteOrder

      protected void setByteOrder(ByteOrder order)
      Sets the byte order of the binary data representation from which we are decoding data.
      Parameters:
      order - the new byte order
      See Also:
    • byteOrder

      protected ByteOrder byteOrder()
      Returns the current byte order of the binary data representation from which we are decoding.
      Returns:
      the byte order
      See Also:
    • loadBytes

      protected void loadBytes(long n, int size)
      Set the number of bytes we can buffer from the input for subsequent retrieval from this buffer. The get methods of this class will be ensured not to fetch data from the input beyond the requested size.
      Parameters:
      n - the number of elements we can read and buffer from the input
      size - the number of bytes in each elements.
    • loadOne

      protected boolean loadOne(int size) throws IOException
      Loads just a single element of the specified byte size. The element must fit into the conversion buffer, and it is up to the caller to ensure that. The method itself does not check.
      Parameters:
      size - The number of bytes in the element
      Returns:
      true if the data was successfully read from the uderlying stream or file, otherwise false.
      Throws:
      IOException - if there was an IO error, other than the end-of-file.
    • get

      protected int get() throws IOException
      Retrieves a single byte from the buffer. Before data can be retrieved with this method they should be 'loaded' into the buffer via loadOne(int) or loadBytes(long, int). This method is appropriate for retrieving one or a fwew bytes at a time. For bulk input of bytes, you should use InputDecoder.read(byte[], int, int) instead for superior performance.
      Returns:
      the byte value, or -1 if no more data is available from the buffer or the underlying input.
      Throws:
      IOException - if there as an IO error, other than the end of file, while trying to read more data from the underlying input into the buffer.
      See Also:
    • getUnsignedShort

      protected int getUnsignedShort() throws IOException
      Retrieves a 2-byte unsigned integer from the buffer. Before data can be retrieved with this method the should be 'loaded' into the buffer via loadOne(int) or loadBytes(long, int).
      Returns:
      the 16-bit integer value, or -1 if no more data is available from the buffer or the underlying input.
      Throws:
      IOException - if there as an IO error, other than the end of file, while trying to read more data from the underlying input into the buffer.
      See Also:
    • getInt

      protected int getInt() throws EOFException, IOException
      Retrieves a 4-byte integer from the buffer. Before data can be retrieved with this method the should be 'loaded' into the buffer via loadOne(int) or loadBytes(long, int).
      Returns:
      the 32-bit integer value.
      Throws:
      EOFException - if already at the end of file.
      IOException - if there as an IO error
      See Also:
    • getLong

      protected long getLong() throws EOFException, IOException
      Retrieves a 8-byte integer from the buffer. Before data can be retrieved with this method the should be 'loaded' into the buffer via loadOne(int) or loadBytes(long, int).
      Returns:
      the 64-bit integer value.
      Throws:
      EOFException - if already at the end of file.
      IOException - if there as an IO error
      See Also:
    • getFloat

      protected float getFloat() throws EOFException, IOException
      Retrieves a 4-byte single-precision floating point value from the buffer. Before data can be retrieved with this method the shold be 'loaded' into the buffer via loadOne(int) or loadBytes(long, int).
      Returns:
      the 32-bit single-precision floating-point value.
      Throws:
      EOFException - if already at the end of file.
      IOException - if there as an IO error
      See Also:
    • getDouble

      protected double getDouble() throws EOFException, IOException
      Retrieves a 8-byte double-precision floating point value from the buffer. Before data can be retrieved with this method they should be 'loaded' into the buffer via loadOne(int) or loadBytes(long, int).
      Returns:
      the 64-bit double-precision floating-point value.
      Throws:
      EOFException - if already at the end of file.
      IOException - if there as an IO error
      See Also:
    • get

      protected int get(byte[] dst, int from, int n) throws EOFException, IOException
      Retrieves a sequence of signed bytes from the buffer. Before data can be retrieved with this method the should be 'loaded' into the buffer via loadBytes(long, int).
      Parameters:
      dst - Java array in which to store the retrieved sequence of elements
      from - the array index for storing the first element retrieved
      n - the number of elements to retrieve
      Returns:
      the number of elements successfully retrieved
      Throws:
      EOFException - if already at the end of file.
      IOException - if there was an IO error before, before requested number of bytes could be read
      Since:
      1.18
      See Also:
    • get

      protected int get(short[] dst, int from, int n) throws EOFException, IOException
      Retrieves a sequence of big-endian 16-bit signed integers from the buffer. Before data can be retrieved with this method they should be 'loaded' into the buffer via loadBytes(long, int).
      Parameters:
      dst - Java array in which to store the retrieved sequence of elements
      from - the array index for storing the first element retrieved
      n - the number of elements to retrieve
      Returns:
      the number of elements successfully retrieved
      Throws:
      EOFException - if already at the end of file.
      IOException - if there was an IO error before, before requested number of bytes could be read
      Since:
      1.18
      See Also:
    • get

      protected int get(int[] dst, int from, int n) throws EOFException, IOException
      Retrieves a sequence of big-endian 32-bit signed integers from the buffer. Before data can be retrieved with this method they should be 'loaded' into the buffer via loadBytes(long, int).
      Parameters:
      dst - Java array in which to store the retrieved sequence of elements
      from - the array index for storing the first element retrieved
      n - the number of elements to retrieve
      Returns:
      the number of elements successfully retrieved
      Throws:
      EOFException - if already at the end of file.
      IOException - if there was an IO error before, before requested number of bytes could be read
      Since:
      1.18
      See Also:
    • get

      protected int get(long[] dst, int from, int n) throws EOFException, IOException
      Retrieves a sequence of big-endian 64-bit signed integers from the buffer. Before data can be retrieved with this method they should be 'loaded' into the buffer via loadBytes(long, int).
      Parameters:
      dst - Java array in which to store the retrieved sequence of elements
      from - the array index for storing the first element retrieved
      n - the number of elements to retrieve
      Returns:
      the number of elements successfully retrieved
      Throws:
      EOFException - if already at the end of file.
      IOException - if there was an IO error before, before requested number of bytes could be read
      Since:
      1.18
      See Also:
    • get

      protected int get(float[] dst, int from, int n) throws EOFException, IOException
      Retrieves a sequence of big-endian 32-bit floating-point values from the buffer. Before data can be retrieved with this method they should be 'loaded' into the buffer via loadBytes(long, int).
      Parameters:
      dst - Java array in which to store the retrieved sequence of elements
      from - the array index for storing the first element retrieved
      n - the number of elements to retrieve
      Returns:
      the number of elements successfully retrieved
      Throws:
      EOFException - if already at the end of file.
      IOException - if there was an IO error before, before requested number of bytes could be read
      Since:
      1.18
      See Also:
    • get

      protected int get(double[] dst, int from, int n) throws EOFException, IOException
      Retrieves a sequence of big-endian 64-bit floating-point values from the buffer. Before data can be retrieved with this method they should be 'loaded' into the buffer via loadBytes(long, int).
      Parameters:
      dst - Java array in which to store the retrieved sequence of elements
      from - the array index for storing the first element retrieved
      n - the number of elements to retrieve
      Returns:
      the number of elements successfully retrieved
      Throws:
      EOFException - if already at the end of file.
      IOException - if there was an IO error before, before requested number of bytes could be read
      Since:
      1.18
      See Also: