FileDocCategorySizeDatePackage
Protocol.javaAPI DocJ2ME CLDC 1.117525Wed Feb 05 15:56:00 GMT 2003com.sun.cldc.io.j2me.socket

Protocol

public class Protocol extends Object implements ConnectionBaseInterface, StreamConnection
Connection to the J2ME socket API.
author
Nik Shaylor
author
Efren Serra
version
1.0 1/16/2000

Fields Summary
int
handle
Socket object used by native code
int
iocb
Private variable the native code uses
private int
mode
Access mode
int
opens
Open count
private boolean
copen
Connection open flag
protected boolean
isopen
Input stream open flag
protected boolean
osopen
Output stream open flag
public static int
bufferSize
Size of the read ahead / write behind buffers
Constructors Summary
Methods Summary
protected native intavailable0()

public synchronized voidclose()
Close the connection.

exception
IOException if an I/O error occurs when closing the connection.

        if (copen) {
            copen = false;
            realClose();
        }
    
protected native voidclose0()

voidensureOpen()
Ensure connection is open

        if (!copen) {
            throw new IOException("Connection closed");
        }
    
private static native voidinitializeInternal()
Class initializer

public voidopen(java.lang.String name, int mode, boolean timeouts)
Open the connection

 /* Default is no buffering */

           
     
        initializeInternal();

        /* See if a read ahead / write behind buffer size has been specified */
        String size = System.getProperty("com.sun.cldc.io.j2me.socket.buffersize");
        if (size != null) {
            try {
                bufferSize = Integer.parseInt(size);
            } catch(NumberFormatException ex) {}
        }
    

        throw new RuntimeException("Should not be called");
    
public voidopen(int handle, int mode)
Open the connection

param
handle an already formed socket handle

This function is only used by com.sun.cldc.io.j2me.socketserver;

        this.handle = handle;
        opens++;
        copen = true;
        this.mode = mode;
    
protected native voidopen0(java.lang.String name, int mode, boolean timeouts)

public java.io.DataInputStreamopenDataInputStream()
Open and return a data input stream for a connection.

return
An input stream
exception
IOException If an I/O error occurs

        return new DataInputStream(openInputStream());
    
public java.io.DataOutputStreamopenDataOutputStream()
Open and return a data output stream for a connection.

return
An input stream
exception
IOException If an I/O error occurs

        return new DataOutputStream(openOutputStream());
    
public synchronized java.io.InputStreamopenInputStream()
Returns an input stream for this socket.

return
an input stream for reading bytes from this socket.
exception
IOException if an I/O error occurs when creating the input stream.


        ensureOpen();

        if ((mode&Connector.READ) == 0) {
            throw new IOException("Connection not open for reading");
        }

        if (isopen) {
            throw new IOException("Input stream already opened");
        }

        isopen = true;
        InputStream in;

        if (bufferSize == 0) {
            in = new PrivateInputStream(this);
        } else {
            in = new PrivateInputStreamWithBuffer(this, bufferSize);
        }

        opens++;
        return in;
    
public synchronized java.io.OutputStreamopenOutputStream()
Returns an output stream for this socket.

return
an output stream for writing bytes to this socket.
exception
IOException if an I/O error occurs when creating the output stream.

        ensureOpen();

        if ((mode&Connector.WRITE) == 0) {
            throw new IOException("Connection not open for writing");
        }

        if (osopen) {
            throw new IOException("Output stream already opened");
        }

        osopen = true;
        OutputStream os;

        os = new PrivateOutputStream(this);

        opens++;
        return os;
    
public ConnectionopenPrim(java.lang.String name, int mode, boolean timeouts)
Open the connection

param
name the target for the connection
param
writeable a flag that is true if the caller expects to write to the connection
param
timeouts a flag to indicate that the caller wants timeout exceptions

The name string for this protocol should be: "socket://<name or IP number>:<port number> We allow "socket://:nnnn" to mean "serversocket://:nnnn". The native code will spot this and signal back with an InterruptedException to tell the calling Java code about this.


        try {
            open0(name, mode, timeouts);
            registerCleanup();
            opens++;
            copen = true;
            this.mode = mode;
            return this;
        } catch(InterruptedException x) {
//            com.sun.cldc.io.j2me.serversocket.Protocol con;
//            con = new com.sun.cldc.io.j2me.serversocket.Protocol();
//            con.open(name, mode, timeouts);
//            return con;
              return null;
        }
    
protected native intread0(byte[] b, int off, int len)

synchronized voidrealClose()
Close the connection.

exception
IOException if an I/O error occurs.

        if (--opens == 0) {
             close0();
        }
    
private native voidregisterCleanup()

protected native intwrite0(byte[] b, int off, int len)