FileDocCategorySizeDatePackage
Protocol.javaAPI DocJ2ME MIDP 2.017909Thu Nov 07 12:02:22 GMT 2002com.sun.midp.io.j2me.socket

Protocol

public class Protocol extends NetworkConnectionBase implements SocketConnection
Connection to the J2ME socket API.

Fields Summary
protected static int
bufferSize
Size of the read ahead buffer, default is no buffering.
private String
host
Hostname
private int
port
TCP port
private boolean
outputShutdown
Shutdown output flag, true if output has been shutdown.
Constructors Summary
public Protocol()
Creates a buffered TCP client connection.

        // use the default buffer size
        super(bufferSize);

        // When asking permission use Internet protocol name.
        protocol = "TCP";
        requiredPermission = Permissions.TCP;
    
Methods Summary
public intavailable()
Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream. The next caller might be the same thread or another thread.

return
the number of bytes that can be read from this input stream without blocking.
exception
IOException if an I/O error occurs.

        if (count > 0) {
            /*
             * The next read will only return the bytes in the buffer,
             * so only return the number of bytes in the buffer.
             * While available can return a number less than than the next
             * read will get, it should not return more.
             */
            return count;
        }

        // The buffer is empty, so the next read will go directly to native
        return available0();
    
private native intavailable0()
Get the number of bytes that can be read without blocking, accesses the handle field.

return
number of bytes that can be read without blocking
exception
IOException if an I/O error occurs.

private voidcheckOption(byte option)
Check a socket option to make sure it's a valid option.

param
option socket option identifier (KEEPALIVE, LINGER, SNDBUF, RCVBUF, or DELAY)
exception
IllegalArgumentException if the value is not valid (e.g. negative value)
see
#getSocketOption
see
#setSocketOption

	if (option == SocketConnection.KEEPALIVE 
	    || option == SocketConnection.LINGER 
	    || option == SocketConnection.SNDBUF 
	    || option == SocketConnection.RCVBUF 
	    || option == SocketConnection.DELAY) {
	    return;
	}
	throw new IllegalArgumentException("Unsupported Socket Option");
	    
    
private native voidclose0()
Close the connection, accesses the handle field.

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

protected voidcloseOutputStream()
Called once by the child output stream. The output side of the socket will be shutdown and then the parent method will be called.

exception
IOException if the subclass throws one

        /*
         * Shutdown the output gracefully closes the sending side of the 
         * TCP connection by sending all pending data and the FIN flag.
         */
        shutdownOutput0();
        outputShutdown = true;
        super.closeOutputStream();
    
public voidconnect(java.lang.String name, int mode, boolean timeouts)
Connect to a server.

param
name the target for the connection
param
mode I/O access mode
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>

exception
IOException if an I/O error occurs.
exception
ConnectionNotFoundException if the host cannot be connected to
exception
IllegalStateException if there is no hostname
exception
IllegalArgumentException if the name is malformed


        byte[] szHost;

        verifyPermissionCheck();

        /*
         * The host and port were set by overridding the openPrim method of
         * our super class.
         */

        if (port < 0) {
            throw new IllegalArgumentException("Missing port number");
        }

        szHost = Util.toCString(host);
        open0(szHost, port);
        registerCleanup();
    
public voiddisconnect()
Disconnect from the server.

exception
IOException if an I/O error occurs.

        /*
         * Only shutdown or close of the sending side of a connection is
         * defined in the TCP spec.
         *
         * The receiver can only abort (reset) the entire connection to stop
         * the a sender from sending. InputStream close already causes
         * an reads to fail so no native action is needed.
         *
         * Shutdown the output gracefully closes the sending side of the 
         * TCP connection by sending all pending data and the FIN flag.
         */

        if (!outputShutdown) {
            shutdownOutput0();
        }

        close0();
    
private native voidfinalize()
Native finalizer

public java.lang.StringgetAddress()
Gets the remote address to which the socket is bound. The address can be either the remote host name or the IP address(if available).

return
the remote address to which the socket is bound.
exception
IOException if the connection was closed

	ensureOpen();
	return getHost0(false);
    
private native java.lang.StringgetHost0(boolean local)
Get the requested IP number.

param
local truefalsefor the remote host
return
the IP address as a String

public java.lang.StringgetLocalAddress()
Gets the local address to which the socket is bound.

The host address(IP number) that can be used to connect to this end of the socket connection from an external system. Since IP addresses may be dynamically assigned a remote application will need to be robust in the face of IP number reasssignment.

The local hostname (if available) can be accessed from System.getProperty("microedition.hostname")

return
the local address to which the socket is bound.
exception
IOException if the connection was closed
see
ServerSocketConnection

	ensureOpen();
	return getHost0(true);
    
public intgetLocalPort()
Returns the local port to which this socket is bound.

return
the local port number to which this socket is connected.
exception
IOException if the connection was closed
see
ServerSocketConnection

	ensureOpen();
	return getPort0(true); 
    
public intgetPort()
Returns the remote port to which this socket is bound.

return
the remote port number to which this socket is connected.
exception
IOException if the connection was closed

	ensureOpen();
	return getPort0(false); 
    
private native intgetPort0(boolean local)
Get the requested port number.

param
local truefalsefor the remote host
return
the port number of the requested end point

private native intgetSockOpt0(int option)
Get the requested socket option.

param
option socket option to retrieve
return
value of the socket option

public intgetSocketOption(byte option)
Get a socket option for the connection.

param
option socket option identifier (KEEPALIVE, LINGER, SNDBUF, RCVBUF, or DELAY)
return
positive numeric value for specified option or -1 if the value is not available.
exception
IllegalArgumentException if the option identifier is not valid
exception
IOException if the connection was closed
see
#setSocketOption

	checkOption(option);
	ensureOpen();
	return getSockOpt0(option);
    
protected intnonBufferedRead(byte[] b, int off, int len)
Reads up to len bytes of data from the input stream into an array of bytes, blocks until at least one byte is available. Sets the eof field of the connection when the native read returns -1.

param
b the buffer into which the data is read.
param
off the start offset in array b at which the data is written.
param
len the maximum number of bytes to read.
return
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
exception
IOException if an I/O error occurs.


        int bytesRead;

        for (;;) {
            try {
                bytesRead = read0(b, off, len);
            } finally {
                if (iStreams == 0) {
                    throw new InterruptedIOException("Stream closed");
                }
            }

            if (bytesRead == -1) {
                eof = true;
                return -1;
            }

            if (bytesRead != 0) {
                return bytesRead;
            }

            /* Wait a while for I/O to become ready */
            GeneralBase.iowait(); 
        }
    
public voidopen(int handle)
Create a Java connection object from an open TCP socket. This method is only used by com.sun.midp.io.j2me.serversocket.Socket;

param
handle an already formed socket handle

        this.handle = handle;

        try {
            connectionOpen = true;
            checkForPermission(getAddress());
        } catch (Exception e) {
            connectionOpen = false;

            if (e instanceof IOException) {
                e = new SecurityException("Unknown TCP client");
            }

            try {
                close0();
            } catch (IOException ioe) {
                // ignore
            }

            throw (RuntimeException)e;
        }
        
        registerCleanup();
    
private native voidopen0(byte[] szHost, int port)
Connect to a server and fillin the handle field.

param
szHost host as a zero terminated ASCII string
param
port TCP port at host
exception
IOException if an I/O error occurs.

public ConnectionopenPrim(java.lang.String name, int mode, boolean timeouts)
Open a client or server socket connection.

The name string for this protocol should be: "socket://<name or IP number>:<port number>

We allow "socket://:nnnn" to mean an inbound server socket connection.

param
name the target for the connection
param
mode I/O access mode
param
timeouts a flag to indicate that the caller wants timeout exceptions
return
client or server TCP socket connection
exception
IOException if an I/O error occurs.
exception
ConnectionNotFoundException if the host cannot be connected to
exception
IllegalArgumentException if the name is malformed


        HttpUrl url;
        if (name.charAt(0) != '/" || name.charAt(1) != '/") {
            throw new IllegalArgumentException(
                      "Protocol must start with \"//\"");
        }

        url = new HttpUrl("socket", name); // parse name into host and port

        /*
         * Since we reused the HttpUrl parser, we must make sure that
         * there was nothing past the authority in the URL.
         */
        if (url.path != null || url.query != null || url.fragment != null) {
            throw new IllegalArgumentException("Malformed address");
        }

        host = url.host;
        port = url.port;
        
        /*
         * If 'host' == null then we are a server endpoint at
         * port 'port'.
         */

        if (host != null) {
            // this will call the connect method which uses the host and port
            return super.openPrim(name, mode, timeouts);
        }

        // We allow "socket://:nnnn" to mean an inbound TCP server socket.
        com.sun.midp.io.j2me.serversocket.Socket con;
        con = new com.sun.midp.io.j2me.serversocket.Socket();
        con.open(port);
        return con;
    
private native intread0(byte[] b, int off, int len)
Read from the socket, accesses the handle field.

param
b the buffer into which the data is read.
param
off the start offset in array b at which the data is written.
param
len the maximum number of bytes to read.
return
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
exception
IOException if an I/O error occurs.

private native voidregisterCleanup()
Register with the native cleanup code, accesses the handle field.

private native voidsetSockOpt0(int option, int value)
Set the requested socket option.

param
option socket option to set
param
value of the socket option

public voidsetSocketOption(byte option, int value)
Set a socket option for the connection.

Options inform the low level networking code about intended usage patterns that the application will use in dealing with the socket connection.

param
option socket option identifier (KEEPALIVE, LINGER, SNDBUF, RCVBUF, or DELAY)
param
value numeric value for specified option (must be positive)
exception
IllegalArgumentException if the value is not valid (e.g. negative value)
exception
IOException if the connection was closed
see
#getSocketOption

	checkOption(option);
	if (value < 0) {
	    throw new IllegalArgumentException("Unsupported Socket Option");
	}
	ensureOpen();

	setSockOpt0(option, value);
    
private native voidshutdownOutput0()
Shutdown the output side of the connection.

private native intwrite0(byte[] b, int off, int len)
Write to the socket, accesses the handle field.

param
b the buffer of the data to write
param
off the start offset in array b at which the data is written.
param
len the number of bytes to write.
return
the total number of bytes written
exception
IOException if an I/O error occurs.

public intwriteBytes(byte[] b, int off, int len)
Writes len bytes from the specified byte array starting at offset off to this output stream.

Polling the will be done by our super class.

param
b the data.
param
off the start offset in the data.
param
len the number of bytes to write.
return
number of bytes written
exception
IOException if an I/O error occurs. In particular, an IOException is thrown if the output stream is closed.

        return write0(b, off, len);