com.drew.lang
Class RandomAccessFileReader

java.lang.Object
  extended by com.drew.lang.RandomAccessFileReader
All Implemented Interfaces:
BufferReader

public class RandomAccessFileReader
extends Object
implements BufferReader

Provides methods to read specific values from a byte array, with a consistent, checked exception structure for issues.

By default, the reader operates with Motorola byte order (big endianness). This can be changed by calling setMotorolaByteOrder(boolean).

Author:
Drew Noakes http://drewnoakes.com

Constructor Summary
RandomAccessFileReader(RandomAccessFile file)
           
 
Method Summary
 byte[] getBytes(int index, int count)
           
 double getDouble64(int index)
           
 float getFloat32(int index)
           
 short getInt16(int index)
          Returns a signed 16-bit int calculated from two bytes of data at the specified index (MSB, LSB).
 int getInt32(int index)
          Returns a signed 32-bit integer from four bytes of data at the specified index the buffer.
 long getInt64(int index)
          Get a signed 64-bit integer from the buffer.
 byte getInt8(int index)
          Returns a signed 8-bit int calculated from one byte of data at the specified index.
 long getLength()
          Returns the length of the buffer.
 String getNullTerminatedString(int index, int maxLengthBytes)
          Creates a String from the _data buffer starting at the specified index, and ending where byte=='\0' or where length==maxLength.
 float getS15Fixed16(int index)
           
 String getString(int index, int bytesRequested)
           
 String getString(int index, int bytesRequested, String charset)
           
 int getUInt16(int index)
          Returns an unsigned 16-bit int calculated from two bytes of data at the specified index.
 long getUInt32(int index)
          Get a 32-bit unsigned integer from the buffer, returning it as a long.
 short getUInt8(int index)
          Returns an unsigned 8-bit int calculated from one byte of data at the specified index.
 boolean isMotorolaByteOrder()
          Gets the endianness of this reader.
 void setMotorolaByteOrder(boolean motorolaByteOrder)
          Sets the endianness of this reader.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RandomAccessFileReader

public RandomAccessFileReader(RandomAccessFile file)
                       throws IOException
Throws:
IOException
Method Detail

getLength

public long getLength()
Description copied from interface: BufferReader
Returns the length of the buffer. This value represents the total number of bytes in the underlying source.

Specified by:
getLength in interface BufferReader
Returns:
The number of bytes in the buffer.

setMotorolaByteOrder

public void setMotorolaByteOrder(boolean motorolaByteOrder)
Description copied from interface: BufferReader
Sets the endianness of this reader.

Specified by:
setMotorolaByteOrder in interface BufferReader
Parameters:
motorolaByteOrder - true for motorola/big endian, false for intel/little endian

isMotorolaByteOrder

public boolean isMotorolaByteOrder()
Description copied from interface: BufferReader
Gets the endianness of this reader.

Specified by:
isMotorolaByteOrder in interface BufferReader

getUInt8

public short getUInt8(int index)
               throws BufferBoundsException
Description copied from interface: BufferReader
Returns an unsigned 8-bit int calculated from one byte of data at the specified index.

Specified by:
getUInt8 in interface BufferReader
Parameters:
index - position within the data buffer to read byte
Returns:
the 8 bit int value, between 0 and 255
Throws:
BufferBoundsException - the buffer does not contain enough bytes to service the request, or index is negative

getInt8

public byte getInt8(int index)
             throws BufferBoundsException
Description copied from interface: BufferReader
Returns a signed 8-bit int calculated from one byte of data at the specified index.

Specified by:
getInt8 in interface BufferReader
Parameters:
index - position within the data buffer to read byte
Returns:
the 8 bit int value, between 0x00 and 0xFF
Throws:
BufferBoundsException - the buffer does not contain enough bytes to service the request, or index is negative

getUInt16

public int getUInt16(int index)
              throws BufferBoundsException
Description copied from interface: BufferReader
Returns an unsigned 16-bit int calculated from two bytes of data at the specified index.

Specified by:
getUInt16 in interface BufferReader
Parameters:
index - position within the data buffer to read first byte
Returns:
the 16 bit int value, between 0x0000 and 0xFFFF
Throws:
BufferBoundsException - the buffer does not contain enough bytes to service the request, or index is negative

getInt16

public short getInt16(int index)
               throws BufferBoundsException
Description copied from interface: BufferReader
Returns a signed 16-bit int calculated from two bytes of data at the specified index (MSB, LSB).

Specified by:
getInt16 in interface BufferReader
Parameters:
index - position within the data buffer to read first byte
Returns:
the 16 bit int value, between 0x0000 and 0xFFFF
Throws:
BufferBoundsException - the buffer does not contain enough bytes to service the request, or index is negative

getUInt32

public long getUInt32(int index)
               throws BufferBoundsException
Description copied from interface: BufferReader
Get a 32-bit unsigned integer from the buffer, returning it as a long.

Specified by:
getUInt32 in interface BufferReader
Parameters:
index - position within the data buffer to read first byte
Returns:
the unsigned 32-bit int value as a long, between 0x00000000 and 0xFFFFFFFF
Throws:
BufferBoundsException - the buffer does not contain enough bytes to service the request, or index is negative

getInt32

public int getInt32(int index)
             throws BufferBoundsException
Description copied from interface: BufferReader
Returns a signed 32-bit integer from four bytes of data at the specified index the buffer.

Specified by:
getInt32 in interface BufferReader
Parameters:
index - position within the data buffer to read first byte
Returns:
the signed 32 bit int value, between 0x00000000 and 0xFFFFFFFF
Throws:
BufferBoundsException - the buffer does not contain enough bytes to service the request, or index is negative

getInt64

public long getInt64(int index)
              throws BufferBoundsException
Description copied from interface: BufferReader
Get a signed 64-bit integer from the buffer.

Specified by:
getInt64 in interface BufferReader
Parameters:
index - position within the data buffer to read first byte
Returns:
the 64 bit int value, between 0x0000000000000000 and 0xFFFFFFFFFFFFFFFF
Throws:
BufferBoundsException - the buffer does not contain enough bytes to service the request, or index is negative

getS15Fixed16

public float getS15Fixed16(int index)
                    throws BufferBoundsException
Specified by:
getS15Fixed16 in interface BufferReader
Throws:
BufferBoundsException

getFloat32

public float getFloat32(int index)
                 throws BufferBoundsException
Specified by:
getFloat32 in interface BufferReader
Throws:
BufferBoundsException

getDouble64

public double getDouble64(int index)
                   throws BufferBoundsException
Specified by:
getDouble64 in interface BufferReader
Throws:
BufferBoundsException

getBytes

public byte[] getBytes(int index,
                       int count)
                throws BufferBoundsException
Specified by:
getBytes in interface BufferReader
Throws:
BufferBoundsException

getString

public String getString(int index,
                        int bytesRequested)
                 throws BufferBoundsException
Specified by:
getString in interface BufferReader
Throws:
BufferBoundsException

getString

public String getString(int index,
                        int bytesRequested,
                        String charset)
                 throws BufferBoundsException
Specified by:
getString in interface BufferReader
Throws:
BufferBoundsException

getNullTerminatedString

public String getNullTerminatedString(int index,
                                      int maxLengthBytes)
                               throws BufferBoundsException
Description copied from interface: BufferReader
Creates a String from the _data buffer starting at the specified index, and ending where byte=='\0' or where length==maxLength.

Specified by:
getNullTerminatedString in interface BufferReader
Parameters:
index - The index within the buffer at which to start reading the string.
maxLengthBytes - The maximum number of bytes to read. If a zero-byte is not reached within this limit, reading will stop and the string will be truncated to this length.
Returns:
The read string.
Throws:
BufferBoundsException - The buffer does not contain enough bytes to satisfy this request.


Copyright © 2012. All Rights Reserved.