Class ByteLink
- Direct Known Subclasses:
MemoryLink, 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();
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected static voidThrows when called on the EDT; for implementations, at the top of every blocking method.abstract voidclose()Disconnects.abstract booleanisOpen()abstract voidopen()Connects.protected static voidpause(int millis) Sleeps briefly between polls; for implementations.abstract intread(byte[] buffer, int offset, int length, int timeout) Reads what has arrived, waiting up totimeoutmilliseconds for the first byte.intreadByte(int timeout) Reads one byte.booleanreadFully(byte[] buffer, int offset, int length, int timeout) Reads exactlylengthbytes, each withintimeoutmilliseconds of the one before.voidwrite(byte[] data) Writes all ofdata.abstract voidwrite(byte[] data, int offset, int length) Writes bytes, returning once they are handed to the device.
-
Constructor Details
-
ByteLink
public ByteLink()
-
-
Method Details
-
open
Connects. Does nothing when already open.
Throws
IOException: when the device cannot be reached
- Throws:
IOException
-
isOpen
-
write
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
Reads what has arrived, waiting up to
timeoutmilliseconds 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
Writes all of
data.Parameters
data: the bytes
Throws
IOException: when the link is closed or broken
- Throws:
IOException
-
readByte
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
Reads exactly
lengthbytes, each withintimeoutmilliseconds 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
timeoutThrows
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
-