FileDocCategorySizeDatePackage
DataL2CAPReaderWriter.javaAPI DocphoneME MR2 API (J2ME)5587Wed May 02 18:00:32 BST 2007com.sun.kvem.jsr082.bluetooth

DataL2CAPReaderWriter

public class DataL2CAPReaderWriter extends DataElementSerializer
Logical Link Control and Adaptation Protocol connection reader/writer.

Fields Summary
private javax.bluetooth.L2CAPConnection
con
The L2CAP connection to read from or write to.
private long
readSize
Actual size of the read buffer.
Constructors Summary
DataL2CAPReaderWriter(javax.bluetooth.L2CAPConnection con)
Constructs reader-and-writer object for the given connection.

param
con the L2CAP connection to create reader-writer for.


                         
        
        this.con = con;
        writeBuffer = new byte[con.getTransmitMTU()];
        readBuffer = new byte[con.getReceiveMTU()];
    
Methods Summary
voidflush()
Flushes write buffer to the conection.

        if ((writePos < 0) || (writePos >= writeBuffer.length)) {
            throw new IndexOutOfBoundsException();
        }
        if (writePos == 0) {
            return;
        }
        byte[] tempBuffer = new byte[(int)writePos];
        System.arraycopy(writeBuffer, 0, tempBuffer, 0, (int)writePos);
        con.send(tempBuffer);
        writePos = 0;
    
bytereadByte()
Reads 1-byte value from the connection.

return
byte recieved.

        if ((readPos == 0) || (readPos == readSize)) {
            if ((readSize = con.receive(readBuffer)) == 0) {
                throw new IOException("Empty packet is received");
            }
            readPos = 0;
        } else if ((readPos < 0) || (readPos > readSize)) {
            throw new IndexOutOfBoundsException();
        }
        
        if (readSize == -1) {
            throw new IOException("Reached end of stream");
        }
        
        byte data = readBuffer[(int)readPos];
        readPos++;
        return data;
    
byte[]readBytes(int size)
Reads given number of bytes from the connection.

param
size number of bytes to read.
return
array of bytes read.

        byte[] data = new byte[size];
        int dataPos = 0;

        if ((readPos == 0) || (readPos == readSize)) {
            readSize = con.receive(readBuffer);
            readPos = 0;
        } else if ((readPos < 0) || (readPos > readSize)) {
            throw new IndexOutOfBoundsException();
        }

        while ((data.length - dataPos) > (readSize - readPos)) {
            int length = (int) (readSize - readPos);
            System.arraycopy(readBuffer, (int)readPos, data, (int)dataPos,
                             length);
            dataPos += length;
            readSize = con.receive(readBuffer);
            readPos = 0;
        }
        int length = data.length - dataPos;
        System.arraycopy(readBuffer, (int)readPos, data, (int)dataPos,
                         length);
        readPos += length;
        return data;
    
voidwriteByte(long data)
Writes 1-byte data to the connection.

param
data byte value to write.

        if ((writePos < 0) || (writePos >= writeBuffer.length)) {
            throw new IndexOutOfBoundsException();
        }
        writeBuffer[(int)writePos] = (byte)data;
        writePos++;
        if (writePos == writeBuffer.length) {
            con.send(writeBuffer);
            writePos = 0;
        }
    
voidwriteBytes(byte[] data)
Writes given data to the connection.

param
data bytes to write.

        if ((writePos < 0) || (writePos >= writeBuffer.length)) {
            throw new IndexOutOfBoundsException();
        }
        int dataPos = 0;

        while ((data.length - dataPos) >= ((writeBuffer.length - writePos))) {
            int length = writeBuffer.length - ((int) writePos);
            System.arraycopy(data, (int) dataPos, writeBuffer, (int) writePos,
                             (int) length);
            con.send(writeBuffer);
            writePos = 0;
            dataPos += length;
        }
        int length = data.length - dataPos;

        if (length > 0) {
            System.arraycopy(data, (int) dataPos, writeBuffer, (int) writePos,
                             (int) length);
        }
        writePos += length;