FileDocCategorySizeDatePackage
TCPOBEXConnection.javaAPI DocphoneME MR2 API (J2ME)6218Wed May 02 18:00:30 BST 2007com.sun.midp.io.j2me.tcpobex

TCPOBEXConnection

public class TCPOBEXConnection extends Object implements com.sun.kvem.jsr082.obex.ObexTransport
Provides TCP OBEX underlying stream connection as a transport to shared OBEX implementation.

Fields Summary
private boolean
isClosed
Indicates if this notifier has been closed.
private InputStream
in
Keeps the correspondent input stream.
private OutputStream
out
Keeps the correspondent output stream.
Constructors Summary
protected TCPOBEXConnection(com.sun.midp.security.SecurityToken securityToken, String host, int port)
Opens the underlaying tcp/socket connection and initiates the streams.

param
securityToken The security token
param
host Target host name (ip address).
param
port Target's port to connect to.
throws
IOException if any error occurs.


                                           
      
                  
        
        Protocol sp = new Protocol();
        StreamConnection conn = (StreamConnection) sp.openPrim(securityToken,
                "//" + host + ":" + port);

        // do not delay request since this delays the response.
        sp.setSocketOption(SocketConnection.DELAY, 0);
        
        in = conn.openInputStream();;
        out = conn.openOutputStream();
        conn.close();
    
protected TCPOBEXConnection(Object[] streams)
Used by notifier that opened tcp/socket connection - just copy the streams here.

param
streams An array of the streams. The first item is input stream. The second, output stream.

        in = (InputStream) streams[0];
        out = (OutputStream) streams[1];
    
Methods Summary
public voidclose()
Closes this connection.

throws
IOException if I/O error.

        synchronized (TCPOBEXConnection.class) {
            if (isClosed) {
                return;
            }
            isClosed = true;
        }
        boolean hasException = false;

        try {
            in.close();
        } catch (IOException e) {
            hasException = true;
        }

        try {
            out.close();
        } catch (IOException e) {
            hasException = true;
        }

        if (hasException) {
            throw new IOException("Can't close connection");
        }
    
private final intdecodeLength16(byte[] buffer, int off)
Interprets two bytes located in buffer with off offset as packet length.

param
buffer to read length from
param
off the offset in buffer
return
the packet length

        return ((((int)buffer[off]) & 0xFF) << 8)
            + (((int)buffer[off + 1]) & 0xFF);
    
public intgetMaximumPacketSize()
Gets maximum packet size.

return
The maximum packet size to be used by obex implementation.

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

return
Always returns null.

        return null;
    
public intread(byte[] inData)
Reading obex packet.

param
inData the buffer to read packet from
return
the packet size

        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 size bytes of data from the connection into an array of bytes.

param
array the buffer into which the data is read.
param
offset the start offset in array array at which the data is written.
param
size the number of bytes to read.

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

param
outData the buffer to write packet to
param
len the packet size

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