fiftyone.mobile.detection.readers
Class TrieReader

java.lang.Object
  extended by fiftyone.mobile.detection.readers.TrieReader

public class TrieReader
extends Object

Due to the size of the Trie tree structure, the data cannot fit into a single ByteBuffer. Therefore, this class is used to wrap the file channel and and become logically one channel.


Nested Class Summary
private static class TrieReader.DataType
          Defines what data type is being read from a given buffer.
 
Field Summary
private  int _bufferIndex
          The index of the buffer that _position is pointed at.
private  ArrayList<MappedByteBuffer> _buffers
          Buffers mapped to the files channel.
private  long _globalPosition
           
private  long _position
          Logical position of the data needed in the file.
private  long _start
          The start position of the tree in the trie file.
private  FileChannel _triFile
          The File Channel linked to the trie data.
 
Constructor Summary
TrieReader(FileChannel reader)
          Creates the class by creating multiple ByteBuffers that can be read from.
 
Method Summary
 void close()
          Clears the ByteBuffers and closes the File Channel.
private  byte getNextByte()
          Gets the MappedByteBuffer for that this class is currently positioned on, automatically incrementing position and changing buffer if required.
 long getPos()
          Returns the current logical position value.
private  byte[] getValue(TrieReader.DataType dataType)
          Reads a certain data type from the file e.g.
 byte readByte()
           
 byte[] readBytes(int length)
           
 int readInt()
          Read an signed integer from the file at the current location.
 long readLong()
          Read an signed long from the file at the current location.
 long readLong(long position)
          Read an signed long from the file at a specified location.
 short readUByte()
          Read an Unsigned byte from the file at the current location.
 short readUByte(long position)
          Read an Unsigned byte from the file at a specified location.
 long readUInt()
          Read an Unsigned integer from the file at the current location.
 int readUShort()
          Read an Unsigned short from the file at the current location.
 int readUShort(long position)
          Read an Unsigned short from the file at a specified location.
 void setPos(long position)
          Sets the logical position in the file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_buffers

private ArrayList<MappedByteBuffer> _buffers
Buffers mapped to the files channel.


_triFile

private final FileChannel _triFile
The File Channel linked to the trie data.


_start

private final long _start
The start position of the tree in the trie file.


_position

private long _position
Logical position of the data needed in the file.


_bufferIndex

private int _bufferIndex
The index of the buffer that _position is pointed at.


_globalPosition

private long _globalPosition
Constructor Detail

TrieReader

public TrieReader(FileChannel reader)
           throws IOException
Creates the class by creating multiple ByteBuffers that can be read from.

Parameters:
reader - File channel to read from.
Throws:
IOException
Method Detail

getPos

public long getPos()
Returns the current logical position value.

Returns:
the current logical position in the file.

setPos

public void setPos(long position)
Sets the logical position in the file.

Parameters:
position - position value.

readUByte

public short readUByte()
Read an Unsigned byte from the file at the current location.

Returns:
The value read.

readUByte

public short readUByte(long position)
Read an Unsigned byte from the file at a specified location.

Parameters:
position - Position to read from.
Returns:
The value read.

readUShort

public int readUShort()
Read an Unsigned short from the file at the current location.

Returns:
The value read.

readUShort

public int readUShort(long position)
Read an Unsigned short from the file at a specified location.

Parameters:
position - Position to read from.
Returns:
The value read.

readUInt

public long readUInt()
              throws IOException
Read an Unsigned integer from the file at the current location.

Returns:
The Value read.
Throws:
IOException

readInt

public int readInt()
            throws IOException
Read an signed integer from the file at the current location.

Returns:
The Value read.
Throws:
IOException

readLong

public long readLong()
Read an signed long from the file at the current location.

Returns:
The Value read.
Throws:
IOException

readLong

public long readLong(long position)
              throws IOException
Read an signed long from the file at a specified location.

Parameters:
position - Position to read from.
Returns:
The value read.
Throws:
IOException

getNextByte

private byte getNextByte()
Gets the MappedByteBuffer for that this class is currently positioned on, automatically incrementing position and changing buffer if required.


readByte

public byte readByte()

readBytes

public byte[] readBytes(int length)

getValue

private byte[] getValue(TrieReader.DataType dataType)
Reads a certain data type from the file e.g. byte, Unsigned Integer etc.

Parameters:
dataType - Type of data to read.
Returns:
byte array of data.

close

public void close()
           throws IOException
Clears the ByteBuffers and closes the File Channel.

Throws:
IOException