FileDocCategorySizeDatePackage
BytePack.javaAPI DocphoneME MR2 API (J2ME)4123Wed May 02 18:00:30 BST 2007com.sun.midp.jsr82emul

BytePack

public class BytePack extends Object
Utility class that packs various data to resores from byte array. An instance refers to byte array that keeps packed data and passed to the instance thru either constructor or reset() method.

Fields Summary
protected int
offset
Current offset in byte array
protected byte[]
buffer
Byte array that keeps packed data
Constructors Summary
public BytePack(byte[] buffer)
Constructs an instance with given byte array.

param
buffer byte array to either unpak data from or pack into.

        reset(buffer);
    
public BytePack()
Constructs an instance.

Methods Summary
public voidappend(byte b)
Appends byte to packed data.

param
b byte to append.

        buffer[offset++] = b;
    
public voidappendBytes(byte[] bytes)
Appends given bytes to packed data.

param
bytes bytes to append.

        System.arraycopy(bytes, 0, buffer, offset, bytes.length);
        offset += bytes.length;
    
public voidappendInt(int value)
Appends a 4-byte rpresentation of given integer value to packed data. The value can be restored by extractInt() method.

param
value value to append

        for (int i = 0; i < 4; i++) {
            buffer[offset++] = (byte)value; 
            value >>= 8;
        }
    
public byteextract()
Extracts next byte from packed data.

return
byte extracted.

        return buffer[offset++];
    
public byte[]extractBytes(int length)
Extracts next requested amount of bytes from packed data.

param
length amount of bytes to extract
return
(refernce to) newly created byte array that contains bytes extracted.

        byte[] bytes = new byte[length];
        System.arraycopy(buffer, offset, bytes, 0, length);
        offset += length;
        return bytes;
    
public intextractInt()
Extracts integer value previously stored by appendInt() from byte represnetation.

return
value extracted

        int value = 0;
        for (int i = 0; i < 4; i++) {
            value |= (buffer[offset++] & 0xff) << (8 * i);
        }
        return value;
    
public byte[]release()
Removes internal reference to current byte array, returning the reference outside.

return
(reference to) byte array processed.

        byte[] tmp = buffer;
        buffer = null;
        return tmp;
    
public voidreset(byte[] buffer)
Resets state, setting packed data destination to given array and current offset to 0.

param
buffer new byte array to either unpak data from or pack into.

        offset = 0;
        this.buffer = buffer;
    
public voidreset()
Resets state, setting current offset to 0.

        offset = 0;