Package com.zebra.sdk.comm
Class MultichannelConnection
Object
com.zebra.sdk.comm.MultichannelConnection
- All Implemented Interfaces:
Connection,ConnectionWithWriteLogging
- Direct Known Subclasses:
MultichannelRemoteConnection,MultichannelTcpConnection,MultichannelTlsConnection
Base class for Link-OS printers which support separate printing and status channels. This class conforms to a
standard
Connection and if only the Connection methods are used, the SDK will attempt to open
both the printing and status channels. MultichannelConnection will use the raw Connection when a method is
called directly. Furthermore, when using a ZebraPrinterLinkOs in conjunction with a
MultichannelConnection, the ZebraPrinterLinkOs will attempt to use whichever connection is the most
efficient. If you wish to only open up a specific channel, use either the
openPrintingChannel() or
openStatusChannel() method and their corresponding
closePrintingChannel() and
closeStatusChannel() method.-
Method Summary
Modifier and TypeMethodDescriptionvoidaddWriteLogStream(OutputStream logStream) Adds the OutputStream to the connection to log all data written to the connected device.intReturns an estimate of the number of bytes that can be read from this connection without blocking.voidclose()Closes both the printing and status channels of this MultichannelConnection.voidCloses the printing channel of this MultichannelConnection.voidCloses the status channel of this MultichannelConnection.getConnectionReestablisher(long thresholdTime) Returns aConnectionReestablisherwhich allows for easy recreation of a connection which may have been closed.intReturns the maximum time, in milliseconds, to wait for any data to be received on the printing channel.Returns the underlying printingConnectionof this MultichannelConnection.Returns the underlying statusStatusConnectionof this MultichannelConnection.intReturns the maximum time, in milliseconds, to wait in-between reads after the initial read for the printing channel.booleanReturns true if the connection is open.voidopen()Opens both the printing and status channel of this Multichannel connection.voidOpens the printing channel of this Multichannel connection.voidOpens the status channel of this Multichannel connection.byte[]read()Reads all the available data from the connection.voidread(OutputStream destinationStream) Reads all the available data from the connection.intreadChar()Reads the next byte of data from the connection, similar to a Java InputStream.byte[]sendAndWaitForResponse(byte[] dataToSend, int initialResponseTimeout, int responseCompletionTimeout, String terminator) SendsdataToSendand returns the response data.voidsendAndWaitForResponse(OutputStream destinationStream, InputStream sourceStream, int initialResponseTimeout, int responseCompletionTimeout, String terminator) Sends data fromsourceStreamand writes the response data to destinationStream.byte[]sendAndWaitForValidResponse(byte[] dataToSend, int initialResponseTimeout, int responseCompletionTimeout, ResponseValidator validator) SendsdataToSendand returns the response data.voidsendAndWaitForValidResponse(OutputStream destinationStream, InputStream sourceStream, int initialResponseTimeout, int responseCompletionTimeout, ResponseValidator validator) Sends data fromsourceStreamand writes the response data to destinationStream.voidsetMaxTimeoutForRead(int maxTimeoutForRead) Set the maximum time, in milliseconds, to wait for any data to be received.voidsetTimeToWaitForMoreData(int timeToWaitForMoreData) Set the maximum time, in milliseconds, to wait in-between reads after the initial read.voidwaitForData(int maxTimeout) Causes the currently executing thread to sleep untilConnection.bytesAvailable()> 0, or for a maximum ofmaxTimeoutmilliseconds.voidwrite(byte[] data) Writesdata.lengthbytes from the specified byte array to this output stream.voidwrite(byte[] data, int offset, int length) Writeslengthbytes fromdatastarting atoffset.voidwrite(InputStream dataSource) Writes all available bytes from the data source to this output stream.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.zebra.sdk.comm.Connection
getSimpleConnectionName, toString
-
Method Details
-
open
Opens both the printing and status channel of this Multichannel connection. If neither channel can be opened, then aConnectionExceptionis thrown. When this Multichannel connection is no longer needed, you must callConnection.close()to free up system resources.- Specified by:
openin interfaceConnection- Throws:
ConnectionException- if the connection cannot be established.
-
openPrintingChannel
Opens the printing channel of this Multichannel connection. If this method is called when this channel has already been opened, the call is ignored. When this channel is no longer needed, you must call eitherConnection.close()orclosePrintingChannel()to free up system resources.- Throws:
ConnectionException- if the connection cannot be established.
-
openStatusChannel
Opens the status channel of this Multichannel connection. If this method is called when this channel has already been opened, the call is ignored. When this channel is no longer needed, you must call eitherConnection.close()orcloseStatusChannel()to free up system resources.- Throws:
ConnectionException
-
close
Closes both the printing and status channels of this MultichannelConnection. Releases any system resources associated with the connection. If the connection is already closed then invoking this method has no effect.- Specified by:
closein interfaceConnection- Throws:
ConnectionException- if an I/O error occurs.
-
closePrintingChannel
Closes the printing channel of this MultichannelConnection. Releases any system resources associated with the channel. If the channel is already closed then invoking this method has no effect.- Throws:
ConnectionException- if an I/O error occurs.
-
closeStatusChannel
Closes the status channel of this MultichannelConnection. Releases any system resources associated with the channel. If the channel is already closed then invoking this method has no effect.- Throws:
ConnectionException- if an I/O error occurs.
-
write
Description copied from interface:ConnectionWritesdata.lengthbytes from the specified byte array to this output stream. The connection must be open before this method is called. Ifwriteis called when a connection is closed, aConnectionExceptionis thrown.- Specified by:
writein interfaceConnection- Parameters:
data- the data.- Throws:
ConnectionException- if an I/O error occurs.- See Also:
-
write
Description copied from interface:ConnectionWriteslengthbytes fromdatastarting atoffset. The connection must be open before this method is called. Ifwriteis called when a connection is closed, aConnectionExceptionis thrown.- Specified by:
writein interfaceConnection- Parameters:
data- the data.offset- the start offset in thedata.length- number of bytes to write.- Throws:
ConnectionException- if an I/O error occurs.- See Also:
-
write
Description copied from interface:ConnectionWrites all available bytes from the data source to this output stream. The connection must be open before this method is called. Ifwriteis called when a connection is closed, aConnectionExceptionis thrown.- Specified by:
writein interfaceConnection- Parameters:
dataSource- the data.- Throws:
ConnectionException- if an I/O error occurs.- See Also:
-
read
Description copied from interface:ConnectionReads all the available data from the connection. This call is non-blocking.byte[] data = printerConnection.read();
- Specified by:
readin interfaceConnection- Returns:
- the bytes read from the connection.
- Throws:
ConnectionException- if an I/O error occurs.- See Also:
-
read
Description copied from interface:ConnectionReads all the available data from the connection. This call is non-blocking.- Specified by:
readin interfaceConnection- Parameters:
destinationStream- for read data- Throws:
ConnectionException- if an I/O error occurs.- See Also:
-
readChar
Description copied from interface:ConnectionReads the next byte of data from the connection, similar to a Java InputStream. The value byte is returned as an int in the range of 0 to 255. If no byte is available on the connection the value -1 is returned.int singleCharacter = printerConnection.readChar();
- Specified by:
readCharin interfaceConnection- Returns:
- the next byte from the connection
- Throws:
ConnectionException- if an I/O error occurs.- See Also:
-
isConnected
public boolean isConnected()Description copied from interface:ConnectionReturns true if the connection is open.- Specified by:
isConnectedin interfaceConnection- Returns:
trueif this connection is open.- See Also:
-
bytesAvailable
Description copied from interface:ConnectionReturns an estimate of the number of bytes that can be read from this connection without blocking.- Specified by:
bytesAvailablein interfaceConnection- Returns:
- estimated number of bytes available.
- Throws:
ConnectionException- if an I/O error occurs.- See Also:
-
waitForData
Description copied from interface:ConnectionCauses the currently executing thread to sleep untilConnection.bytesAvailable()> 0, or for a maximum ofmaxTimeoutmilliseconds.- Specified by:
waitForDatain interfaceConnection- Parameters:
maxTimeout- maximum time in milliseconds to wait for an initial response from the printer.- Throws:
ConnectionException- if an I/O error occurs.- See Also:
-
getMaxTimeoutForRead
public int getMaxTimeoutForRead()Returns the maximum time, in milliseconds, to wait for any data to be received on the printing channel.- Specified by:
getMaxTimeoutForReadin interfaceConnection- Returns:
- the maximum time, in milliseconds, to wait for any data to be received.
-
getTimeToWaitForMoreData
public int getTimeToWaitForMoreData()Returns the maximum time, in milliseconds, to wait in-between reads after the initial read for the printing channel.- Specified by:
getTimeToWaitForMoreDatain interfaceConnection- Returns:
- the maximum time, in milliseconds, to wait in-between reads after the initial read.
-
setMaxTimeoutForRead
public void setMaxTimeoutForRead(int maxTimeoutForRead) Set the maximum time, in milliseconds, to wait for any data to be received. This sets the timeout for the printing and status channels.- Specified by:
setMaxTimeoutForReadin interfaceConnection- Parameters:
maxTimeoutForRead- the maximum time, in milliseconds, to wait for any data to be received.
-
setTimeToWaitForMoreData
public void setTimeToWaitForMoreData(int timeToWaitForMoreData) Set the maximum time, in milliseconds, to wait in-between reads after the initial read. This sets the timeout for the printing and status channels.- Specified by:
setTimeToWaitForMoreDatain interfaceConnection- Parameters:
timeToWaitForMoreData- the maximum time, in milliseconds, to wait in-between reads after the initial read.
-
sendAndWaitForResponse
public byte[] sendAndWaitForResponse(byte[] dataToSend, int initialResponseTimeout, int responseCompletionTimeout, String terminator) throws ConnectionException Description copied from interface:ConnectionSendsdataToSendand returns the response data. The software returns immediately if the data received containsterminator. The connection must be open before this method is called. IfsendAndWaitForResponseis called when a connection is closed, aConnectionExceptionis thrown.- Specified by:
sendAndWaitForResponsein interfaceConnection- Parameters:
dataToSend- byte array of data to sendinitialResponseTimeout- The maximum time, in milliseconds, to wait for the initial response to be received. If no data is received during this time, the function returns a zero length array.responseCompletionTimeout- After the initial response, if no data is received for this period of time, the input is considered complete and the method returns.terminator- If the response contains this string, the input is considered complete and the method returns. May be used to avoid waiting for more data when the response is always terminated with a known string. Usenullif no terminator is desired.- Returns:
- received data
- Throws:
ConnectionException- if an I/O error occurs.- See Also:
-
sendAndWaitForResponse
public void sendAndWaitForResponse(OutputStream destinationStream, InputStream sourceStream, int initialResponseTimeout, int responseCompletionTimeout, String terminator) throws ConnectionException Description copied from interface:ConnectionSends data fromsourceStreamand writes the response data to destinationStream. The software returns immediately if the data received containsterminator. The connection must be open before this method is called. IfsendAndWaitForResponseis called when a connection is closed, aConnectionExceptionis thrown.- Specified by:
sendAndWaitForResponsein interfaceConnection- Parameters:
destinationStream- Destination for response.sourceStream- Source of data to be sent.initialResponseTimeout- The maximum time, in milliseconds, to wait for the initial response to be received. If no data is received during this time, the function does not write any data to the destination stream.responseCompletionTimeout- After the initial response, if no data is received for this period of time, the input is considered complete and the method returns.terminator- If the response contains this string, the input is considered complete and the method returns. May be used to avoid waiting for more data when the response is always terminated with a known string. Usenullif no terminator is desired.- Throws:
ConnectionException- if an I/O error occurs.- See Also:
-
sendAndWaitForValidResponse
public byte[] sendAndWaitForValidResponse(byte[] dataToSend, int initialResponseTimeout, int responseCompletionTimeout, ResponseValidator validator) throws ConnectionException Description copied from interface:ConnectionSendsdataToSendand returns the response data. The software returns immediately if the data received satisfiesvalidator. The connection must be open before this method is called. IfsendAndWaitForResponseis called when a connection is closed, aConnectionExceptionis thrown.- Specified by:
sendAndWaitForValidResponsein interfaceConnection- Parameters:
dataToSend- byte array of data to sendinitialResponseTimeout- The maximum time, in milliseconds, to wait for the initial response to be received. If no data is received during this time, the function returns a zero length array.responseCompletionTimeout- After the initial response, if no data is received for this period of time, the input is considered complete and the method returns.validator- If the response satisfies this validator, the input is considered complete and the method returns. May be used to avoid waiting for more data when the response follows a known format.- Returns:
- received data
- Throws:
ConnectionException- if an I/O error occurs.- See Also:
-
sendAndWaitForValidResponse
public void sendAndWaitForValidResponse(OutputStream destinationStream, InputStream sourceStream, int initialResponseTimeout, int responseCompletionTimeout, ResponseValidator validator) throws ConnectionException Description copied from interface:ConnectionSends data fromsourceStreamand writes the response data to destinationStream. The software returns immediately if the data received satisfiesvalidator. The connection must be open before this method is called. IfsendAndWaitForResponseis called when a connection is closed, aConnectionExceptionis thrown.- Specified by:
sendAndWaitForValidResponsein interfaceConnection- Parameters:
destinationStream- Destination for response.sourceStream- Source of data to be sent.initialResponseTimeout- The maximum time, in milliseconds, to wait for the initial response to be received. If no data is received during this time, the function does not write any data to the destination stream.responseCompletionTimeout- After the initial response, if no data is received for this period of time, the input is considered complete and the method returns.validator- If the response satisfies this validator, the input is considered complete and the method returns. May be used to avoid waiting for more data when the response follows a known format. If validator is null, no validation is performed. When performing validation, this method will use enough memory to hold the entire response.- Throws:
ConnectionException- if an I/O error occurs.- See Also:
-
getPrintingChannel
Returns the underlying printingConnectionof this MultichannelConnection.- Returns:
- the printing channel.
-
getStatusChannel
Returns the underlying statusStatusConnectionof this MultichannelConnection.- Returns:
- the status channel.
-
getConnectionReestablisher
public ConnectionReestablisher getConnectionReestablisher(long thresholdTime) throws ConnectionException Description copied from interface:ConnectionReturns aConnectionReestablisherwhich allows for easy recreation of a connection which may have been closed.- This call should be made while there is still an active connection to the printer (prior to issuing a command which will make the printer non-responsive).
- Specified by:
getConnectionReestablisherin interfaceConnection- Parameters:
thresholdTime- how long the Connection reestablisher will wait before attempting to reconnection to the printer- Returns:
- ConnectionReestablisher
- Throws:
ConnectionException- if the ConnectionReestablisher could not be created.- See Also:
-
addWriteLogStream
Description copied from interface:ConnectionWithWriteLoggingAdds the OutputStream to the connection to log all data written to the connected device.- Specified by:
addWriteLogStreamin interfaceConnectionWithWriteLogging- Parameters:
logStream- log all write data to this stream.- See Also:
-