FileDocCategorySizeDatePackage
Connector.javaAPI DocphoneME MR2 API (J2ME)14297Wed May 02 17:59:54 BST 2007javax.microedition.io

Connector

public class Connector extends Object
This class is a placeholder for the static methods that are used for creating all the Connection objects.

The creation of Connections is performed dynamically by looking up a protocol implementation class whose name is formed from the platform name (read from a system property) and the protocol name of the requested connection (extracted from the parameter string supplied by the application programmer.) The parameter string that describes the target should conform to the URL format as described in RFC 2396. This takes the general form:

{scheme}:[{target}][{parms}]

where {scheme} is the name of a protocol such as http}.

The {target} is normally some kind of network address.

Any {parms} are formed as a series of equates of the form ";x=y". Example: ";type=a".

An optional second parameter may be specified to the open function. This is a mode flag that indicates to the protocol handler the intentions of the calling code. The options here specify if the connection is going to be read (READ), written (WRITE), or both (READ_WRITE). The validity of these flag settings is protocol dependent. For instance, a connection for a printer would not allow read access, and would throw an IllegalArgumentException. If the mode parameter is not specified, READ_WRITE is used by default.

An optional third parameter is a boolean flag that indicates if the calling code can handle timeout exceptions. If this flag is set, the protocol implementation may throw an InterruptedIOException when it detects a timeout condition. This flag is only a hint to the protocol handler, and it does not guarantee that such exceptions will actually be thrown. If this parameter is not set, no timeout exceptions will be thrown.

Because connections are frequently opened just to gain access to a specific input or output stream, four convenience functions are provided for this purpose. See also: {@link DatagramConnection DatagramConnection} for information relating to datagram addressing

version
1.1 1/7/2000
version
1.2 12/8/2000 (comments revised)

Fields Summary
public static final int
READ
Access mode READ.
public static final int
WRITE
Access mode WRITE.
public static final int
READ_WRITE
Access mode READ_WRITE.
private static String
platform
The platform name.
private static String
classRoot
The root of the classes.
Constructors Summary
private Connector()
Prevent instantiation of this class.


           
     
        /* Set up the platform name */
        platform = System.getProperty("microedition.platform");
        if (platform == null) {
            platform = "j2me";
        }

        /* See if there is an alternate protocol class root path */
        classRoot = System.getProperty("javax.microedition.io.Connector.protocolpath");
        if (classRoot == null) {
            classRoot = "com.sun.cldc.io";
        }
    
Methods Summary
public static Connectionopen(java.lang.String name)
Create and open a Connection.

param
name The URL for the connection.
return
A new Connection object.
exception
IllegalArgumentException If a parameter is invalid.
exception
ConnectionNotFoundException If the requested connection cannot be make, or the protocol type does not exist.
exception
IOException If some other kind of I/O error occurs.

        return open(name, READ_WRITE);
    
public static Connectionopen(java.lang.String name, int mode)
Create and open a Connection.

param
name The URL for the connection.
param
mode The access mode.
return
A new Connection object.
exception
IllegalArgumentException If a parameter is invalid.
exception
ConnectionNotFoundException If the requested connection cannot be make, or the protocol type does not exist.
exception
IOException If some other kind of I/O error occurs.


        return open(name, mode, false);
    
public static Connectionopen(java.lang.String name, int mode, boolean timeouts)
Create and open a Connection.

param
name The URL for the connection
param
mode The access mode
param
timeouts A flag to indicate that the caller wants timeout exceptions
return
A new Connection object
exception
IllegalArgumentException If a parameter is invalid.
exception
ConnectionNotFoundException if the requested connection cannot be make, or the protocol type does not exist.
exception
IOException If some other kind of I/O error occurs.

        try {
            return openPrim(name, mode, timeouts);
        } catch(ClassNotFoundException x ) {
        }

        throw new ConnectionNotFoundException(
/* #ifdef VERBOSE_EXCEPTIONS */
/// skipped                   "The requested protocol does not exist "+name
/* #endif */
            );
    
public static java.io.DataInputStreamopenDataInputStream(java.lang.String name)
Create and open a connection input stream.

param
name The URL for the connection.
return
A DataInputStream.
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.


        InputConnection con = null;
        try {
            con = (InputConnection)Connector.open(name, Connector.READ);
        } catch (ClassCastException e) {
            throw new IOException(
/* #ifdef VERBOSE_EXCEPTIONS */
/// skipped                       e.toString()
/* #endif */
            );
        }

        try {
            return con.openDataInputStream();
        } finally {
            con.close();
        }
    
public static java.io.DataOutputStreamopenDataOutputStream(java.lang.String name)
Create and open a connection output stream.

param
name The URL for the connection.
return
A DataOutputStream.
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.


        OutputConnection con = null;
        try {
            con = (OutputConnection)Connector.open(name, Connector.WRITE);
        } catch (ClassCastException e) {
            throw new IOException(
/* #ifdef VERBOSE_EXCEPTIONS */
/// skipped                       e.toString()
/* #endif */
            );
        }

        try {
            return con.openDataOutputStream();
        } finally {
            con.close();
        }
    
public static java.io.InputStreamopenInputStream(java.lang.String name)
Create and open a connection input stream.

param
name The URL for the connection.
return
An InputStream.
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 openDataInputStream(name);
    
public static java.io.OutputStreamopenOutputStream(java.lang.String name)
Create and open a connection output stream.

param
name The URL for the connection.
return
An OutputStream.
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 openDataOutputStream(name);
    
private static ConnectionopenPrim(java.lang.String name, int mode, boolean timeouts)
Create and open a Connection.

param
string The URL for the connection
param
mode The access mode
param
timeouts A flag to indicate that the caller wants timeout exceptions
return
A new Connection object
exception
ClassNotFoundException If the protocol cannot be found.
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.
exception
IllegalArgumentException If a parameter is invalid.


        /* Test for null argument */
        if (name == null) {
            throw new IllegalArgumentException(
/* #ifdef VERBOSE_EXCEPTIONS */
/// skipped                       "Null URL"
/* #endif */
            );
        }

        /* Look for : as in "http:", "file:", or whatever */
        int colon = name.indexOf(':");

        /* Test for null argument */
        if (colon < 1) {
            throw new IllegalArgumentException(
/* #ifdef VERBOSE_EXCEPTIONS */
/// skipped                       "no ':' in URL"
/* #endif */
            );
        }

        try {
            String protocol;

            /* Strip off the protocol name */
            protocol = name.substring(0, colon);

            /* sanity check the protocol name */
            char[] chars = protocol.toCharArray();
            for (int i = 0; i < chars.length; ++i) {
                char c = chars[i];
                /* only allow characters that are valid in RFC 2396
                   alpha *( alpha | digit | "+" | "-" | "." )
                */
                if ( ('A" <= c && c <= 'Z") ||
                     ('a" <= c && c <= 'z") ||
                     ( (i > 0) && (
                         ('0" <= c && c <= '9") ||
                         c == '+" ||
                         c == '-" ||
                         c == '."))) {
                    continue;
                }
                throw new IllegalArgumentException("Invalid protocol name");
            }

            /* Strip the protocol name from the rest of the string */
            name = name.substring(colon+1);

            /* Use the platform and protocol names to look up */
            /* a class to implement the connection */
            Class clazz =
                Class.forName(classRoot +
                              "." + platform +
                              "." + protocol + ".Protocol");

            /* Construct a new instance */
            ConnectionBaseInterface uc =
                (ConnectionBaseInterface)clazz.newInstance();

            /* Open the connection, and return it */
            return uc.openPrim(name, mode, timeouts);

        } catch (InstantiationException x) {
            throw new IOException(
/* #ifdef VERBOSE_EXCEPTIONS */
/// skipped                       x.toString()
/* #endif */
            );
        } catch (IllegalAccessException x) {
            throw new IOException(
/* #ifdef VERBOSE_EXCEPTIONS */
/// skipped                       x.toString()
/* #endif */
            );
        } catch (ClassCastException x) {
            throw new IOException(
/* #ifdef VERBOSE_EXCEPTIONS */
/// skipped                       x.toString()
/* #endif */
            );
        }