FileDocCategorySizeDatePackage
Protocol.javaAPI DocphoneME MR2 API (J2ME)15754Wed May 02 17:59:56 BST 2007com.sun.cldc.io.j2me.socket

Protocol

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

Fields Summary
int
handle
Socket object used by native code
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
Constructors Summary
Methods Summary
protected static native intavailable0(int handle)

public synchronized voidclose()
Close the connection.

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

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

voidensureOpen()
Ensure connection is open

        if (!copen) {
            throw new IOException(
/* #ifdef VERBOSE_EXCEPTIONS */
/// skipped                       "Connection closed"
/* #endif */
            );
        }
    
public voidopen(java.lang.String name, int mode, boolean timeouts)
Open the connection


            
           
         
    
        throw new RuntimeException(
/* #ifdef VERBOSE_EXCEPTIONS */
/// skipped                   "Should not be called"
/* #endif */
        );
    
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 static native intopen0(byte[] hostname, int port, int mode)

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(
/* #ifdef VERBOSE_EXCEPTIONS */
/// skipped                       "Connection not open for reading"
/* #endif */
            );
        }
        if (isopen) {
            throw new IOException(
/* #ifdef VERBOSE_EXCEPTIONS */
/// skipped                       "Input stream already opened"
/* #endif */
            );
        }
        isopen = true;
        InputStream in = new PrivateInputStream(this);
        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(
/* #ifdef VERBOSE_EXCEPTIONS */
/// skipped                       "Connection not open for writing"
/* #endif */
            );
        }
        if (osopen) {
            throw new IOException(
/* #ifdef VERBOSE_EXCEPTIONS */
/// skipped                       "Output stream already opened"
/* #endif */
            );
        }
        osopen = true;
        OutputStream 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. It must be in this format: "//:"
param
mode read/write mode of the connection (currently ignored).
param
timeouts A flag to indicate that the called wants timeout exceptions (currently ignored).

        if (!name.startsWith("//")) {
            throw new IOException(
/* #ifdef VERBOSE_EXCEPTIONS */
/// skipped                       "bad socket connection name: " + name
/* #endif */
            );
        }
        int i = name.indexOf(':");
        if (i < 0) {
            throw new IOException(
/* #ifdef VERBOSE_EXCEPTIONS */
/// skipped                       "bad socket connection name: port missing"
/* #endif */
            );
        }
        String hostname = name.substring(2, i);
        int port;
        try {
            port = Integer.parseInt(name.substring(i+1));
        } catch (NumberFormatException e) {
            throw new IOException(
/* #ifdef VERBOSE_EXCEPTIONS */
/// skipped                       "bad socket connection name: bad port"
/* #endif */
            );
        }
        // cstring is always NUL terminated (note the extra byte allocated).
        // This avoids awkward char array manipulation in C code.
        byte cstring[] = new byte[hostname.length() + 1];
        for (int n=0; n<hostname.length(); n++) {
            cstring[n] = (byte)(hostname.charAt(n));
        }
        if ((this.handle = open0(cstring, port, mode)) < 0) {
            int errorCode = this.handle & 0x7fffffff;
            throw new IOException(
/* #ifdef VERBOSE_EXCEPTIONS */
/// skipped                       "connection failed: error = " + errorCode
/* #endif */
            );
        }
        opens++;
        copen = true;
        this.mode = mode;
        return this;
     
protected static native intreadBuf(int handle, byte[] b, int off, int len)

protected static native intreadByte(int handle)

synchronized voidrealClose()
Close the connection.

exception
IOException if an I/O error occurs.

        if (--opens == 0) {
             close0(this.handle);
        }
    
protected static native intwriteBuf(int handle, byte[] b, int off, int len)

protected static native intwriteByte(int handle, int b)