Class StreamLink

java.lang.Object
com.codename1.io.link.ByteLink
com.codename1.io.link.StreamLink
Direct Known Subclasses:
TcpLink

public class StreamLink extends ByteLink
A link over a pair of streams that are already connected, such as a Bluetooth socket's. Reads wait by polling available(), so the input stream must report it; most socket streams do.
  • Constructor Details

  • Method Details

    • setStreams

      protected void setStreams(InputStream in, OutputStream out)

      Supplies the streams once connected; for subclasses.

      Parameters
      • in: the stream from the device

      • out: the stream to the device

    • open

      public void open() throws IOException
      Description copied from class: ByteLink

      Connects. Does nothing when already open.

      Throws
      • IOException: when the device cannot be reached
      Specified by:
      open in class ByteLink
      Throws:
      IOException
    • isOpen

      public boolean isOpen()
      Description copied from class: ByteLink

      Whether ByteLink.open() succeeded and ByteLink.close() has not been called.

      Returns

      true while open

      Specified by:
      isOpen in class ByteLink
    • write

      public void write(byte[] data, int offset, int length) throws IOException
      Description copied from class: ByteLink

      Writes bytes, returning once they are handed to the device.

      Parameters
      • data: the buffer

      • offset: where to start in it

      • length: how many bytes

      Throws
      • IOException: when the link is closed or broken
      Specified by:
      write in class ByteLink
      Throws:
      IOException
    • read

      public int read(byte[] buffer, int offset, int length, int timeout) throws IOException
      Description copied from class: ByteLink

      Reads what has arrived, waiting up to timeout milliseconds for the first byte.

      Parameters
      • buffer: where to put the bytes

      • offset: where to start in it

      • length: at most how many bytes

      • timeout: how long to wait, in milliseconds

      Returns

      the number of bytes read; 0 when nothing arrived in time

      Throws
      • IOException: when the link is closed or broken, including when the other side closed it
      Specified by:
      read in class ByteLink
      Throws:
      IOException
    • close

      public void close()
      Description copied from class: ByteLink
      Disconnects. Safe to call more than once, and from any thread; a read blocked in another thread fails soon after.
      Specified by:
      close in class ByteLink