connectionbaseadapter.jpp

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

JPP
892
字号
/* * 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.midp.io;// #ifdef ENABLE_CDCimport com.sun.cdc.io.ConnectionBaseInterface;// #elseimport com.sun.cldc.io.ConnectionBaseInterface;// #endifimport com.sun.midp.midlet.*;import java.io.*;import javax.microedition.io.*;/** * Protocol classes extend this class to gain some of the common functionality * needed to implement a CLDC 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 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 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;    /**     * Initialize the StreamConnection and return it.     *     * @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 abstract Connection openPrim(String name, int mode,                                        boolean timeouts) throws IOException;    /**     * Check the mode argument and initialize the StreamConnection.     * Any permissions checks should be     * checked before this method is called because the TCK expects the     * security check to fail before the arguments are called even though     * the spec does not mandate it.     *     * @param mode             I/O access mode, see {@link Connector}     *     * @exception IllegalArgumentException If a parameter is invalid.     * @exception IOException  If some other kind of I/O error occurs.     */    public void initStreamConnection(int mode) throws IOException {        switch (mode) {        case Connector.READ:        case Connector.WRITE:        case Connector.READ_WRITE:            break;        default:            throw new IllegalArgumentException("Illegal mode");        }        connectionOpen = true;    }    /**     * 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();        if (maxIStreams == 0) {            throw new IOException("no more input streams available");        }        i = new BufferedInputStream(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();        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 {        iStreams--;        notifyClosedInput();        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     * method will be called.     *     * @exception IOException if the subclass throws one     */    protected void closeOutputStream() throws IOException {        oStreams--;        notifyClosedOutput();        closeCommon();    }    /**     * Disconnect if the connection and all the streams are closed.     *     * @exception  IOException  if an I/O error occurs when closing the     *                          connection.     */    void closeCommon() throws IOException {        if (!connectionOpen && iStreams == 0 && oStreams == 0) {            disconnect();        }    }    /**     * Notify blocked Java threads waiting for an input data     * that all InputStream instances of the connection are closed     */    protected void notifyClosedInput() {};    /**     * Notify blocked Java threads trying to output data     * that all OutputStream instances of the connection are closed      */    protected void notifyClosedOutput() {};    /**     * Check if the connection is open.     *     * @exception  IOException  is thrown, if the stream is not open.     */    protected void ensureOpen() throws IOException {        if (!connectionOpen) {            throw new IOException("Connection closed");        }    }    /**     * Free up the connection resources.     *     * @exception  IOException  if an I/O error occurs.     */    protected abstract void disconnect() throws IOException;    /**     * Reads up to <code>len</code> 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 <code>b</code>     *                   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     *             <code>-1</code> if there is no more data because the end of     *             the stream has been reached.     * @exception  IOException  if an I/O error occurs.     */    protected abstract int readBytes(byte b[], int off, int len)        throws IOException;    /**     * 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

⌨️ 快捷键说明

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