Class ByteIterator

java.lang.Object
  |
  +--ByteIterator

public class ByteIterator
extends java.lang.Object


Constructor Summary
ByteIterator(byte[] d)
          Creates a new copy of the buffer.
ByteIterator(byte[] d, boolean newBuf)
          May create a copy of the given buffer or create a new one.
ByteIterator(byte[] d, int start, int length)
          Create a new buffer from a portion of a data buffer.
 
Method Summary
 void compile(java.lang.String pattern)
          Compiles the text pattern for searching.
static int concoctInt(byte[] b)
          Converts 4 bytes of a byte array into an int.
static int concoctInt(byte[] b, int start)
          Converts 4 bytes of a byte array into an int from the starting position.
static long concoctLong(byte[] b)
          Converts the first 8 bytes of a byte array into a long.
static long concoctLong(byte[] b, int start)
          Converts 8 bytes of a byte array into a long from the starting position.
static short concoctShort(byte[] b)
          Converts 2 bytes of a byte array into a short
static short concoctShort(byte[] b, int start)
          Converts 2 bytes of a byte array into a short from the starting position.
 int current()
          Returns the current position in the buffer.
static byte[] decoct(int i)
          Converts an int into 4 bytes.
static byte[] decoct(long i)
          Converts a long into 8 bytes.
static byte[] decoct(short i)
          Converts a short into 2 bytes.
 java.lang.String dump(int start, int length)
          Dump part of the byte stream.
 byte[] extend(int diff)
          Extends data buffer (may be set longer or shorter).
 byte first()
          Returns the first byte of buffer in the data.
 byte[] getBuffer()
          Return the current data buffer.
 byte last()
          Returns the last byte of buffer in the data.
 long length()
          Returns the current length of the data buffer.
 void move(int count)
          Move current position by an arbitrary number of bytes.
 void moveByte()
          Advance in the array by one byte
 void moveInt()
          Advance in the array by four bytes (size of int).
 void moveLong()
          Advance in the array by 8 bytes (size of long).
 void moveShort()
          Advance in the array by two bytes (size of short).
 byte nextByte()
          Return the next byte.
 int nextInt()
          Returns the next int (4 bytes).
 long nextLong()
          Returns the next long (8 bytes).
 short nextShort()
          Returns the next short (2 bytes).
 int nextUnsignedByte()
          Returns the next unsigned byte (1 byte) as an int.
 int nextUnsignedShort()
          Returns the next unsigned short (2 bytes) as an int.
 int partialMatch()
          Returns the position at the end of the text buffer where a partial match was found.
 int read(byte[] b)
          Read a buffer full of bytes.
 int read(byte[] b, int offset, int count)
          Read bytes into a buffer.
 byte readByte()
          Returns the next byte.
 int readInt()
          Returns the next int (4 bytes).
 java.lang.String readLine()
          Read a line from the byte stream.
 long readLong()
          Returns the next long (8 bytes).
 short readShort()
          Returns the next short (2 bytes).
 int readUnsignedByte()
          Returns the next unsignedbyte (1 byte).
 int readUnsignedShort()
          Returns the next unsignedshort (2 bytes).
 int search(int start, int length)
          Search for the compiled pattern in the given text.
 void seek(long posn)
          Absolute position to move to in the data buffer.
 void setIndex(int posn)
          Sets the current position.
 void setInt(int i)
          Sets an int at the current location Does not advance the buffer position.
 void setLong(long i)
          Sets a long at the current location.
 void setShort(short i)
          Sets a short at the current location Does not advance the buffer position.
 void skipBytes(int count)
          Skips over bytes.
 int write(byte[] b)
          Write a byte array to the buffer at the current position.
 void writeInt(int i)
          Write an int to the buffer at the current position.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ByteIterator

public ByteIterator(byte[] d)
Creates a new copy of the buffer. Used if you want to preserve the original buffer while making changes to the copy.
Parameters:
data - Data byte array to use.
See Also:
getBuffer()

ByteIterator

public ByteIterator(byte[] d,
                    boolean newBuf)
May create a copy of the given buffer or create a new one.
Parameters:
d - Data byte array to use.
newBuf - true if a new buffer is required, false if we use the original.
See Also:
getBuffer()

ByteIterator

public ByteIterator(byte[] d,
                    int start,
                    int length)
Create a new buffer from a portion of a data buffer. References to the new buffer are zero based.
Parameters:
data - Data byte array to use.
start - Index of the first byte
length - length to use
See Also:
getBuffer()
Method Detail

getBuffer

public byte[] getBuffer()
Return the current data buffer. Useful if you need to retreive the copied data buffer.
Returns:
work buffer

first

public byte first()
Returns the first byte of buffer in the data. Sets the current position to zero.
Returns:
byte

last

public byte last()
Returns the last byte of buffer in the data. Sets the current position to the last byte.
Returns:
byte

setIndex

public void setIndex(int posn)
Sets the current position. If the position exceeds the length, the position is not changed
Parameters:
posn - Set the current index into the array.

move

public void move(int count)
Move current position by an arbitrary number of bytes. If the position exceeds the length, the position is not changed
Parameters:
count - Number of byte to advance

current

public int current()
Returns the current position in the buffer.
Returns:
current position

nextLong

public long nextLong()
Returns the next long (8 bytes). Does not advance the buffer position.
Returns:
the long value.

moveLong

public void moveLong()
Advance in the array by 8 bytes (size of long).

nextInt

public int nextInt()
Returns the next int (4 bytes). Does not advance the buffer position.
Returns:
the int value.

moveInt

public void moveInt()
Advance in the array by four bytes (size of int).

nextShort

public short nextShort()
Returns the next short (2 bytes). Does not advance the buffer position.
Returns:
the short value.

nextUnsignedShort

public int nextUnsignedShort()
Returns the next unsigned short (2 bytes) as an int. Does not advance the buffer position.
Returns:
unsigned short value as an int.

nextUnsignedByte

public int nextUnsignedByte()
Returns the next unsigned byte (1 byte) as an int. Does not advance the buffer position.
Returns:
the unsigned byte value an int.

moveShort

public void moveShort()
Advance in the array by two bytes (size of short).

nextByte

public byte nextByte()
Return the next byte. Does not advance the buffer position.
Returns:
the next byte value

moveByte

public void moveByte()
Advance in the array by one byte

setShort

public void setShort(short i)
Sets a short at the current location Does not advance the buffer position.
Parameters:
i -  

setInt

public void setInt(int i)
Sets an int at the current location Does not advance the buffer position.
Parameters:
i -  

setLong

public void setLong(long i)
Sets a long at the current location. Does not advance the buffer position.
Parameters:
i -  

concoctLong

public static long concoctLong(byte[] b)
Converts the first 8 bytes of a byte array into a long.
Parameters:
b - buffer.
Returns:
long value

concoctLong

public static long concoctLong(byte[] b,
                               int start)
Converts 8 bytes of a byte array into a long from the starting position.
Parameters:
b - buffer.
start - postion to extract data
Returns:
long value

concoctInt

public static int concoctInt(byte[] b)
Converts 4 bytes of a byte array into an int.
Parameters:
b - buffer.
Returns:
int value

concoctInt

public static int concoctInt(byte[] b,
                             int start)
Converts 4 bytes of a byte array into an int from the starting position.
Parameters:
b - buffer.
start - postion to extract data
Returns:
int value

concoctShort

public static short concoctShort(byte[] b)
Converts 2 bytes of a byte array into a short
Parameters:
b - buffer.
Returns:
int value

concoctShort

public static short concoctShort(byte[] b,
                                 int start)
Converts 2 bytes of a byte array into a short from the starting position.
Parameters:
b - buffer.
start - postion to extract data
Returns:
int value

decoct

public static byte[] decoct(short i)
Converts a short into 2 bytes.
Parameters:
i. -  
Returns:
byte array

decoct

public static byte[] decoct(int i)
Converts an int into 4 bytes.
Parameters:
i. -  
Returns:
byte array

decoct

public static byte[] decoct(long i)
Converts a long into 8 bytes.
Parameters:
i. -  
Returns:
byte array

readInt

public int readInt()
Returns the next int (4 bytes). Advance the buffer position.
Returns:
the int value.

readShort

public short readShort()
Returns the next short (2 bytes). Advances the buffer position.
Returns:
the short value.

readLong

public long readLong()
Returns the next long (8 bytes). Advances the buffer position.
Returns:
the long value.

readUnsignedShort

public int readUnsignedShort()
Returns the next unsignedshort (2 bytes). Advances the buffer position.
Returns:
the int value.

readUnsignedByte

public int readUnsignedByte()
Returns the next unsignedbyte (1 byte). Advances the buffer position.
Returns:
the int value.

readByte

public byte readByte()
Returns the next byte. Advances the buffer position.
Returns:
the byte value.

skipBytes

public void skipBytes(int count)
Skips over bytes. Advances the buffer position.
Parameters:
count - bytes to skip.

read

public int read(byte[] b)
Read a buffer full of bytes. If there are not enough bytes in the ByteIterator the buffer will not be filled.
Parameters:
b - buffer to fill.
Returns:
number of bytes read.

read

public int read(byte[] b,
                int offset,
                int count)
Read bytes into a buffer. If there are not enough bytes in the ByteIterator the requested number of bytes will not be filled.
Parameters:
b - buffer to fill.
offset - position in the buffer to start filling.
count - number of bytes to read.
Returns:
number of bytes read.

seek

public void seek(long posn)
Absolute position to move to in the data buffer.
Parameters:
posn - position

writeInt

public void writeInt(int i)
Write an int to the buffer at the current position.
Parameters:
i - value to write.

write

public int write(byte[] b)
Write a byte array to the buffer at the current position.
Parameters:
b - buffer to write.

length

public long length()
Returns the current length of the data buffer.
Returns:
length of buffer.
See Also:
extend(int)

extend

public byte[] extend(int diff)
Extends data buffer (may be set longer or shorter). If the caller constructed the data buffer with newBuf == false the caller *MUST* reassign the original buffer to the returned value In other words if you're using the original buffer to work in, this reassigns the contents. Will throw exception if the resulting buffer size is negative.

byte b[] = new byte[100];
ByteIterator bi = new ByteIterator(b, false)
file.read(b);
b = bi.extend(100); // Extended by 100 bytes to 200, we must get the new buffer back.
b = bi.extend(-110); // Shortened by 110 bytes to 90.
Parameters:
diff - difference in size (negative or positive)
Returns:
new buffer.
See Also:
getBuffer()

readLine

public java.lang.String readLine()
Read a line from the byte stream. Lines are terminated by EOF, '\n', or '\r' or both '\n' and '\r' in any order. These characters are not returned. Advances position in buffer.
Returns:
the string read or null if at the end of file.

dump

public java.lang.String dump(int start,
                             int length)
Dump part of the byte stream. This is done in the customary way, suitable for framing or wrapping fish.
 6D 69 73 63 3B 0D 0A 0D - 0A 69 6D 70 6F 72 74 20   misc;....import
 
Does not advance the buffer position.
Parameters:
start - start position
length - length to dump
Returns:
String containing the String representation of the dump.

compile

public void compile(java.lang.String pattern)
Compiles the text pattern for searching.
Parameters:
pattern - What we're looking for.

search

public int search(int start,
                  int length)
Search for the compiled pattern in the given text. A side effect of the search is the notion of a partial match at the end of the searched buffer. This partial match is helpful in searching text files when the entire file doesn't fit into memory.
Parameters:
text - Buffer containing the text
start - Start position for search
length - Length of text in the buffer to be searched.
Returns:
position in buffer where the pattern was found.
See Also:
partialMatch()

partialMatch

public int partialMatch()
Returns the position at the end of the text buffer where a partial match was found.

In many case where a full text search of a large amount of data precludes access to the entire file or stream the search algorithm will note where the final partial match occurs. After an entire buffer has been searched for full matches calling this method will reveal if a potential match appeared at the end. This information can be used to patch together the partial match with the next buffer of data to determine if a real match occurred.

Returns:
-1 the number of bytes that formed a partial match, -1 if no partial match