jwo.landserf.process.io
Class FileIO

java.lang.Object
  extended byjwo.landserf.process.io.FileIO
Direct Known Subclasses:
BinRasterIO, BinVectorIO, ImageIO, LandSerfIO, ShapefileIO, TextRasterIO, TextVectorIO, VrmlIO, XmlIO

public class FileIO
extends Object

Superclass of all file IO classes. Stores the last error that might have been generated if problem reading or writing a file. Also contains convenience methods for reading and writing binary integers, doubles and floating points from input streams and random access files. Keeps track of a file pointer and byte order.

Version:
2.2, 18th September, 2004.
Author:
Jo Wood

Field Summary
static int BIG_ENDIAN
          Indicates 'big endian' or Motorola byte ordering.
protected static int bitsPerValue
          Bits per value (likely to be 8, 16, 32, or 64)
protected static int byteOrder
          Byte order for binary files.
protected static String errorMessage
          Last error message or null if no errors.
protected static long filePointer
          Keeps track of read position in binary file.
static int GATHER_INFO
          Indicates file should be scanned for without loading data.
protected static int lineNumber
          Keeps track of line number in text file.
static int LITTLE_ENDIAN
          Indicates 'little endian' or Intel byte ordering.
static int LOAD_ALL
          Indicates all data should be loaded when reading a file
static int LOAD_SELECTED
          Indicates only selected data should be loaded when reading a file.
protected static float nullCode
          Value used to indicate null or missing values.
protected static boolean substituteNull
          Indicates whether or not to substitute nulls for selected values.
protected static boolean useMultiplier
          Indicates whether or not to scale z values by the multiplier.
protected static float zMultiplier
          Multiplier used for scaling z values.
 
Constructor Summary
FileIO()
          There should be no need to call the constructor explicitly since all methods are static.
 
Method Summary
static String getErrorMessage()
          Reports the last error message, or null if last file operation was sucessful.
protected static byte readByte(InputStream is)
          Reads in a single byte from the given input stream.
protected static byte readByte(RandomAccessFile raf)
          Reads a single byte of data from the given random access file.
protected static String readCharacters(int numChars, BufferedInputStream inStream)
          Reads in a given number of characters from the input stream and returns a text string representing them.
protected static String readCharacters(int numChars, BufferedReader inFile)
          Reads in a given number of characters and returns text string representing them.
protected static double readDouble(InputStream is)
          Reads a 64 bit double precision number from the given input stream.
protected static double readDouble(RandomAccessFile raf)
          Reads a 64 bit double precision number from the given random access file.
protected static double readDoubleBigEndian(InputStream is)
          Reads a 64 bit big endian double precision number from the given input stream.
protected static double readDoubleBigEndian(RandomAccessFile raf)
          Reads a 64 bit big endian double precision number from the given random access file.
protected static double readDoubleLittleEndian(InputStream is)
          Reads in a little-endian 8-byte double from the given input stream.
protected static double readDoubleLittleEndian(RandomAccessFile raf)
          Reads a 64 bit little endian double precision number from the given random access file.
protected static float readFloat(InputStream is)
          Reads a 32 bit floating point number from the given input stream.
protected static float readFloat(RandomAccessFile raf)
          Reads a 32 bit floating point number from the given random access file.
protected static float readFloatBigEndian(InputStream is)
          Reads in a big-endian 4-byte float from the given input stream.
protected static float readFloatBigEndian(RandomAccessFile raf)
          Reads a 32 bit big endian floating point number from the given random access file.
protected static float readFloatLittleEndian(InputStream is)
          Reads in a little-endian 4-byte float from the given input stream.
protected static float readFloatLittleEndian(RandomAccessFile raf)
          Reads a 32 bit little endian floating point number from the given random access file.
protected static int readInt(InputStream is)
          Reads a 32 bit unsigned word of data from the given input stream.
protected static int readInt(RandomAccessFile raf)
          Reads a 32 bit unsigned word of data from the given random access file.
protected static int readIntBigEndian(InputStream is)
          Reads in a big-endian 4-byte integer from the given input stream.
protected static int readIntBigEndian(RandomAccessFile raf)
          Reads a 32 bit unsigned big endian word of data from the given random access file.
protected static int readIntLittleEndian(InputStream is)
          Reads in a little-endian 4-byte integer from the given input stream.
protected static int readIntLittleEndian(RandomAccessFile raf)
          Reads a 32 bit unsigned little endian word of data from the given random access file.
protected static String readNextWord(BufferedReader inFile)
          Reads in the next non-whitespace set of characters.
protected static short readShort(InputStream is)
          Reads a 16 bit unsigned word of data from the given input stream.
protected static short readShort(RandomAccessFile raf)
          Reads a 16 bit unsigned word of data from the given random access file.
protected static short readShortBigEndian(InputStream is)
          Reads in a big-endian 2-byte short integer from the given input stream.
protected static short readShortBigEndian(RandomAccessFile raf)
          Reads a 16 bit unsigned big endian word of data from the given random access file.
protected static short readShortLittleEndian(InputStream is)
          Reads in a little-endian 2-byte short integer from the given input stream.
protected static short readShortLittleEndian(RandomAccessFile raf)
          Reads a 16 bit unsigned little endian word of data from the given random access file.
static void reset()
          Resets the error message and file pointer to null.
protected static void seek(RandomAccessFile raf, long numBytes)
          Moves the file pointer to the given offset from the start.
protected static void skip(InputStream is, long numBytes)
          Skips the given number of bytes in the input stream.
protected static void skip(RandomAccessFile raf, int numBytes)
          Skips the given number of bytes in the given random access file.
protected static boolean writeByte(byte value, OutputStream os)
          Writes an 8 bit byte of data to the given output stream.
protected static boolean writeCharacters(String text, OutputStream os)
          Writes out the given text as ASCII characters to the given output stream.
protected static boolean writeDouble(double value, OutputStream os)
          Writes a 64 bit double precision value to the given output stream.
protected static boolean writeDoubleBigEndian(double value, OutputStream os)
          Writes a little-endian 8-byte double to the given output stream.
protected static boolean writeDoubleLittleEndian(double value, OutputStream os)
          Writes a little-endian 8-byte double to the given output stream.
protected static boolean writeFloat(float value, OutputStream os)
          Writes a 32 bit float point value to the given output stream.
protected static boolean writeFloatBigEndian(float value, OutputStream os)
          Writes a little-endian 4-byte float to the given output stream.
protected static boolean writeFloatLittleEndian(float value, OutputStream os)
          Writes a little-endian 4-byte float to the given output stream.
protected static boolean writeInt(int value, OutputStream os)
          Writes a 32 bit unsigned word of data to the given output stream.
protected static boolean writeIntBigEndian(int value, OutputStream os)
          Writes a 32 bit unsigned big-endian ('Motorola') word of data to the given output stream.
protected static boolean writeIntLittleEndian(int value, OutputStream os)
          Writes a 32 bit unsigned little-endian ('Intel') word of data to the given output stream.
protected static boolean writeLine(String text, BufferedWriter outFile)
          Writes out a given line of text on its own line to the given buffered writer.
protected static boolean writeShort(short value, OutputStream os)
          Writes a 16 bit unsigned word of data to the given output stream.
protected static boolean writeShortBigEndian(short value, OutputStream os)
          Writes a 16 bit unsigned big-endian ('Motorola') word of data to the given output stream.
protected static boolean writeShortLittleEndian(short value, OutputStream os)
          Writes a 16 bit unsigned little-endian ('Intel') word of data to the given output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

errorMessage

protected static String errorMessage
Last error message or null if no errors.


filePointer

protected static long filePointer
Keeps track of read position in binary file.


lineNumber

protected static int lineNumber
Keeps track of line number in text file.


bitsPerValue

protected static int bitsPerValue
Bits per value (likely to be 8, 16, 32, or 64)


byteOrder

protected static int byteOrder
Byte order for binary files. Either BIG_ENDIAN or LITTLE_ENDIAN


nullCode

protected static float nullCode
Value used to indicate null or missing values.


substituteNull

protected static boolean substituteNull
Indicates whether or not to substitute nulls for selected values.


zMultiplier

protected static float zMultiplier
Multiplier used for scaling z values.


useMultiplier

protected static boolean useMultiplier
Indicates whether or not to scale z values by the multiplier.


LOAD_ALL

public static final int LOAD_ALL
Indicates all data should be loaded when reading a file

See Also:
Constant Field Values

LOAD_SELECTED

public static final int LOAD_SELECTED
Indicates only selected data should be loaded when reading a file.

See Also:
Constant Field Values

GATHER_INFO

public static final int GATHER_INFO
Indicates file should be scanned for without loading data.

See Also:
Constant Field Values

BIG_ENDIAN

public static final int BIG_ENDIAN
Indicates 'big endian' or Motorola byte ordering. The most significant byte comes first in a sequence of bytes.

See Also:
Constant Field Values

LITTLE_ENDIAN

public static final int LITTLE_ENDIAN
Indicates 'little endian' or Intel byte ordering. The least significant byte comes first in a sequence of bytes.

See Also:
Constant Field Values
Constructor Detail

FileIO

public FileIO()
There should be no need to call the constructor explicitly since all methods are static.

Method Detail

reset

public static void reset()
Resets the error message and file pointer to null.


getErrorMessage

public static String getErrorMessage()
Reports the last error message, or null if last file operation was sucessful.

Returns:
Last error message or null if last operation was sucessful.

skip

protected static void skip(InputStream is,
                           long numBytes)
Skips the given number of bytes in the input stream.

Parameters:
is - Input stream.
numBytes - Number of 8-bit bytes to skip.

skip

protected static void skip(RandomAccessFile raf,
                           int numBytes)
Skips the given number of bytes in the given random access file.

Parameters:
raf - Random access file to process.
numBytes - Number of 8-bit bytes to skip.

seek

protected static void seek(RandomAccessFile raf,
                           long numBytes)
Moves the file pointer to the given offset from the start.

Parameters:
raf - Random access file to process.
numBytes - Number of bytes from start to move file pointer.

readByte

protected static byte readByte(InputStream is)
Reads in a single byte from the given input stream.

Parameters:
is - Input stream.
Returns:
Value read from input stream.

readByte

protected static byte readByte(RandomAccessFile raf)
Reads a single byte of data from the given random access file.

Parameters:
raf - Random access file to process.
Returns:
Data read from random access file.

readShort

protected static short readShort(RandomAccessFile raf)
Reads a 16 bit unsigned word of data from the given random access file. Assumes a byte order determined by the 'byteOrder'

Parameters:
raf - Random access file to process.
Returns:
Data read from random access file.

readShort

protected static short readShort(InputStream is)
Reads a 16 bit unsigned word of data from the given input stream. Assumes a byte order determined by the 'byteOrder'

Parameters:
is - Input stream to process.
Returns:
Data read from input stream.

writeByte

protected static boolean writeByte(byte value,
                                   OutputStream os)
Writes an 8 bit byte of data to the given output stream.

Parameters:
value - Value to write to output stream.
os - Output stream to process.
Returns:
True if written successfully.

writeShort

protected static boolean writeShort(short value,
                                    OutputStream os)
Writes a 16 bit unsigned word of data to the given output stream. Assumes a byte order determined by the 'byteOrder'

Parameters:
value - Value to write to output stream.
os - Output stream to process.
Returns:
True if written successfully.

writeFloat

protected static boolean writeFloat(float value,
                                    OutputStream os)
Writes a 32 bit float point value to the given output stream. Assumes a byte order determined by the 'byteOrder'

Parameters:
value - Value to write to output stream.
os - Output stream to process.
Returns:
True if written successfully.

writeDouble

protected static boolean writeDouble(double value,
                                     OutputStream os)
Writes a 64 bit double precision value to the given output stream. Assumes a byte order determined by the 'byteOrder'

Parameters:
value - Value to write to output stream.
os - Output stream to process.
Returns:
True if written successfully.

readInt

protected static int readInt(RandomAccessFile raf)
Reads a 32 bit unsigned word of data from the given random access file. Assumes a byte order determined by the 'byteOrder'

Parameters:
raf - Random access file to process.
Returns:
Data read from random access file.

readInt

protected static int readInt(InputStream is)
Reads a 32 bit unsigned word of data from the given input stream. Assumes a byte order determined by the 'byteOrder'

Parameters:
is - Input stream to process.
Returns:
Data read from input stream.

writeInt

protected static boolean writeInt(int value,
                                  OutputStream os)
Writes a 32 bit unsigned word of data to the given output stream. Assumes a byte order determined by the 'byteOrder'

Parameters:
value - Value to write to output stream.
os - Output stream to process.
Returns:
True if written successfully.

readFloat

protected static float readFloat(RandomAccessFile raf)
Reads a 32 bit floating point number from the given random access file. Assumes a byte order determined by the 'byteOrder'

Parameters:
raf - Random access file to process.
Returns:
Data read from random access file.

readFloat

protected static float readFloat(InputStream is)
Reads a 32 bit floating point number from the given input stream. Assumes a byte order determined by the 'byteOrder'

Parameters:
is - Input stream to process.
Returns:
Data read from input stream.

readDouble

protected static double readDouble(RandomAccessFile raf)
Reads a 64 bit double precision number from the given random access file. Assumes a byte order determined by the 'byteOrder'

Parameters:
raf - Random access file to process.
Returns:
Data read from random access file.

readDouble

protected static double readDouble(InputStream is)
Reads a 64 bit double precision number from the given input stream. Assumes a byte order determined by the 'byteOrder'

Parameters:
is - Input stream to process.
Returns:
Data read from input stream.

readShortLittleEndian

protected static short readShortLittleEndian(RandomAccessFile raf)
Reads a 16 bit unsigned little endian word of data from the given random access file.

Parameters:
raf - Random access file to process.
Returns:
Data read from random access file.

readShortLittleEndian

protected static short readShortLittleEndian(InputStream is)
Reads in a little-endian 2-byte short integer from the given input stream.

Parameters:
is - Input stream.
Returns:
Value read from input stream.

writeShortLittleEndian

protected static boolean writeShortLittleEndian(short value,
                                                OutputStream os)
Writes a 16 bit unsigned little-endian ('Intel') word of data to the given output stream.

Parameters:
value - Value to write to output stream.
os - Output stream to process.
Returns:
True if written successfully.

readIntLittleEndian

protected static int readIntLittleEndian(RandomAccessFile raf)
Reads a 32 bit unsigned little endian word of data from the given random access file.

Parameters:
raf - Random access file to process.
Returns:
Data read from random access file.

readIntLittleEndian

protected static int readIntLittleEndian(InputStream is)
Reads in a little-endian 4-byte integer from the given input stream.

Parameters:
is - Input stream.
Returns:
Value read from input stream.

writeIntLittleEndian

protected static boolean writeIntLittleEndian(int value,
                                              OutputStream os)
Writes a 32 bit unsigned little-endian ('Intel') word of data to the given output stream.

Parameters:
value - Value to write to output stream.
os - Output stream to process.
Returns:
True if written successfully.

readFloatLittleEndian

protected static float readFloatLittleEndian(RandomAccessFile raf)
Reads a 32 bit little endian floating point number from the given random access file.

Parameters:
raf - Random access file to process.
Returns:
Data read from random access file.

readFloatLittleEndian

protected static float readFloatLittleEndian(InputStream is)
Reads in a little-endian 4-byte float from the given input stream.

Parameters:
is - Input stream.
Returns:
Value read from input stream.

writeFloatBigEndian

protected static boolean writeFloatBigEndian(float value,
                                             OutputStream os)
Writes a little-endian 4-byte float to the given output stream.

Parameters:
value - Value to write to output stream.
os - Output stream.
Returns:
True if written successfully.

writeFloatLittleEndian

protected static boolean writeFloatLittleEndian(float value,
                                                OutputStream os)
Writes a little-endian 4-byte float to the given output stream.

Parameters:
value - Value to write to output stream.
os - Output stream.
Returns:
True if written successfully.

writeDoubleBigEndian

protected static boolean writeDoubleBigEndian(double value,
                                              OutputStream os)
Writes a little-endian 8-byte double to the given output stream.

Parameters:
value - Value to write to output stream.
os - Output stream.
Returns:
True if written successfully.

writeDoubleLittleEndian

protected static boolean writeDoubleLittleEndian(double value,
                                                 OutputStream os)
Writes a little-endian 8-byte double to the given output stream.

Parameters:
value - Value to write to output stream.
os - Output stream.
Returns:
True if written successfully.

readDoubleLittleEndian

protected static double readDoubleLittleEndian(RandomAccessFile raf)
Reads a 64 bit little endian double precision number from the given random access file.

Parameters:
raf - Random access file to process.
Returns:
Data read from random access file.

readDoubleLittleEndian

protected static double readDoubleLittleEndian(InputStream is)
Reads in a little-endian 8-byte double from the given input stream.

Parameters:
is - Input stream.
Returns:
Value read from input stream.

readShortBigEndian

protected static short readShortBigEndian(RandomAccessFile raf)
Reads a 16 bit unsigned big endian word of data from the given random access file.

Parameters:
raf - Random access file to process.
Returns:
Data read from random access file.

readShortBigEndian

protected static short readShortBigEndian(InputStream is)
Reads in a big-endian 2-byte short integer from the given input stream.

Parameters:
is - Input stream.
Returns:
Value read from input stream.

writeShortBigEndian

protected static boolean writeShortBigEndian(short value,
                                             OutputStream os)
Writes a 16 bit unsigned big-endian ('Motorola') word of data to the given output stream.

Parameters:
value - Value to write to output stream.
os - Output stream to process.
Returns:
True if written successfully.

readIntBigEndian

protected static int readIntBigEndian(RandomAccessFile raf)
Reads a 32 bit unsigned big endian word of data from the given random access file.

Parameters:
raf - Random access file to process.
Returns:
Data read from random access file.

readIntBigEndian

protected static int readIntBigEndian(InputStream is)
Reads in a big-endian 4-byte integer from the given input stream.

Parameters:
is - Input stream.
Returns:
Value read from input stream.

writeIntBigEndian

protected static boolean writeIntBigEndian(int value,
                                           OutputStream os)
Writes a 32 bit unsigned big-endian ('Motorola') word of data to the given output stream.

Parameters:
value - Value to write to output stream.
os - Output stream to process.
Returns:
True if written successfully.

readFloatBigEndian

protected static float readFloatBigEndian(RandomAccessFile raf)
Reads a 32 bit big endian floating point number from the given random access file.

Parameters:
raf - Random access file to process.
Returns:
Data read from random access file.

readFloatBigEndian

protected static float readFloatBigEndian(InputStream is)
Reads in a big-endian 4-byte float from the given input stream.

Parameters:
is - Input stream.
Returns:
Value read from input stream.

readDoubleBigEndian

protected static double readDoubleBigEndian(RandomAccessFile raf)
Reads a 64 bit big endian double precision number from the given random access file.

Parameters:
raf - Random access file to process.
Returns:
Data read from random access file.

readDoubleBigEndian

protected static double readDoubleBigEndian(InputStream is)
Reads a 64 bit big endian double precision number from the given input stream.

Parameters:
is - Input stream.
Returns:
Value read from input stream.

writeLine

protected static boolean writeLine(String text,
                                   BufferedWriter outFile)
Writes out a given line of text on its own line to the given buffered writer.

Parameters:
text - Text to write.
outFile - Buffered file writer to receive the text.
Returns:
true if written successfully.

readCharacters

protected static String readCharacters(int numChars,
                                       BufferedReader inFile)
Reads in a given number of characters and returns text string representing them. If the end of file is reached before reading is finished, the returned string will be suitably truncated. Note this does not keep track of line numbers since this method does not require line feed or carrage return characters to work.

Parameters:
numChars - Number of characters to read.
inFile - File from which to read.
Returns:
Text read or null if problem reading from file.

readCharacters

protected static String readCharacters(int numChars,
                                       BufferedInputStream inStream)
Reads in a given number of characters from the input stream and returns a text string representing them. If the end of file is reached before reading is finished, the returned string will be suitably truncated. Note this does not keep track of line numbers since this method does not require line feed or carrage return characters to work.

Parameters:
numChars - Number of characters to read.
inStream - Input stream from which to read.
Returns:
Text read or null if problem reading from file.

writeCharacters

protected static boolean writeCharacters(String text,
                                         OutputStream os)
Writes out the given text as ASCII characters to the given output stream. This method should be used for writing text in binary files only.

Parameters:
text - Text to write.
os - Output stream to use.
Returns:
True if written successfully.

readNextWord

protected static String readNextWord(BufferedReader inFile)
Reads in the next non-whitespace set of characters. Effectively acts as a simple instance of a string tokenizer. For efficiency reasons, this method will move the file pointer to 1 character after the last character of the word. This should not be a problem for whitespace separated text, but could be a problem for fixed width reading. Note this does not keep track of line numbers since this method does not require line feed or carrage return characters to work.

Parameters:
inFile - File from which to read.
Returns:
Text read or null if problem reading from file.


Copyright Jo Wood, 1996-2005, last modified, 11th March, 2005