DataL2CAPReaderWriterpublic class DataL2CAPReaderWriter extends DataElementSerializer Logical Link Control and Adaptation Protocol connection reader/writer. |
Fields Summary |
---|
private javax.bluetooth.L2CAPConnection | conThe L2CAP connection to read from or write to. | private long | readSizeActual size of the read buffer. |
Constructors Summary |
---|
DataL2CAPReaderWriter(javax.bluetooth.L2CAPConnection con)Constructs reader-and-writer object for the given connection.
this.con = con;
writeBuffer = new byte[con.getTransmitMTU()];
readBuffer = new byte[con.getReceiveMTU()];
|
Methods Summary |
---|
void | flush()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;
| byte | readByte()Reads 1-byte value from the connection.
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.
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;
| void | writeByte(long data)Writes 1-byte data to the connection.
if ((writePos < 0) || (writePos >= writeBuffer.length)) {
throw new IndexOutOfBoundsException();
}
writeBuffer[(int)writePos] = (byte)data;
writePos++;
if (writePos == writeBuffer.length) {
con.send(writeBuffer);
writePos = 0;
}
| void | writeBytes(byte[] data)Writes given data to the connection.
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;
|
|