FileDocCategorySizeDatePackage
BTGOEPConnection.javaAPI DocphoneME MR2 API (J2ME)5595Wed May 02 18:00:28 BST 2007com.sun.midp.io.j2me.btgoep

BTGOEPConnection

public class BTGOEPConnection extends Object implements com.sun.kvem.jsr082.obex.ObexTransport
Provides underlying stream connection used as transport by shared obex implementation.

Fields Summary
private javax.microedition.io.StreamConnection
sock
private InputStream
is
private OutputStream
os
Constructors Summary
protected BTGOEPConnection(javax.microedition.io.StreamConnection sock)

        this.sock = sock;
        is = sock.openInputStream();
        os = sock.openOutputStream();
    
Methods Summary
public voidclose()
Closes connection as well as the input stream and the output stream openning for this connection.

throws
IOException if I/O error.

        IOException ioe = null;

        try {
            is.close();
        } catch (IOException e) {
            ioe = e;
        }

        try {
            os.close();
        } catch (IOException e) {
            ioe = e;
        }

        try {
            sock.close();
        } catch (IOException e) {
            ioe = e;
        }

        // catch IOException if any of the above call has thrown one
        if (ioe != null) {
            throw ioe;
        }
    
private final intdecodeLength16(byte[] buffer, int off)

        return ((((int)buffer[off]) & 0xFF) << 8)
            + (((int)buffer[off + 1]) & 0xFF);
    
public intgetMaximumPacketSize()
Determines the amount of data (maximum packet size) that can be successfully sent in a single write operation. If the size of data is greater than the maximum packet size, then then only the first maximum packet size bytes of the packet are sent, and the rest will be discarded.

return
the maximum number of bytes that can be sent/received in a single call to read()/ write() without losing any data.

        return Configuration.getIntProperty(
                    "obex.packetLength.max", 4096);
    
public javax.microedition.io.ConnectiongetUnderlyingConnection()
Get underlying connection.

        return sock;
    
public intread(byte[] inData)
Reads the packet data into specified buffer.

If the specified buffer length is 0, then 0 data will be read into this buffer, and the rest of packet data is lost.

param
inData the data array to fill with received bytes.
exception
IOException if a local or remote connection is closed or I/O error has happen.
exception
NullPointerException if the specified buffer is null.

        readFully(inData, 0, 3); // read header
        int packetLength = decodeLength16(inData, 1);
        if (packetLength < 3 || packetLength > inData.length) {
            throw new IOException("protocol error");
        }

        readFully(inData, 3, packetLength - 3);
        return packetLength;
    
private final voidreadFully(byte[] array, int offset, int size)
Reads up to len bytes of data from the input stream into an array of bytes.

param
array the buffer into which the data is read.
param
offset the start offset in array b at which the data is written.
param
size the maximum number of bytes to read.
exception
IOException if an I/O error occurs.

        while (size != 0) {
            int count = is.read(array, offset, size);
            if (count == -1) {
                throw new IOException("read error");
            }
            offset += count;
            size -= count;
        }
    
public voidwrite(byte[] outData, int len)

param
outData the buffer with the data to be sent.
param
len the number of bytes to be sent.
exception
IOException if a local or remote connection is closed or I/O error has happen.
exception
NullPointerException if the specified buffer is null.

        os.write(outData, 0, len);
        os.flush();