⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connectionbaseadapter.java

📁 有关j2me的很好的例子可以研究一下
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* *  Copyright (c) 2000-2001 Sun Microsystems, Inc., 901 San Antonio Road, *  Palo Alto, CA 94303, U.S.A.  All Rights Reserved. * *  Sun Microsystems, Inc. has intellectual property rights relating *  to the technology embodied in this software.  In particular, and *  without limitation, these intellectual property rights may include *  one or more U.S. patents, foreign patents, or pending *  applications.  Sun, Sun Microsystems, the Sun logo, Java, KJava, *  and all Sun-based and Java-based marks are trademarks or *  registered trademarks of Sun Microsystems, Inc.  in the United *  States and other countries. * *  This software is distributed under licenses restricting its use, *  copying, distribution, and decompilation.  No part of this *  software may be reproduced in any form by any means without prior *  written authorization of Sun and its licensors, if any. * *  FEDERAL ACQUISITIONS:  Commercial Software -- Government Users *  Subject to Standard License Terms and Conditions */package com.sun.midp.io;import com.sun.cldc.io.*;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)} * <li>Implements {@link InputStream#available()} when a subclass * overrides {@link #readBytesNonBlocking(byte[], int, int)}</li> * </ul> * <p align="center"> * <b>Class Relationship Diagram</b></p> * <p align="center"> * <img src="doc-files/ConnectionBaseAdapter.gif" border=0></p> * * @author  Stephen Flores * @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. */    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 */    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;    /**     * 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 {        switch (mode) {        case Connector.READ:            maxOStreams = 0;            break;        case Connector.WRITE:            maxIStreams = 0;            break;        case Connector.READ_WRITE:            break;        default:            throw new IllegalArgumentException("Illegal mode");        }        connect(name, mode, timeouts);        connectionOpen = true;        return this;    }                    /**     * 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("write-only connection");        }        if (iStreams == maxIStreams) {	    throw new IOException("no more input streams available");        }                i = new BaseInputStream(this);        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("read-only connection");        }        if (oStreams == maxOStreams) {	    throw new IOException("no more output streams available");        }                o = new BaseOutputStream(this);        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();        }    }    /**     * Disconnect if the connection and all the streams and the closed.     *     * @exception  IOException  if an I/O error occurs when closing the     *                          connection.     */    void closeCommon() throws IOException {        if (!connectionOpen && iStreams == 0 && oStreams == 0) {            disconnect();        }    }    /**     * Check if the stream is open.     *     * @exception  IOException  is thrown, if the stream is not open.     */    protected void ensureOpen() throws IOException {        if (!connectionOpen) {            throw new IOException("Connection closed");        }    }    /**     * 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 void connect(String name, int mode, boolean timeouts)        throws IOException;    /**     * 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;    /**     * Reads up to <code>len</code> bytes of data from the input stream into     * an array of bytes, but does not block if no bytes available. A subclass     * should implement this to so the available method on the InputStream     * will be useful.     * <p>     * The <code>readBytesNonBlocking</code> method of     * <code>ConnectionBaseAdapter</code> does nothing and returns 0.     *     * @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 int readBytesNonBlocking(byte b[], int off, int len)        throws IOException {        return 0;    }    /**     * Writes <code>len</code> bytes from the specified byte array     * starting at offset <code>off</code> to this output stream.     * <p>     * 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 <code>IOException</code> is thrown if the output     *             stream is closed.     */    protected abstract int writeBytes(byte b[], int off, int len)        throws IOException;    /**     * Forces any buffered output bytes to be written out.     * The general contract of <code>flush</code> 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.     * <p>     * The <code>flush</code> method of <code>ConnectionBaseAdapter</code>     * does nothing.     *     * @exception  IOException  if an I/O error occurs.     */    protected void flush() throws IOException {    }}/** * Input stream for the connection */

⌨️ 快捷键说明

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