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

📄 protocol.java

📁 有关j2me的很好的例子可以研究一下
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * Reads up to <code>len</code> bytes of data from the input stream into     * an array of bytes.     * <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 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.     */    synchronized public int read(byte b[], int off, int len)        throws IOException {        ensureOpen();        if (eof) {            return -1;        }        if (b == null) {            throw new NullPointerException();        }        if (len == 0) {            return 0;        }        if (len <0) {            throw new IndexOutOfBoundsException();        }        for (;;) {            int count = read1(b, off, len);            if (parent == null) {                throw new InterruptedIOException();            }            if (count != 0) {                if (count < 0) {                    eof = true;                }                return count;            }            GeneralBase.iowait(); /* Wait a while for I/O to become ready */        }    }   /*    * read1    */    protected int read1(byte b[], int off, int len) throws IOException {        if (parent != null) {            return parent.read0(b, off, len);        } else {            return -1;        }    }    /**     * 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.     *     * @return     the number of bytes that can be read from this input stream.     * @exception  IOException  if an I/O error occurs.     */    synchronized public int available() throws IOException {        ensureOpen();        return parent.available0();    }    /**     * Close the stream.     *     * @exception  IOException  if an I/O error occurs     */    public void close() throws IOException {        if (parent != null) {            ensureOpen();            parent.realClose();            parent.isopen = false;            parent = null;        }    }}/** * Input stream for the connection */class PrivateInputStreamWithBuffer extends PrivateInputStream {    /**     * The internal buffer array where the data is stored.     * When necessary, it may be replaced by another array     * of a different size.     */    protected byte buf[];    /**     * The index one greater than the index of the last valid     * byte in the buffer.     * This value is always in the range     * <code>0</code> through <code>buf.length</code>;     * elements <code>buf[0]</code> through <code>buf[count-1]     * </code>contain buffered input data obtained     * from the underlying input stream.     */    protected int count;    /**     * The current position in the buffer. This is the index     * of the next character to be read from the     * <code>buf</code> array.     * <p>     * This value is always in the range <code>0</code>     * through <code>count</code>. If it is less     * than <code>count</code>, then <code>buf[pos]</code>     * is the next byte to be supplied as input;     * if it is equal to <code>count</code>, then     * the  next <code>read</code> or <code>skip</code>     * operation will require more bytes to be     * read from the contained input stream.     */    protected int pos;    /**     * Constructor     * @param pointer to the connection object     *     * @exception  IOException  if an I/O error occurs.     */    public PrivateInputStreamWithBuffer(Protocol parent, int bufferSize)        throws IOException {        super(parent);        buf = new byte[bufferSize];    }   /*    * read1    */    protected int read1(byte b[], int off, int len) throws IOException {        if (count == 0) {            if (len >= buf.length) {                return super.read1(b, off, len);            } else {                pos = 0;                int res = super.read1(buf, pos, buf.length);                if (res <= 0) {                    return res;                } else {                    count = res;                }            }        }        if (len > count) {            len = count;        }        System.arraycopy(buf, pos, b, off, len);        count -= len;        pos   += len;        return len;    }    /**     * 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.     *     * @return     the number of bytes that can be read from this     *             input stream.     * @exception  IOException  if an I/O error occurs.     */    public synchronized int available() throws IOException {        ensureOpen();        return count + super.available();    }}/** * Output stream for the connection */class PrivateOutputStream extends OutputStream {    /**     * Pointer to the connection     */    protected Protocol parent;    /**     * Single byte buffer.     */    byte[] bytebuf;    /**     * Constructor     * @param pointer to the connection object     *     * @exception  IOException  if an I/O error occurs.     */    public PrivateOutputStream(Protocol parent) throws IOException {        this.parent = parent;    }    /**     * Check the stream is open     *     * @exception  IOException  if it is not.     */    void ensureOpen() throws IOException {        if (parent == null) {            throw new IOException("Stream closed");        }    }    /**     * Writes the specified byte 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 <code>byte</code>.     * @exception  IOException  if an I/O error occurs. In particular,     *             an <code>IOException</code> may be thrown if the     *             output stream has been closed.     */    synchronized public void write(int b) throws IOException {        if (bytebuf == null) {            bytebuf = new byte[1];        }        bytebuf[0] = (byte)b;        write(bytebuf, 0, 1);    }    /**     * 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.     * @exception  IOException  if an I/O error occurs. In particular,     *             an <code>IOException</code> is thrown if the output     *             stream is closed.     */    synchronized public void write(byte b[], int off, int len)        throws IOException {        ensureOpen();        if (b == null) {            throw new NullPointerException();        }        if (len == 0) {            return;        }        int n = 0;        while (true) {            int count = write1(b, off + n, len - n);            n += count;            if (n == len) {                break;            }            if (count == 0) {                GeneralBase.iowait(); /* Wait a while for I/O to become ready */            }        }    }   /**    * write1    */    protected int write1(byte b[], int off, int len) throws IOException {        if (parent == null) {            throw new InterruptedIOException();        }        return parent.write0(b, off, len);    }    /**     * Close the stream.     *     * @exception  IOException  if an I/O error occurs     */    public void close() throws IOException {        if (parent != null) {            ensureOpen();            flush();            parent.realClose();            parent.osopen = false;            parent = null;        }    }}

⌨️ 快捷键说明

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