connectionbaseadapter.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 1,034 行 · 第 1/3 页

JAVA
1,034
字号
/* * @(#)ConnectionBaseAdapter.java	1.7 06/10/10 *  * Copyright  1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER *  * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation.  *  * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt).  *  * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA  *  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions.  */package com.sun.cdc.io;import com.sun.cdc.io.ConnectionBaseInterface;import com.sun.cdc.io.GeneralBase;//import com.sun.midp.midlet.*;//import com.sun.midp.security.*;import java.io.*;import javax.microedition.io.*;/** * Protocol classes extend this class to gain some of the common functionality * needed to implement a CDC Generic Connection. * <p> * The common functionality includes:</p> * <ul> * <li>Supplies the input and output stream classes for a StreamConnection</li> * <li>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</li> * <li>Only "disconnects" when the connection and all streams are closed</li> * <li>Throws I/O exceptions when used after being closed</li> * <li>Provides a more efficient implementation of * {@link InputStream#read(byte[], int, int)}, which is called by * {@link InputStream#read()} * <li>Provides a more efficient implementation of * {@link OutputStream#write(byte[], int, int)}, which is called by * {@link OutputStream#write(int)} * </ul> * <p align="center"> * <b>Class Relationship Diagram</b></p> * <p align="center"> * <img src="doc-files/ConnectionBaseAdapter.gif" border=0></p> * * @version 3.0 9/1/2000 */public abstract class ConnectionBaseAdapter implements ConnectionBaseInterface,    StreamConnection {    /** Flag indicating if the connection is open. */    protected boolean connectionOpen = false;    /** Number of input streams that were opened. */    protected static int iStreams = 0;    /**     * Maximum number of open input streams. Set this     * to zero to prevent openInputStream from giving out a stream in     * write-only mode.     */    protected int maxIStreams = 1;    /** Number of output streams were opened. */    protected static int oStreams = 0;    /**     * Maximum number of output streams. Set this     * to zero to prevent openOutputStream from giving out a stream in     * read-only mode.     */    protected int maxOStreams = 1;    /**     * 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.     */    public Connection openPrim(String name, int mode, boolean timeouts)            throws IOException {	///        checkForPermission(null, name);	// Give the subclass a chance to check	///	checkForPermission(name);	// Give the subclass a chance to check        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;    }    /**     * Overridden by Protocols to check for permissions.     * This implementation always throws a security exception.     * The subclass is responsible for checking permissions and     * maintaining the state (in private local fields) as to whether     * it was granted.      *     * @param token security token of the calling class or null     * @param name the URL of the connection without the protocol     *     * @exception SecurityException if permissions are not granted     * @exception InterruptedIOException if I/O associated with permissions is interrupted     */    ///    protected void checkForPermission(SecurityToken token, String name)    protected void checkForPermission()	throws SecurityException, InterruptedIOException    {	throw new SecurityException("Permission not granted");    }    /**     * Utility method to check for the required permission, and handle     * prompts, etc.  A SecurityToken may be supplied, in which case     * the permission must be allowed by the token. If the token is     * <code>null</code> then the permission is checked in the current     * app.  If there is no app then the permission     * is allowed.  A SecurityException is thrown when the permission     * is not granted.     *     * @param token security token of the calling class or null     * @param requiredPermission the permission that is needed     * @param name resource to insert into the permission question     * @param protocol the protocol string used in the resource name     *     * @exception SecurityException if the permission is not granted     * @exception InterruptedIOException if another thread interrupts the     *   calling thread while this method is waiting to preempt the     *   display.     */    ///    protected final void checkForPermission(SecurityToken token, 	    ///					    int requiredPermission,	    ///			    String name, String protocol)	    ///	    protected final void checkForPermission()	    ///	throws InterruptedIOException    ///{	/// If a security token was supplied, use it for the check	///	if (token != null) {	///    token.checkIfPermissionAllowed(requiredPermission);	///    return;	///}		///        Scheduler scheduler;	///        MIDletSuite midletSuite;	///        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");	    ///    }	    ///}    ///}    /**     * Check for the required permission and open a connection to a target.     * This method can be used with permissions greater than     * the current app.     *      * @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.     *////    public Connection openPrim(SecurityToken token, String name, int mode,    ///                               boolean timeouts) throws IOException {    ///        checkForPermission(token, name);	///    public Connection openPrim(String name, int mode,	///			       boolean timeouts) throws IOException {	///            checkForPermission();	///        return openPrim(name, mode, timeouts);	///    }    /**     * Check for required permission and open a connection to a target.     * This method can be used with permissions greater than     * the current app. 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.     *////    public Connection openPrim(SecurityToken token, String name)	///            throws IOException {	///        return openPrim(token, name, Connector.READ_WRITE, false);    public Connection openPrim(String name)            throws IOException {        return openPrim(name, Connector.READ_WRITE, false);    }    /**     * 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.     */    public InputStream openInputStream() throws IOException {        InputStream i;        ensureOpen();        /* Fix for CR 6246819: Comment out MIDP code that limits streams so           that multiple streams are supported for CDC */         /*if (maxIStreams == 0) {          throw new IOException("no more input streams available");          }*/                i = new BaseInputStream(this);        //maxIStreams--;        iStreams++;        return i;    }    /**     * Open and return a data input stream for a connection.     *     * @return                 An input stream     * @exception IOException  If an I/O error occurs     */    public DataInputStream openDataInputStream() throws IOException {        return new DataInputStream(openInputStream());    }    /**     * 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.     */    public OutputStream openOutputStream() throws IOException {        OutputStream o;        ensureOpen();        /* Fix for CR 6246819: Comment out MIDP code that limits streams so           that multiple streams are supported for CDC */         /*if (maxOStreams == 0) {          throw new IOException("no more output streams available");          }*/        o = new BaseOutputStream(this);        //maxOStreams--;        oStreams++;        return o;    }    /**     * Open and return a data output stream for a connection.     *     * @return                 An input stream     * @exception IOException  If an I/O error occurs     */    public DataOutputStream openDataOutputStream() throws IOException {        return new DataOutputStream(openOutputStream());    }    /**     * Close the connection.     *     * @exception  IOException  if an I/O error occurs when closing the     *                          connection.     */    public void close() throws IOException {        if (connectionOpen) {            connectionOpen = false;            closeCommon();        }    }    /**     * 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     */    protected void closeInputStream() throws IOException {        if (iStreams>0) {            iStreams--;            closeCommon();        }    }    /**     * 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

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?