Class COMInputStream
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class COMInputStream
extends java.io.InputStream
The COM input stream follows the standard Java InputStream behavior, except for the following key difference:
When using the native driver and reading into a byte buffer (refer to read(byte[]) and read(byte[],int,int))
the call will only block for a maximum of 200ms if no data is available. This is useful to avoid blocking indefinitely if the
communication device stops responding. If not a single byte of input data is available after the 200ms timeout -1 is returned
instead of 0. This was done to avoid the "underlying input stream returned zero bytes" exception when wrapping the COM
input stream into a Java buffered reader. When calling the single byte read() or the skip(long) method the operation
will block as specified in the standard Java InputStream API.
On the other hand, when using the Java RAF driver all I/O operations will block indefinitely, as specified in the standard Java InputStream API.
-
Method Summary
Modifier and Type Method Description intread()Reads the next byte of data from the COM port.intread(byte[] buf)Reads some number of bytes from the COM port and stores them into the buffer arraybuf.intread(byte[] buf, int off, int len)Reads up tolenbytes from the COM port and stores them into the buffer arraybuf.longskip(long nn)Skips over and discardsnnbytes of data from the COM port.
-
Method Details
-
read
public final int read()Reads the next byte of data from the COM port. The value byte is returned as anintin the range0to255. If an I/O error is detected, like a device disconnection, the value-1is returned. This method blocks until input data is available or an error is detected.- Specified by:
readin classjava.io.InputStream- Returns:
- The next byte of data, or
-1if no data.
-
read
public final int read(byte[] buf)Reads some number of bytes from the COM port and stores them into the buffer arraybuf. The number of bytes actually read is returned as an integer. This method blocks until input data is available or an error is detected. When using the native driver this method will block for 200ms at most.This method attempts to read at least one byte. If an error is detected the value
-1is returned. Otherwise, at least one byte is read and stored intobuf.The first byte read is stored into element
buf[0], the next one intobuf[1], and so on. The number of bytes read is, at most, equal to the length ofbuf.The
read(buf)method has the same effect asread(buf, 0, buf.length)- Overrides:
readin classjava.io.InputStream- Parameters:
buf- The buffer into which the data is read.- Returns:
- The total number of bytes read into the buffer, or
-1if an error was detected. The native driver will also return-1if no data is available after the 200ms timeout. - Throws:
java.lang.NullPointerException- ifbufisnull
-
read
public final int read(byte[] buf, int off, int len)Reads up tolenbytes from the COM port and stores them into the buffer arraybuf. The number of bytes actually read is returned as an integer. This method blocks until input data is available or an error is detected. When using the native driver this method will block for 200ms at most.This method attempts to read at least one byte. If an error is detected the value
-1is returned. Otherwise, at least one byte is read and stored intobuf.The first byte read is stored into element
buf[0], the next one intobuf[1], and so on. The number of bytes read is, at most, equal tolen.- Overrides:
readin classjava.io.InputStream- Parameters:
buf- The buffer into which the data is read.off- The start offset in arraybufat which the data is written.len- The maximum number of bytes to read.- Returns:
- The total number of bytes read into the buffer, or
-1if an error was detected. The native driver will also return-1if no data is available after the 200ms timeout. - Throws:
java.lang.NullPointerException- ifbufisnulljava.lang.IndexOutOfBoundsException- ifoffis negative,lenis negative, orlenis greater thanbuf.length - off
-
skip
public final long skip(long nn)Skips over and discardsnnbytes of data from the COM port. Theskipmethod may end up skipping over some smaller number of bytes, possibly0. This may result due to an I/O error beforennbytes have been skipped. The actual number of bytes skipped is returned. Ifnnis negative, no bytes are skipped.- Overrides:
skipin classjava.io.InputStream- Parameters:
nn- The number of bytes to be skipped.- Returns:
- The actual number of bytes skipped. Can be
0.
-
