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

📄 protocol.java

📁 用于移动设备上的java虚拟机源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * @(#)Protocol.java	1.22 02/09/20 @(#) * * Copyright (c) 1999-2002 Sun Microsystems, Inc.  All rights reserved. * PROPRIETARY/CONFIDENTIAL * Use is subject to license terms. */package com.sun.midp.io.j2me.socket;import java.io.*;import javax.microedition.io.*;import com.sun.cldc.io.GeneralBase;import com.sun.midp.main.Configuration;import com.sun.midp.io.*;import com.sun.midp.midlet.*;import com.sun.midp.security.*;/** Connection to the J2ME socket API. */public class Protocol extends NetworkConnectionBase        implements SocketConnection {    /** Size of the read ahead buffer, default is no buffering. */    protected static int bufferSize;    /**     * Class initializer     */    static {        /* See if a read ahead / write behind buffer size has been specified */        String size = Configuration.getProperty(                          "com.sun.midp.io.j2me.socket.buffersize");        if (size != null) {            try {                bufferSize = Integer.parseInt(size);            } catch (NumberFormatException ex) {}        }    }    /** Hostname */    private String host;    /** TCP port */    private int port;    /** Shutdown output flag, true if output has been shutdown. */    private boolean outputShutdown;    /** Creates a buffered TCP client connection. */    public Protocol() {        // use the default buffer size        super(bufferSize);        // When asking permission use Internet protocol name.        protocol = "TCP";        requiredPermission = Permissions.TCP;    }    /**     * Open a client or server socket connection.     * <p>     * The name string for this protocol should be:     * "socket://&lt;name or IP number&gt;:&lt;port number&gt;     * <p>     * We allow "socket://:nnnn" to mean an inbound server socket connection.     *     * @param name       the target for the connection     * @param mode       I/O access mode     * @param timeouts   a flag to indicate that the caller wants     *                   timeout exceptions     *     * @return client or server TCP socket connection     *     * @exception  IOException  if an I/O error occurs.     * @exception  ConnectionNotFoundException  if the host cannot be connected     *              to     * @exception  IllegalArgumentException  if the name is malformed     */    public Connection openPrim(String name, int mode, boolean timeouts)        throws IOException {        HttpUrl url;        if (name.charAt(0) != '/' || name.charAt(1) != '/') {            throw new IllegalArgumentException(                      "Protocol must start with \"//\"");        }        url = new HttpUrl("socket", name); // parse name into host and port        /*         * Since we reused the HttpUrl parser, we must make sure that         * there was nothing past the authority in the URL.         */        if (url.path != null || url.query != null || url.fragment != null) {            throw new IllegalArgumentException("Malformed address");        }        host = url.host;        port = url.port;                /*         * If 'host' == null then we are a server endpoint at         * port 'port'.         */        if (host != null) {            // this will call the connect method which uses the host and port            return super.openPrim(name, mode, timeouts);        }        // We allow "socket://:nnnn" to mean an inbound TCP server socket.        com.sun.midp.io.j2me.serversocket.Socket con;        con = new com.sun.midp.io.j2me.serversocket.Socket();        con.open(port);        return con;    }    /**     * Connect to a server.     * @param name       the target for the connection     * @param mode       I/O access mode     * @param timeouts   a flag to indicate that the caller wants     *                   timeout exceptions     * <p>     * The name string for this protocol should be:     * "socket://&lt;name or IP number&gt;:&lt;port number&gt;     * @exception  IOException  if an I/O error occurs.     * @exception  ConnectionNotFoundException  if the host cannot be connected     *              to     * @exception  IllegalStateException  if there is no hostname     * @exception  IllegalArgumentException  if the name is malformed     */    public void connect(String name, int mode, boolean timeouts)        throws IOException {        byte[] szHost;        verifyPermissionCheck();        /*         * The host and port were set by overridding the openPrim method of         * our super class.         */        if (port < 0) {            throw new IllegalArgumentException("Missing port number");        }        szHost = Util.toCString(host);        open0(szHost, port);        registerCleanup();    }    /**     * Create a Java connection object from an open TCP socket.     * This method is only used by com.sun.midp.io.j2me.serversocket.Socket;     *     * @param handle an already formed socket handle     */    public void open(int handle) {        this.handle = handle;        try {            connectionOpen = true;            checkForPermission(getAddress());        } catch (Exception e) {            connectionOpen = false;            if (e instanceof IOException) {                e = new SecurityException("Unknown TCP client");            }            try {                close0();            } catch (IOException ioe) {                // ignore            }            throw (RuntimeException)e;        }                registerCleanup();    }    /**     * Disconnect from the server.     *     * @exception  IOException  if an I/O error occurs.     */    public void disconnect() throws IOException {        /*         * Only shutdown or close of the sending side of a connection is         * defined in the TCP spec.         *         * The receiver can only abort (reset) the entire connection to stop         * the a sender from sending. InputStream close already causes         * an reads to fail so no native action is needed.         *         * Shutdown the output gracefully closes the sending side of the          * TCP connection by sending all pending data and the FIN flag.         */        if (!outputShutdown) {            shutdownOutput0();        }        close0();    }    /**     * 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.     * Sets the <code>eof</code> field of the connection when the native read     * returns -1.     *     * @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 nonBufferedRead(byte b[], int off, int len)        throws IOException {        int bytesRead;        for (;;) {            try {                bytesRead = read0(b, off, len);            } finally {                if (iStreams == 0) {                    throw new InterruptedIOException("Stream closed");                }            }            if (bytesRead == -1) {                eof = true;                return -1;            }            if (bytesRead != 0) {                return bytesRead;            }            /* Wait a while for I/O to become ready */            GeneralBase.iowait();         }    }    /**     * 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.     *     * @return     the number of bytes that can be read from this input stream     *             without blocking.     * @exception  IOException  if an I/O error occurs.     */    public int available() throws IOException {        if (count > 0) {            /*             * The next read will only return the bytes in the buffer,             * so only return the number of bytes in the buffer.             * While available can return a number less than than the next             * read will get, it should not return more.             */            return count;        }

⌨️ 快捷键说明

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