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 int
read()
Reads the next byte of data from the COM port.int
read(byte[] buf)
Reads some number of bytes from the COM port and stores them into the buffer arraybuf
.int
read(byte[] buf, int off, int len)
Reads up tolen
bytes from the COM port and stores them into the buffer arraybuf
.long
skip(long nn)
Skips over and discardsnn
bytes 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 anint
in the range0
to255
. If an I/O error is detected, like a device disconnection, the value-1
is returned. This method blocks until input data is available or an error is detected.- Specified by:
read
in classjava.io.InputStream
- Returns:
- The next byte of data, or
-1
if 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
-1
is 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:
read
in classjava.io.InputStream
- Parameters:
buf
- The buffer into which the data is read.- Returns:
- The total number of bytes read into the buffer, or
-1
if an error was detected. The native driver will also return-1
if no data is available after the 200ms timeout. - Throws:
java.lang.NullPointerException
- ifbuf
isnull
-
read
public final int read(byte[] buf, int off, int len)Reads up tolen
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
-1
is 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:
read
in classjava.io.InputStream
- Parameters:
buf
- The buffer into which the data is read.off
- The start offset in arraybuf
at which the data is written.len
- The maximum number of bytes to read.- Returns:
- The total number of bytes read into the buffer, or
-1
if an error was detected. The native driver will also return-1
if no data is available after the 200ms timeout. - Throws:
java.lang.NullPointerException
- ifbuf
isnull
java.lang.IndexOutOfBoundsException
- ifoff
is negative,len
is negative, orlen
is greater thanbuf.length - off
-
skip
public final long skip(long nn)Skips over and discardsnn
bytes of data from the COM port. Theskip
method may end up skipping over some smaller number of bytes, possibly0
. This may result due to an I/O error beforenn
bytes have been skipped. The actual number of bytes skipped is returned. Ifnn
is negative, no bytes are skipped.- Overrides:
skip
in classjava.io.InputStream
- Parameters:
nn
- The number of bytes to be skipped.- Returns:
- The actual number of bytes skipped. Can be
0
.
-