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

public class StreamLink

  1. Object
  2. ByteLink
  3. 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 IOExceptionConnects.
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

Constructor details

StreamLink

public StreamLink(InputStream in, OutputStream out)

Parameters

in InputStream
the stream from the device
out OutputStream
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

in InputStream
the stream from the device
out OutputStream
the stream to the device

open

public void open() throws IOException
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.