Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public abstract class ByteLink

  1. Object
  2. ByteLink

Known subtypesMemoryLink, SerialLink, StreamLink, UsbLink

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();

Constructors

public ByteLink()

Methods

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

Inherited methods

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

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 byte[]
the buffer
offset int
where to start in it
length int
how many bytes

Throws

IOException
when the link is closed or broken

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 byte[]
where to put the bytes
offset int
where to start in it
length int
at most how many bytes
timeout int
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

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 byte[]
the bytes

Throws

IOException
when the link is closed or broken

readByte

public int readByte(int timeout) throws IOException
Reads one byte.

Parameters

timeout int
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

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 byte[]
where to put them
offset int
where to start in it
length int
how many
timeout int
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

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 int
how long