Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public class StreamLink
Known subtypesTcpLink
A link over a pair of streams that are already connected, such as a
Bluetooth socket’s. Reads wait by polling
available(), so the input
stream must report it; most socket streams do.Constructors
public StreamLink(InputStream in, OutputStream out) | |
protected StreamLink() | For subclasses that connect in open() and call setStreams(InputStream, OutputStream). |
Methods
protected void setStreams(InputStream in, OutputStream out) | Supplies the streams once connected; for subclasses. |
public void open()
throws IOException | Connects. |
public boolean isOpen() | Whether open() succeeded and close() has not been called. |
public void write(byte[] data, int offset, int length)
throws IOException | Writes bytes, returning once they are handed to the device. |
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. |
public void close() | Disconnects. |
Inherited methods
Constructor details
StreamLink
public StreamLink(InputStream in, OutputStream out)Parameters
inInputStream- the stream from the device
outOutputStream- the stream to the device
StreamLink
protected StreamLink()For subclasses that connect in
open() and call
setStreams(InputStream, OutputStream).Method details
setStreams
protected void setStreams(InputStream in, OutputStream out)Supplies the streams once connected; for subclasses.
Parameters
inInputStream- the stream from the device
outOutputStream- the stream to the device
open
public void open()
throws IOExceptionConnects. Does nothing when already open.
Throws
IOException- when the device cannot be reached
isOpen
public boolean isOpen()Returns
true while open
write
public 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 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 void close()Disconnects. Safe to call more than once, and from any thread; a read
blocked in another thread fails soon after.