FileDocCategorySizeDatePackage
ConnectionBaseAdapter.javaAPI DocJ2ME MIDP 2.025864Thu Nov 07 12:02:20 GMT 2002com.sun.midp.io

ConnectionBaseAdapter

public abstract class ConnectionBaseAdapter extends Object implements com.sun.cldc.io.ConnectionBaseInterface, StreamConnection
Protocol classes extend this class to gain some of the common functionality needed to implement a CLDC Generic Connection.

The common functionality includes:

  • Supplies the input and output stream classes for a StreamConnection
  • Limits the number of streams opened according to mode, but the limit can be overridden. Read-write allows 1 input and 1 output, write-only allows 1 output, read-only allows 1 input
  • Only "disconnects" when the connection and all streams are closed
  • Throws I/O exceptions when used after being closed
  • Provides a more efficient implementation of {@link InputStream#read(byte[], int, int)}, which is called by {@link InputStream#read()}
  • Provides a more efficient implementation of {@link OutputStream#write(byte[], int, int)}, which is called by {@link OutputStream#write(int)}

Class Relationship Diagram

author
Stephen Flores
version
3.0 9/1/2000

Fields Summary
protected boolean
connectionOpen
Flag indicating if the connection is open.
protected int
iStreams
Number of input streams that were opened.
protected int
maxIStreams
Maximum number of open input streams. Set this to zero to prevent openInputStream from giving out a stream in write-only mode.
protected int
oStreams
Number of output streams were opened.
protected int
maxOStreams
Maximum number of output streams. Set this to zero to prevent openOutputStream from giving out a stream in read-only mode.
protected int
requiredPermission
The security permission required by a subclass. -1 for none.
protected String
protocol
Protocol name of subclass to prefix name of resource, can be null.
private boolean
permissionChecked
The subclass needs know that the permission occured.
Constructors Summary
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. This classes implementation always returns 0. It is up to subclasses to override this method.

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

        return 0;
    
public voidcheckForPermission(java.lang.String name)
Check for the required permission, if needed.

param
name name of resource to insert into the permission question
exception
IOInterruptedException if another thread interrupts the calling thread while this method is waiting to preempt the display.

        Scheduler scheduler;
        MIDletSuite midletSuite;

        if (permissionChecked) {
            return;
        }

        permissionChecked = true;

        if (requiredPermission == -1) {
            return;
        }
            
        scheduler = Scheduler.getScheduler();
        midletSuite = scheduler.getMIDletSuite();

        // there is no suite running when installing from the command line
        if (midletSuite != null) {
            if (protocol != null) {
                name = protocol + ":" + name;
            }

            try {
                midletSuite.checkForPermission(requiredPermission, name);
            } catch (InterruptedException ie) {
                throw new InterruptedIOException(
                    "Interrupted while trying to ask the user permission");
            }
        }
    
public voidclose()
Close the connection.

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

        if (connectionOpen) {
            connectionOpen = false;
            closeCommon();
        }
    
voidcloseCommon()
Disconnect if the connection and all the streams and the closed.

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

        if (!connectionOpen && iStreams == 0 && oStreams == 0) {
            disconnect();
        }
    
protected voidcloseInputStream()
Called once by each child input stream. If the input stream is marked open, it will be marked closed and the if the connection and output stream are closed the disconnect method will be called.

exception
IOException if the subclass throws one

        iStreams--;
        closeCommon();
    
protected voidcloseOutputStream()
Called once by each child output stream. If the output stream is marked open, it will be marked closed and the if the connection and input stream are closed the disconnect method will be called.

exception
IOException if the subclass throws one

        oStreams--;
        closeCommon();
    
protected abstract voidconnect(java.lang.String name, int mode, boolean timeouts)
Connect to a target.

param
name URL for the connection, without the protocol part
param
mode I/O access mode, see {@link Connector}
param
timeouts flag to indicate that the called wants timeout exceptions
exception
IllegalArgumentException If a parameter is invalid.
exception
ConnectionNotFoundException If the connection cannot be found.
exception
IOException If some other kind of I/O error occurs.

protected abstract voiddisconnect()
Free up the connection resources.

exception
IOException if an I/O error occurs.

protected voidensureOpen()
Check if the connection is open.

exception
IOException is thrown, if the stream is not open.

        if (!connectionOpen) {
            throw new IOException("Connection closed");
        }
    
protected voidflush()
Forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written that have been buffered by the connection, should immediately be written to their intended destination.

The flush method of ConnectionBaseAdapter does nothing.

exception
IOException if an I/O error occurs.

    
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 java.io.InputStreamopenInputStream()
Returns an input stream.

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

        InputStream i;

        ensureOpen();

        if (maxIStreams == 0) {
	    throw new IOException("no more input streams available");
        }
        
        i = new BaseInputStream(this);
        maxIStreams--;
        iStreams++;
        return i;
    
public java.io.OutputStreamopenOutputStream()
Returns an output stream.

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

        OutputStream o;

        ensureOpen();

        if (maxOStreams == 0) {
	    throw new IOException("no more output streams available");
        }

        o = new BaseOutputStream(this);
        maxOStreams--;
        oStreams++;
        return o;
    
public ConnectionopenPrim(java.lang.String name, int mode, boolean timeouts)
Check for required permission and open a connection to a target.

param
name URL for the connection, without the without the protocol part
param
mode I/O access mode, see {@link Connector}
param
timeouts flag to indicate that the caller wants timeout exceptions
return
this Connection object
exception
IllegalArgumentException If a parameter is invalid.
exception
ConnectionNotFoundException If the connection cannot be found.
exception
IOException If some other kind of I/O error occurs.

        checkForPermission(name);

        switch (mode) {
        case Connector.READ:
        case Connector.WRITE:
        case Connector.READ_WRITE:
            break;

        default:
            throw new IllegalArgumentException("Illegal mode");
        }

        connect(name, mode, timeouts);
        connectionOpen = true;
        return this;
    
public ConnectionopenPrim(SecurityToken token, java.lang.String name, int mode, boolean timeouts)
Check for the required permission and open a connection to a target. This method can be used with permissions greater than the current MIDlet suite.

param
token security token of the calling class
param
name URL for the connection, without the without the protocol part
param
mode I/O access mode, see {@link Connector}
param
timeouts flag to indicate that the caller wants timeout exceptions
return
this Connection object
exception
IllegalArgumentException If a parameter is invalid.
exception
ConnectionNotFoundException If the connection cannot be found.
exception
IOException If some other kind of I/O error occurs.

        if (requiredPermission != -1) {
            token.checkIfPermissionAllowed(requiredPermission);
        }

        permissionChecked = true;

        return openPrim(name, mode, timeouts);
    
public ConnectionopenPrim(SecurityToken token, java.lang.String name)
Check for required permission and open a connection to a target. This method can be used with permissions greater than the current MIDlet suite. Assume read/write and no timeouts.

param
token security token of the calling class
param
name URL for the connection, without the without the protocol part
return
this Connection object
exception
IllegalArgumentException If a parameter is invalid.
exception
ConnectionNotFoundException If the connection cannot be found.
exception
IOException If some other kind of I/O error occurs.

        return openPrim(token, name, Connector.READ_WRITE, false);
    
protected abstract intreadBytes(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.

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.

protected voidverifyPermissionCheck()
Lets a subclass know that the security check was done. Called by the connect method of subclasses that require permission.

This method is needed since the security check must be put off until connection since the name of the resource must be presented to the user in the permission question. An attacker could directly construct a in a subclass of the protocol and then call its connect method. A subclass can detect this attack by calling this method.

exception
SecurityException if the check did not happen.


                                                                                                
       
        if (permissionChecked) {
            return;
        }

        throw new SecurityException("The permission check was bypassed");
    
protected abstract 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 native code is done here to allow for simple asynchronous native code to be written. Not all implementations work this way (they block in the native code) but the same Java code works for both.

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.