Package eu.mihosoft.devcom
Class StreamConnection<T>
- java.lang.Object
-
- eu.mihosoft.devcom.StreamConnection<T>
-
- All Implemented Interfaces:
DataConnection<T,StreamConnection<T>>,java.lang.AutoCloseable
public final class StreamConnection<T> extends java.lang.Object implements DataConnection<T,StreamConnection<T>>
Stream connection for reading and writing data from io streams.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface eu.mihosoft.devcom.DataConnection
DataConnection.Subscription
-
-
Constructor Summary
Constructors Constructor Description StreamConnection(DataFormat<T> format)Creates a new connection instance.StreamConnection(DataFormat<T> format, java.util.function.Consumer<StreamConnection<T>> onConnectionOpened, java.util.function.BiConsumer<StreamConnection<T>,java.lang.Exception> onIOError)Creates a new connection instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the connection to the specified port.DataFormat<T>getFormat()Returns the data format used by this connection.booleanisOpen()Indicates whether this connection is open.voidopen()Opens the specified port and connects to it.voidopen(java.io.InputStream inputStream, java.io.OutputStream outputStream)Opens the specified port and connects to it.DataConnection.SubscriptionregisterDataListener(java.util.function.Consumer<T> l)Registers a data listener that is notified whenever a message has been received.StreamConnection<T>setOnConnectionOpened(java.util.function.Consumer<StreamConnection<T>> onConnectionOpened)Specifies the action to be performed if the connection has been opened.StreamConnection<T>setOnDataReceived(java.util.function.Consumer<T> onDataReceived)Sets the action to be executed if data has been received.StreamConnection<T>setOnIOError(java.util.function.BiConsumer<StreamConnection<T>,java.lang.Exception> onIOError)Specifies the action to be performed if an I/O error occurs.StreamConnection<T>setStreams(java.io.InputStream inputStream, java.io.OutputStream outputStream)Sets the configuration to use for connecting to a port.voidwriteData(T msg)Writes the specified raw message to the output stream.
-
-
-
Constructor Detail
-
StreamConnection
public StreamConnection(DataFormat<T> format)
Creates a new connection instance.- Parameters:
format- the data format to use for communication
-
StreamConnection
public StreamConnection(DataFormat<T> format, java.util.function.Consumer<StreamConnection<T>> onConnectionOpened, java.util.function.BiConsumer<StreamConnection<T>,java.lang.Exception> onIOError)
Creates a new connection instance.- Parameters:
onConnectionOpened- task to be executed if the selected COM-port has been successfully openedonIOError- task to be executed if an io error has occurred
-
-
Method Detail
-
setOnDataReceived
public StreamConnection<T> setOnDataReceived(java.util.function.Consumer<T> onDataReceived)
Description copied from interface:DataConnectionSets the action to be executed if data has been received.- Specified by:
setOnDataReceivedin interfaceDataConnection<T,StreamConnection<T>>- Parameters:
onDataReceived- consumer to be called if data has been received
-
setOnIOError
public StreamConnection<T> setOnIOError(java.util.function.BiConsumer<StreamConnection<T>,java.lang.Exception> onIOError)
Description copied from interface:DataConnectionSpecifies the action to be performed if an I/O error occurs.- Specified by:
setOnIOErrorin interfaceDataConnection<T,StreamConnection<T>>- Parameters:
onIOError- the action to be performed if an I/O error occurs
-
setOnConnectionOpened
public StreamConnection<T> setOnConnectionOpened(java.util.function.Consumer<StreamConnection<T>> onConnectionOpened)
Specifies the action to be performed if the connection has been opened.- Parameters:
onConnectionOpened- the action to be performed if the connection has been opened
-
registerDataListener
public DataConnection.Subscription registerDataListener(java.util.function.Consumer<T> l)
Description copied from interface:DataConnectionRegisters a data listener that is notified whenever a message has been received.- Specified by:
registerDataListenerin interfaceDataConnection<T,StreamConnection<T>>- Parameters:
l- the data listener to register- Returns:
- a subscription that allows a listener to be unsubscribed
-
open
public void open(java.io.InputStream inputStream, java.io.OutputStream outputStream)Opens the specified port and connects to it.- Parameters:
inputStream- input stream to be used by this connectionoutputStream- output stream to be used by this connection
-
setStreams
public StreamConnection<T> setStreams(java.io.InputStream inputStream, java.io.OutputStream outputStream)
Sets the configuration to use for connecting to a port.- Parameters:
inputStream- the input stream to be used by this connectionoutputStream- the output stream to be used by this connection- Returns:
- this connection
-
open
public void open()
Opens the specified port and connects to it.- Specified by:
openin interfaceDataConnection<T,StreamConnection<T>>
-
writeData
public void writeData(T msg) throws java.io.IOException
Description copied from interface:DataConnectionWrites the specified raw message to the output stream.- Specified by:
writeDatain interfaceDataConnection<T,StreamConnection<T>>- Parameters:
msg- the message to send- Throws:
java.io.IOException- if an i/o error occurs during message sending
-
close
public void close()
Closes the connection to the specified port.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfaceDataConnection<T,StreamConnection<T>>
-
getFormat
public DataFormat<T> getFormat()
Description copied from interface:DataConnectionReturns the data format used by this connection.- Specified by:
getFormatin interfaceDataConnection<T,StreamConnection<T>>- Returns:
- the data format used by this connection
-
isOpen
public boolean isOpen()
Description copied from interface:DataConnectionIndicates whether this connection is open.- Specified by:
isOpenin interfaceDataConnection<T,StreamConnection<T>>- Returns:
trueif this connection is currently open;falseotherwise
-
-