Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public abstract class ByteLink
- Object
- 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
Inherited methods
Constructor details
ByteLink
public ByteLink()Method details
open
public abstract void open()
throws IOExceptionConnects. Does nothing when already open.
Throws
IOException- when the device cannot be reached
isOpen
public abstract boolean isOpen()Returns
true while open
write
public abstract void write(byte[] data, int offset, int length)
throws IOExceptionWrites bytes, returning once they are handed to the device.
Parameters
databyte[]- the buffer
offsetint- where to start in it
lengthint- 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 IOExceptionReads what has arrived, waiting up to
timeout milliseconds for the
first byte.Parameters
bufferbyte[]- where to put the bytes
offsetint- where to start in it
lengthint- at most how many bytes
timeoutint- 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 IOExceptionWrites all of
data.Parameters
databyte[]- the bytes
Throws
IOException- when the link is closed or broken
readByte
public int readByte(int timeout)
throws IOExceptionReads one byte.
Parameters
timeoutint- 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 IOExceptionReads exactly
length bytes, each within timeout milliseconds of
the one before.Parameters
bufferbyte[]- where to put them
offsetint- where to start in it
lengthint- how many
timeoutint- the longest gap allowed, in milliseconds
Returns
true when all arrived; false on a gap longer than
timeoutThrows
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
millisint- how long