System information
Appendix D. JAVA Example Code
D-20
public int read_int() throws Exception
{
byte[] temp = read_bytes(4);
int rtn = (((int)temp[0] & 0xff) << 24) |
(((int)temp[1] & 0xff) << 16) |
(((int)temp[2] & 0xff) << 8) |
((int)temp[3] & 0xff);
return rtn;
}
public float read_float() throws Exception
{
int int_val = read_int();
return Float.intBitsToFloat(int_val);
}
public String read_string()
{
String rtn = new String();
while(storage[read_index] != 0 && read_index < storage_len)
rtn += (char)storage[read_index++];
if(read_index < storage_len && storage[read_index] == 0)
++read_index; // increment past the terminator
return rtn;
}
public void move_past(int len)
{
if(read_index + len >= storage_len)
read_index = storage_len;
else
read_index += len;
}
public void reset()
{ read_index = 0; }
public int whats_left()
{ return storage_len - read_index; }
public void clear()
{ storage_len = read_index = 0; }
public int get_read_index()
{ return read_index; }
public byte[] get_fragment(int start_pos, int end_pos) throws
Exception
{
if(start_pos > end_pos ||
start_pos >= storage_len ||
end_pos >= storage_len ||
start_pos < 0 ||
end_pos < 0)
throw new Exception("invalid fragment position pointers");
byte[] rtn = new byte[end_pos - start_pos + 1];
for(int i = start_pos; i < end_pos; ++i)
rtn[i - start_pos] = storage[i];
return rtn;
}