Class ByteLink

java.lang.Object
com.codename1.io.link.ByteLink
Direct Known Subclasses:
MemoryLink, SerialLink, StreamLink, UsbLink

public abstract class ByteLink extends Object

A two-way byte connection to one device: a serial port, a TCP socket, a USB endpoint pair, or anything else with bytes in and out.

Calls block, so a protocol driver runs them on a thread of its own and hands results to the EDT with CN.callSerially. Every blocking call refuses to run on the EDT, where it would freeze the app.

ByteLink link = new TcpLink("192.168.1.50", 9100);
new Thread(() -> {
    try {
        link.open();
        link.write("Hello\n".getBytes("UTF-8"));
    } catch (IOException err) {
        CN.callSerially(() -> System.out.println("Printer unreachable: " + err));
    } finally {
        link.close();
    }
}).start();
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected static void
    Throws when called on the EDT; for implementations, at the top of every blocking method.
    abstract void
    Disconnects.
    abstract boolean
    Whether open() succeeded and close() has not been called.
    abstract void
    Connects.
    protected static void
    pause(int millis)
    Sleeps briefly between polls; for implementations.
    abstract int
    read(byte[] buffer, int offset, int length, int timeout)
    Reads what has arrived, waiting up to timeout milliseconds for the first byte.
    int
    readByte(int timeout)
    Reads one byte.
    boolean
    readFully(byte[] buffer, int offset, int length, int timeout)
    Reads exactly length bytes, each within timeout milliseconds of the one before.
    void
    write(byte[] data)
    Writes all of data.
    abstract void
    write(byte[] data, int offset, int length)
    Writes bytes, returning once they are handed to the device.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ByteLink

      public ByteLink()
  • Method Details

    • open

      public abstract void open() throws IOException

      Connects. Does nothing when already open.

      Throws
      • IOException: when the device cannot be reached
      Throws:
      IOException
    • isOpen

      public abstract boolean isOpen()

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

      Returns

      true while open

    • write

      public abstract void write(byte[] data, int offset, int length) throws IOException

      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
      Throws:
      IOException
    • read

      public abstract int read(byte[] buffer, int offset, int length, int timeout) throws IOException

      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
      Throws:
      IOException
    • close

      public abstract void close()
      Disconnects. Safe to call more than once, and from any thread; a read blocked in another thread fails soon after.
    • write

      public void write(byte[] data) throws IOException

      Writes all of data.

      Parameters
      • data: the bytes
      Throws
      • IOException: when the link is closed or broken
      Throws:
      IOException
    • readByte

      public int readByte(int timeout) throws IOException

      Reads one byte.

      Parameters
      • timeout: how long to wait, in milliseconds
      Returns

      the byte, 0 to 255; -1 when nothing arrived in time

      Throws
      • IOException: when the link is closed or broken
      Throws:
      IOException
    • readFully

      public boolean readFully(byte[] buffer, int offset, int length, int timeout) throws IOException

      Reads exactly length bytes, each within timeout milliseconds of the one before.

      Parameters
      • buffer: where to put them

      • offset: where to start in it

      • length: how many

      • timeout: the longest gap allowed, in milliseconds

      Returns

      true when all arrived; false on a gap longer than timeout

      Throws
      • IOException: when the link is closed or broken
      Throws:
      IOException
    • checkNotEdt

      protected static void checkNotEdt()

      Throws when called on the EDT; for implementations, at the top of every blocking method.

      Throws
      • IllegalStateException: on the EDT
    • pause

      protected static void pause(int millis)

      Sleeps briefly between polls; for implementations.

      Parameters
      • millis: how long