edu.swri.swiftvis.util
Class BinaryInput

java.lang.Object
  extended by edu.swri.swiftvis.util.BinaryInput

public class BinaryInput
extends java.lang.Object

This class is intended to help other parts of the code read in data that is in a FORTRAN, C, or Java binary file. When using this, keep in mind that FORTRAN writes fields with headers and footers that tell you how many bytes are in the field.


Constructor Summary
BinaryInput(java.io.DataInput di)
           
 
Method Summary
 void close()
          Closes the wrapped file.
 byte readByte()
          Reads a single byte.
 java.lang.String readCString()
          This reads in C-style string.
 int readFortranInt(int numDigits)
          This reads a Fortran integer occupying the specified number of characters.
 double[] readFortranRecord(java.lang.String format)
          This method reads in a record from a Fortran binary file.
 java.lang.String readFortranString(int numChars)
          This reads a Fortran string occupying the specified number of characters.
 void readFully(byte[] b)
          Reads a full array of bytes.
 int readInt2()
          Reads in two bytes and compiles them to an integer value.
 int readInt4()
          Reads in four bytes and compiles them to an integer value.
 long readInt8()
          Reads in eight bytes and compiles them to an integer value.
 int readIntXDR2()
          Reads two bytes and converts them to an int according to XDR format.
 int readIntXDR4()
          Reads four bytes and converts them to an int according to XDR format.
 long readIntXDR8()
          Reads eight bytes and converts them to an int according to XDR format.
 int readJavaInt2()
          Reads two bytes and converts them to an int according to Java format.
 int readJavaInt4()
          Reads four bytes and converts them to an int according to Java format.
 double readJavaReal4()
          Reads a float in Java binary format.
 double readJavaReal8()
          Reads a double in Java binary format.
 double readReal4()
          This reads in four bytes and tries to convert them to a floating point value.
 double readReal8()
          This reads in eight bytes and tries to convert them to a floating point value.
 double readXDR4()
          This reads in a single 4-byte real in XDR format.
 double readXDR8()
          This reads in a single 8-byte real in XDR format.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BinaryInput

public BinaryInput(java.io.DataInput di)
Method Detail

close

public void close()
           throws java.io.IOException
Closes the wrapped file.

Throws:
java.io.IOException - passes on the exception from the close method.

readJavaReal4

public double readJavaReal4()
                     throws java.io.IOException
Reads a float in Java binary format.

Returns:
The float that was read.
Throws:
java.io.IOException - This will be thrown if there is a problem reading 4 bytes.

readJavaReal8

public double readJavaReal8()
                     throws java.io.IOException
Reads a double in Java binary format.

Returns:
The double that was read.
Throws:
java.io.IOException - This will be thrown if there is a problem reading 8 bytes.

readReal4

public double readReal4()
                 throws java.io.IOException
This reads in four bytes and tries to convert them to a floating point value. This method references the options for whether you are reading a big-endian or a little-endian file.

Returns:
The float that was read.
Throws:
java.io.IOException - This is thrown if there is a problem reading 4 bytes.

readReal8

public double readReal8()
                 throws java.io.IOException
This reads in eight bytes and tries to convert them to a floating point value. This method references the options for whether you are reading a big-endian or a little-endian file.

Returns:
The double that was read.
Throws:
java.io.IOException - This is thrown if there is a problem reading 8 bytes.

readXDR4

public double readXDR4()
                throws java.io.IOException
This reads in a single 4-byte real in XDR format.

Returns:
The value that was read.
Throws:
java.io.IOException - This is thrown if four bytes couldn't be read.

readXDR8

public double readXDR8()
                throws java.io.IOException
This reads in a single 8-byte real in XDR format.

Returns:
The value that was read.
Throws:
java.io.IOException - This is thrown if eight bytes couldn't be read.

readInt8

public long readInt8()
              throws java.io.IOException
Reads in eight bytes and compiles them to an integer value. It will change byte order if needed for endianness.

Returns:
The integer value that was read.
Throws:
java.io.IOException - This is thrown if eight bytes couldn't be read.

readInt4

public int readInt4()
             throws java.io.IOException
Reads in four bytes and compiles them to an integer value. It will change byte order if needed for endianness.

Returns:
The integer value that was read.
Throws:
java.io.IOException - This is thrown if four bytes couldn't be read.

readInt2

public int readInt2()
             throws java.io.IOException
Reads in two bytes and compiles them to an integer value. It will change byte order if needed for endianness.

Returns:
The integer value that was read.
Throws:
java.io.IOException - This is thrown if two bytes couldn't be read.

readIntXDR2

public int readIntXDR2()
                throws java.io.IOException
Reads two bytes and converts them to an int according to XDR format.

Returns:
The value read.
Throws:
java.io.IOException - This is thrown if two bytes could not be read.

readIntXDR4

public int readIntXDR4()
                throws java.io.IOException
Reads four bytes and converts them to an int according to XDR format.

Returns:
The value read.
Throws:
java.io.IOException - This is thrown if four bytes could not be read.

readIntXDR8

public long readIntXDR8()
                 throws java.io.IOException
Reads eight bytes and converts them to an int according to XDR format.

Returns:
The value read.
Throws:
java.io.IOException - This is thrown if eight bytes could not be read.

readJavaInt4

public int readJavaInt4()
                 throws java.io.IOException
Reads four bytes and converts them to an int according to Java format.

Returns:
The value read.
Throws:
java.io.IOException - This is thrown if four bytes could not be read.

readJavaInt2

public int readJavaInt2()
                 throws java.io.IOException
Reads two bytes and converts them to an int according to Java format.

Returns:
The value read.
Throws:
java.io.IOException - This is thrown if two bytes could not be read.

readCString

public java.lang.String readCString()
                             throws java.io.IOException
This reads in C-style string. That is a null terminated array of chars.

Returns:
A Java string representing the C-string.
Throws:
java.io.IOException - This will be thrown if there is an exception before a 0 is read in.

readByte

public byte readByte()
              throws java.io.IOException
Reads a single byte.

Returns:
The byte read.
Throws:
java.io.IOException - This is thrown if a byte can't be read.

readFully

public void readFully(byte[] b)
               throws java.io.IOException
Reads a full array of bytes.

Parameters:
b - The byte to read the values into.
Throws:
java.io.IOException - This will be thrown if b.length bytes can not be read in.

readFortranInt

public int readFortranInt(int numDigits)
                   throws java.io.IOException
This reads a Fortran integer occupying the specified number of characters.

Parameters:
numDigits - The number of character to read in.
Returns:
The value of the string read in.
Throws:
java.io.IOException - This is thrown if the specified number of bytes can't be read.

readFortranString

public java.lang.String readFortranString(int numChars)
                                   throws java.io.IOException
This reads a Fortran string occupying the specified number of characters.

Parameters:
numDigits - The number of character to read in.
Returns:
The value of the string read in.
Throws:
java.io.IOException - This is thrown if the specified number of bytes can't be read.

readFortranRecord

public double[] readFortranRecord(java.lang.String format)
                           throws java.io.IOException

This method reads in a record from a Fortran binary file. It will read in the header and footer for the record and verify that they are equal and that the number of bytes read in matches them.

The format is specified as a string in a manner similar to that used in general data source. It should be a space separated list of formats. The accepted formats are i, i*2, i*4, i*8, r, r*4, and r*8. The number after the * tells the number of bytes in the integer (i) or floating point (r) value. If no size is specified the i defaults to i*2 and r defaults to r*4. These can be followed by an optional [#] where # is the number of that type you want to read in.

The method returns an array of doubles with one entry for each value read in.

Parameters:
format - The format string for what to read.
Returns:
An array with the values read in.
Throws:
java.io.IOException - This will be thrown if there is a problem reading in the values.
java.lang.IllegalArgumentException - This is thrown if the format can't be parsed or if the record sizes don't match.