FileDocCategorySizeDatePackage
PacketStream.javaAPI DocJ2ME CLDC 1.16622Wed Feb 05 15:56:04 GMT 2003kdp

PacketStream

public class PacketStream extends Object

Fields Summary
final ProxyListener
proxy
private int
inCursor
final Packet
pkt
private ByteArrayOutputStream
dataStream
private boolean
isCommitted
public int
id
Constructors Summary
PacketStream(ProxyListener proxy, int cmdSet, int cmd)

  // public so proxy can get at it quickly

          
        this.proxy = proxy;
        this.pkt = new Packet();
        id = pkt.id;
        pkt.cmdSet = (short)cmdSet;
        pkt.cmd = (short)cmd;
    
PacketStream(ProxyListener proxy, int id, short flags, short errorCode)

        this.pkt = new Packet();
        this.proxy = proxy;
        this.pkt.id = id;
        this.id = id;
        this.pkt.errorCode = errorCode;
        this.pkt.flags = flags;
    
PacketStream(ProxyListener proxy, Packet p)

        this.pkt = p;
        this.proxy = proxy;
        this.id = p.id;
    
Methods Summary
bytecommand()

        return (byte)pkt.cmd;
    
intid()

        return id;
    
booleanreadBoolean()
Read boolean represented as one byte.

        byte ret = readByte();
        return (ret != 0);
    
bytereadByte()
Read byte represented as one bytes.

        byte ret = pkt.data[inCursor];
        inCursor += 1;
        return ret;
    
charreadChar()
Read char represented as two bytes.

        int b1, b2;

        b1 = pkt.data[inCursor++] & 0xff;
        b2 = pkt.data[inCursor++] & 0xff;

        return (char)((b1 << 8) + b2);
    
doublereadDouble()
Read double represented as eight bytes.

        return Double.longBitsToDouble(readLong());
    
private longreadID(int size)

        if (size == 8) {
            return readLong();
        } else {  // Other sizes???
            return readInt();
        }
    
intreadInt()
Read int represented as four bytes.

        int b1,b2,b3,b4;

        b1 = pkt.data[inCursor++] & 0xff;
        b2 = pkt.data[inCursor++] & 0xff;
        b3 = pkt.data[inCursor++] & 0xff;
        b4 = pkt.data[inCursor++] & 0xff;

        return ((b1 << 24) + (b2 << 16) + (b3 << 8) + b4);
    
longreadLong()
Read long represented as eight bytes.

        long b1,b2,b3,b4;
        long b5,b6,b7,b8;

        b1 = pkt.data[inCursor++] & 0xff;
        b2 = pkt.data[inCursor++] & 0xff;
        b3 = pkt.data[inCursor++] & 0xff;
        b4 = pkt.data[inCursor++] & 0xff;

        b5 = pkt.data[inCursor++] & 0xff;
        b6 = pkt.data[inCursor++] & 0xff;
        b7 = pkt.data[inCursor++] & 0xff;
        b8 = pkt.data[inCursor++] & 0xff;

        return ((b1 << 56) + (b2 << 48) + (b3 << 40) + (b4 << 32)
                + (b5 << 24) + (b6 << 16) + (b7 << 8) + b8);
    
shortreadShort()
Read short represented as two bytes.

        int b1, b2;

        b1 = pkt.data[inCursor++] & 0xff;
        b2 = pkt.data[inCursor++] & 0xff;

        return (short)((b1 << 8) + b2);
    
java.lang.StringreadString()
Read string represented as four byte length followed by characters of the string.

        String ret;
        int len = readInt();

        try {
            ret = new String(pkt.data, inCursor, len, "UTF8");
        } catch(IndexOutOfBoundsException e ) {
            System.err.println(e);
            ret = "IndexOutOfBoundsExceptions";
        } catch(java.io.UnsupportedEncodingException e) {
            System.err.println(e);
            ret = "Conversion error!";
        }
        inCursor += len;
        return ret;
    
voidsend()

        if (!isCommitted) {
            pkt.data = dataStream.toByteArray();
            try {
                proxy.send(pkt);
            } catch (IOException e) {
            }
            isCommitted = true;
        }
    
intskipBytes(int n)

        inCursor += n;
        return n;
    
voidwaitForReply()

        if (!isCommitted) {
            throw new Exception("waitForReply without send");
        }

        proxy.waitForReply(pkt);

        if (pkt.errorCode != Packet.ReplyNoError) {
            throw new Exception(String.valueOf(pkt.errorCode));
        }
    
voidwriteBoolean(boolean data)

        if(data) {
            dataStream.write( 1 );
        } else {
            dataStream.write( 0 );
        }
    
voidwriteByte(byte data)

        dataStream.write( data );
    
voidwriteByteArray(byte[] data)

        dataStream.write(data, 0, data.length);
    
voidwriteChar(char data)

        dataStream.write( (byte)((data >>> 8) & 0xFF) );
        dataStream.write( (byte)((data >>> 0) & 0xFF) );
    
voidwriteDouble(double data)

        writeLong(Double.doubleToLongBits(data));
    
voidwriteFloat(float data)

        writeInt(Float.floatToIntBits(data));
    
voidwriteID(int size, long data)

        if (size == 8) {
            writeLong(data);
        } else {
            writeInt((int)data);
        }
    
voidwriteInt(int data)

        dataStream.write( (byte)((data >>> 24) & 0xFF) );
        dataStream.write( (byte)((data >>> 16) & 0xFF) );
        dataStream.write( (byte)((data >>> 8) & 0xFF) );
        dataStream.write( (byte)((data >>> 0) & 0xFF) );
    
voidwriteLong(long data)

        dataStream.write( (byte)((data >>> 56) & 0xFF) );
        dataStream.write( (byte)((data >>> 48) & 0xFF) );
        dataStream.write( (byte)((data >>> 40) & 0xFF) );
        dataStream.write( (byte)((data >>> 32) & 0xFF) );

        dataStream.write( (byte)((data >>> 24) & 0xFF) );
        dataStream.write( (byte)((data >>> 16) & 0xFF) );
        dataStream.write( (byte)((data >>> 8) & 0xFF) );
        dataStream.write( (byte)((data >>> 0) & 0xFF) );
    
voidwriteShort(short data)

        dataStream.write( (byte)((data >>> 8) & 0xFF) );
        dataStream.write( (byte)((data >>> 0) & 0xFF) );
    
voidwriteString(java.lang.String string)

        try {
            byte[] stringBytes = string.getBytes("UTF8");
            writeInt(stringBytes.length);
            writeByteArray(stringBytes);
        } catch (java.io.UnsupportedEncodingException e) {
            throw new RuntimeException("Cannot convert string to UTF8 bytes");
        }