FileDocCategorySizeDatePackage
MessagePacket.javaAPI DocphoneME MR2 API (J2ME)9198Wed May 02 18:00:32 BST 2007com.sun.tck.wma.sms

MessagePacket

public class MessagePacket extends Object
A generic message packet that treats the packet as an input or output stream of data.

Fields Summary
private final int
DATAGRAM_PACKET_LENGTH
The maximum payload size for a datagram.
private int
index
The current read-write index into the data buffer (stream).
private byte[]
data
The data buffer (stream).
private boolean
firstWrite
private ByteArrayOutputStream
bos
The byte output stream, used when creating a new packet.
private DataOutputStream
dos
The data output stream, used when creating a new packet.
Constructors Summary
public MessagePacket()
Construct a new message packet that can be populated with some data.



                     
      
        firstWrite = true;
    
public MessagePacket(byte[] payload)
Construct a new message packet that contains the given payload.

param
payload The bytes to be read.


        /* Make a clone of the payload (message) bytes. */
        int length = payload.length;
        data = new byte[length];
        for (int i = 0; i < length; i++) {
            data[i] = payload[i];
        }
    
Methods Summary
private voidcheckFirstWrite()
Checks to see if the data output streams need to be opened.

        if (firstWrite == true) {
            firstWrite = false;
            bos = new ByteArrayOutputStream();
            dos = new DataOutputStream(bos);
        }
    
public byte[]getBytes(int length)
Read an array of bytes from the data stream, starting at the current index. The number of bytes to be read is specified by length. The index is advanced by length.

param
length The number of bytes to be read.
return
The buffer of bytes that were read.
exception
ArrayIndexOutOfBoundsException if reading goes beyond the end of the data stream.


        byte[] buffer = new byte[length];

        for (int i = 0; i < length; i++) {
            buffer[i] = data[index++];
        }

        return buffer;
    
public byte[]getData()
Returns the contents of the packet as a byte array.

return
The array of packet bytes.
exception
IOException if there was a problem while closing the streams.


        byte[] buffer = new byte[0];

        if (firstWrite == false) {
            dos.close();
            buffer = bos.toByteArray();
            bos.close();
        }

        return buffer;
    
public intgetInt()
Read an integer (32-bit) value from the data stream, starting at the current index.

return
The integer that was read from the data stream.
exception
ArrayIndexOutOfBoundsException if reading goes beyond the end of the data stream.

        int x1 = ((int)(data[index++] & 0xff));
        int x2 = ((int)(data[index++] & 0xff) << 8);
        int x3 = ((int)(data[index++] & 0xff) << 16);
        int x4 = ((int)(data[index++] & 0xff) << 24);
        return (x1 | x2 | x3 | x4);
    
public longgetLong()
Read a long (64-bit) value from data stream, starting at the current index.

return
The long value that was read from the data stream.
exception
ArrayIndexOutOfBoundsException if reading goes beyond the end of the data stream.

        long x1 = ((long)(data[index++] & 0xff));
        long x2 = ((long)(data[index++] & 0xff) << 8);
        long x3 = ((long)(data[index++] & 0xff) << 16);
        long x4 = ((long)(data[index++] & 0xff) << 24);
        long x5 = ((long)(data[index++] & 0xff) << 32);
        long x6 = ((long)(data[index++] & 0xff) << 40);
        long x7 = ((long)(data[index++] & 0xff) << 48);
        long x8 = ((long)(data[index++] & 0xff) << 56);
        return (x1 | x2 | x3 | x4 | x5 | x6 | x7 | x8);
    
public shortgetShort()
Read a short (16-bit) value from the data stream, starting at the current index.

return
The short that was read from the data stream.
exception
ArrayIndexOutOfBoundsException if reading goes beyond the end of the data stream.

        short x1 = ((short)(data[index++] & 0xff));
        short x2 = (short)((data[index++] & 0xff) << 8);
        return (short)(x1 | x2);
    
public java.lang.StringgetString()
Read a string from the data stream, starting at the current index. The string is assumed to be terminated by a null character.

return
The string of characters, converted to a java.lang.String object.
exception
ArrayIndexOutOfBoundsException if reading goes beyond the end of the data stream.

        int offset = index;
        int len = 0;

        /* Determine length of string */
        for (int i = index; i < data.length; i++) {
            if (data[i] == '\0") {
                break;
            }
            len++;
        }
             
        /* update index */
        index += len + 1;

        return new String(data, offset, len);
    
public voidputBytes(byte[] buf)
Write an array of bytes into the data stream, starting at the current index.

param
buf The buffer of bytes to be written into the data stream.
exception
ArrayIndexOutOfBoundsException if reading goes beyond the end of the data stream.

        checkFirstWrite();
        dos.write(buf, 0, buf.length);
    
public voidputInt(int x)
Write an integer (32-bit) value into the data stream and advance the index.

param
x The integer to be written.
exception
ArrayIndexOutOfBoundsException if reading goes beyond the end of the data stream.

        checkFirstWrite();
        dos.writeByte((byte) (x & 0xff));
        dos.writeByte((byte)((x >> 8) & 0xff));
        dos.writeByte((byte)((x >> 16) & 0xff));
        dos.writeByte((byte)((x >> 24) & 0xff));
    
public voidputLong(long x)
Write a long (64-bit) value into the data stream and advance the index.

param
x The long to be written.
exception
ArrayIndexOutOfBoundsException if reading goes beyond the end of the data stream.

        checkFirstWrite();
        dos.writeByte((byte) (x & 0xff));
        dos.writeByte((byte)((x >> 8) & 0xff));
        dos.writeByte((byte)((x >> 16) & 0xff));
        dos.writeByte((byte)((x >> 24) & 0xff));
        dos.writeByte((byte)((x >> 32) & 0xff));
        dos.writeByte((byte)((x >> 40) & 0xff));
        dos.writeByte((byte)((x >> 48) & 0xff));
        dos.writeByte((byte)((x >> 56) & 0xff));
    
public voidputShort(short x)
Write a short (16-bit) value into the data stream and advance the index.

param
x The short to be written.
exception
ArrayIndexOutOfBoundsException if reading goes beyond the end of the data stream.

        checkFirstWrite();
        dos.writeByte((byte) (x & 0xff));
        dos.writeByte((byte)((x >> 8) & 0xff));
    
public voidputString(java.lang.String s)
Write a string to the data stream, starting at the current index. The characters in the string are written, followed by a null-character terminator.

param
s The string to be written.
exception
ArrayIndexOutOfBoundsException if reading goes beyond the end of the data stream.

        checkFirstWrite();
        dos.writeBytes(s);
        dos.writeByte(0);