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

public final class MemoryLink

  1. Object
  2. ByteLink
  3. MemoryLink

Two links joined back to back in memory: what one writes the other reads. For tests and for a simulated device a driver can talk to without hardware.

MemoryLink[] pair = MemoryLink.pair();
ByteLink tillSide = pair[0];
ByteLink deviceSide = pair[1]; // answer from a test thread

Methods

public static MemoryLink[] pair()Two connected links.
public void open()Connects.
public boolean isOpen()Whether open() succeeded and close() has not been called.
public void write(byte[] data, int offset, int length) throws IOExceptionWrites bytes, returning once they are handed to the device.
public 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 void close()Disconnects.

Inherited methods

Method details

pair

public static MemoryLink[] pair()
Two connected links.

Returns

the two ends; either may be the device’s

open

public void open()
Connects. Does nothing when already open.

Throws

IOException
when the device cannot be reached

isOpen

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

Returns

true while open

write

public 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 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 void close()
Disconnects. Safe to call more than once, and from any thread; a read blocked in another thread fails soon after.