Package com.celer

Class COMInputStream

java.lang.Object
java.io.InputStream
com.celer.COMInputStream
All Implemented Interfaces:
java.io.Closeable, java.lang.AutoCloseable

public class COMInputStream
extends java.io.InputStream
The COM input stream can be used to read from a COM port.

CelerSMS

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 array buf.
    int read​(byte[] buf, int off, int len)
    Reads up to len bytes from the COM port and stores them into the buffer array buf.
    long skip​(long nn)
    Skips over and discards nn bytes of data from the COM port.

    Methods inherited from class java.io.InputStream

    available, close, mark, markSupported, nullInputStream, readAllBytes, readNBytes, readNBytes, reset, skipNBytes, transferTo

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • read

      public final int read()
      Reads the next byte of data from the COM port. The value byte is returned as an int in the range 0 to 255. 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 class java.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 array buf. 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 into buf.

      The first byte read is stored into element buf[0], the next one into buf[1], and so on. The number of bytes read is, at most, equal to the length of buf.

      The read(buf) method has the same effect as read(buf, 0, buf.length)

      Overrides:
      read in class java.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 - if buf is null
    • read

      public final int read​(byte[] buf, int off, int len)
      Reads up to len bytes from the COM port and stores them into the buffer array buf. 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 into buf.

      The first byte read is stored into element buf[0], the next one into buf[1], and so on. The number of bytes read is, at most, equal to len.

      Overrides:
      read in class java.io.InputStream
      Parameters:
      buf - The buffer into which the data is read.
      off - The start offset in array buf 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 - if buf is null
      java.lang.IndexOutOfBoundsException - if off is negative, len is negative, or len is greater than buf.length - off
    • skip

      public final long skip​(long nn)
      Skips over and discards nn bytes of data from the COM port. The skip method may end up skipping over some smaller number of bytes, possibly 0. This may result due to an I/O error before nn bytes have been skipped. The actual number of bytes skipped is returned. If nn is negative, no bytes are skipped.
      Overrides:
      skip in class java.io.InputStream
      Parameters:
      nn - The number of bytes to be skipped.
      Returns:
      The actual number of bytes skipped. Can be 0.