📄 protocol.java
字号:
/** * Input stream for the connection */class PrivateInputStream extends InputStream { /** * Buffer for single char reads */ byte[] buf = new byte[1]; /** * Pointer to the connection */ private Protocol parent; /** * Constructor * @param pointer to the connection object * * @exception IOException if an I/O error occurs. */ public PrivateInputStream(Protocol parent) throws IOException { this.parent = parent; } /** * Check the stream is open * * @exception IOException if the stream is not open. */ void ensureOpen() throws IOException { if (parent == null) { throw new IOException("Stream closed"); } } /** * Reads the next byte of data from the input 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. * * @return the next byte of data, or <code>-1</code> * if the end of the stream is reached. * @exception IOException if an I/O error occurs. */ synchronized public int read() throws IOException { if (read(buf, 0, 1) > 0) { return (buf[0] & 0xFF); } return -1; } /** * 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 (b == null) { throw new NullPointerException(); } else if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException(); } for(;;) { int count = read1(parent.hPort, b, off, len); if (count != 0 || parent.blocking == 0) { if (count > len) { // Is this really needed? throw new IOException("Read overrun in comm:"); } return count; } GeneralBase.iowait(); } } /* * read1 */ protected int read1(int port, byte b[], int off, int len) throws IOException { return Protocol.native_readBytes(port, b, off, len); } /** * Returns the number of bytes that can be read (or skipped over) * from this input stream without blocking 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 Protocol.native_available(parent.hPort); } /** * Close the stream * * @exception IOException if an I/O error occurs. */ public synchronized void close() throws IOException { if (parent != null) { parent.realClose(); 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(int port, byte b[], int off, int len) throws IOException { if (count == 0) { if (len >= buf.length) { return super.read1(port, b, off, len); } else { pos = 0; int res = super.read1(port, 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; }}/** * Output stream for the connection */class PrivateOutputStream extends OutputStream { /** * Pointer to the connection */ Protocol parent; /** * Buffer for single char writes */ byte[] buf = new byte[1]; /** * Constructor * * @param pointer to the parent connection */ public PrivateOutputStream(Protocol p) { parent = p; } /** * Check the stream is open * * @exception IOException if the stream is not open. */ 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 { buf[0] = (byte)b; write(buf, 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 { int test; ensureOpen(); if (len <= 0) { return; } // test the parameters so we don't crash in the native code test = b[off] + b[off + len - 1]; Protocol.native_writeBytes(parent.hPort, b, off, len); } /** * Close the stream * * @exception IOException if an I/O error occurs. */ public synchronized void close() throws IOException { if (parent != null) { parent.realClose(); parent = null; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -