com.jme.util
Class BinaryFileReader

java.lang.Object
  extended by com.jme.util.BinaryFileReader

public class BinaryFileReader
extends java.lang.Object

BinaryFileReader provides a convienience class that wraps the DataInputStream object for easier reading. Rather than having to deal with a binary file one byte at a time, this class will handle reading: byte, short, int, float and string. The file is read at the start, the file stored as an array of bytes. An index into the array will keep track of the reading point. The index can be manually adjusted via the setOffset method.

Version:
$Id: BinaryFileReader.java 4131 2009-03-19 20:15:28Z blaine.dev $
Author:
Mark Powell

Constructor Summary
BinaryFileReader(java.io.InputStream md2)
           
BinaryFileReader(java.lang.String f)
          Constructor creates a new BinaryFileReader class.
BinaryFileReader(java.net.URL f)
          Constructor instantiates a new BinaryFileReader object, loading the provided file and reading the data into a byte array.
 
Method Summary
 void markPos()
          Sets a mark for a later seekMarkOffset call.
 void open(java.net.URL f)
          open opens a given URL stream.
 int readByte()
          readByte reads a single byte from the array and returns this.
 float readFloat()
          readFloat reads four bytes from the array, generating a float.
 int readInt()
          readInt reads four bytes from the array, generating an int.
 int readShort()
          readShort reads two bytes from the array, generating a short.
 int readShort2()
           
 short readSignedShort()
          Reads a signed short value.
 java.lang.String readString(int size)
          readString reads a specified number of bytes to form a string.
 void seekMarkOffset(int offset)
          Seeks to the position of the last mark + offset.
 void setOffset(int offset)
          setOffset sets the index of the file data.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BinaryFileReader

public BinaryFileReader(java.lang.String f)
Constructor creates a new BinaryFileReader class. This constructor takes the filename string as a parameter. This filename is converted into a URL and opened. If the filename is invalid, a MalformedURLException will be generated and logged.

Parameters:
f - the file to open.

BinaryFileReader

public BinaryFileReader(java.net.URL f)
Constructor instantiates a new BinaryFileReader object, loading the provided file and reading the data into a byte array.

Parameters:
f - the file to read.

BinaryFileReader

public BinaryFileReader(java.io.InputStream md2)
Method Detail

open

public void open(java.net.URL f)
open opens a given URL stream. The data is read completely and the stream is then closed. This allows the stream to only be needed for the time it takes to read all the data, it is then closed.

Parameters:
f - the url pointing to the file to be read.

readByte

public int readByte()
readByte reads a single byte from the array and returns this. The file index is then increased by one.

Returns:
the byte at the current index.

readShort

public int readShort()
readShort reads two bytes from the array, generating a short. The file index is then increased by two. The short is then inserted into an integer for convienience.

Returns:
the short at the current index.

readShort2

public int readShort2()

readInt

public int readInt()
readInt reads four bytes from the array, generating an int. The file index is then increased by four.

Returns:
the int at the currrent index.

readFloat

public float readFloat()
readFloat reads four bytes from the array, generating a float. The file index is then increased by four.

Returns:
the float at the current index.

readString

public java.lang.String readString(int size)
readString reads a specified number of bytes to form a string. The length of the string (number of characters) is required to notify when reading should stop. The index is increased the number of characters read.

Parameters:
size - the length of the string to read.
Returns:
the string read.

setOffset

public void setOffset(int offset)
setOffset sets the index of the file data.

Parameters:
offset - the new index of the file pointer.

markPos

public void markPos()
Sets a mark for a later seekMarkOffset call.


seekMarkOffset

public void seekMarkOffset(int offset)
Seeks to the position of the last mark + offset.

Parameters:
offset - The Offset relative to mark.

readSignedShort

public short readSignedShort()
Reads a signed short value.

Returns:
The signed short.