Package com.zebra.sdk.comm
Interface Connection
- All Known Subinterfaces:
ConnectionWithWriteLogging,StatusConnection,StatusConnectionWithWriteLogging
- All Known Implementing Classes:
ConnectionA,ConnectionStatusA,DriverPrinterConnection,MultichannelConnection,MultichannelRemoteConnection,MultichannelTcpConnection,MultichannelTlsConnection,RemoteConnection,RemoteStatusConnection,TcpConnection,TcpStatusConnection,TlsConnection,TlsStatusConnection,UsbConnection
public interface Connection
A connection to a device.
-
Method Summary
Modifier and TypeMethodDescriptionintReturns an estimate of the number of bytes that can be read from this connection without blocking.voidclose()Closes this connection and releases any system resources associated with the connection.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.Return a human-readable description of the connection.intReturns the maximum time, in milliseconds, to wait in-between reads after the initial read.booleanReturns true if the connection is open.voidopen()Opens the connection to a device.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 receivedvoidsetTimeToWaitForMoreData(int timeToWaitForMoreData) Set the maximum time, in milliseconds, to wait in-between reads after the initial read.toString()See the classes which implement this method for the format of the description string.voidwaitForData(int maxTimeout) Causes the currently executing thread to sleep untilbytesAvailable()> 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.
-
Method Details
-
open
Opens the connection to a device. If theopenmethod is called when this connection has already been opened, this call is ignored. When a handle to the connection is no longer needed, you must callclose()to free up system resources.- Throws:
ConnectionException- if the connection cannot be established.
-
close
Closes this connection and releases any system resources associated with the connection. If the connection is already closed then invoking this method has no effect.- Throws:
ConnectionException- if an I/O error occurs.
-
write
Writesdata.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.- Parameters:
data- the data.- Throws:
ConnectionException- if an I/O error occurs.- See Also:
-
write
Writeslengthbytes fromdatastarting atoffset. The connection must be open before this method is called. Ifwriteis called when a connection is closed, aConnectionExceptionis thrown.- 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
Writes 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.- Parameters:
dataSource- the data.- Throws:
ConnectionException- if an I/O error occurs.
-
read
Reads all the available data from the connection. This call is non-blocking.byte[] data = printerConnection.read();
- Returns:
- the bytes read from the connection.
- Throws:
ConnectionException- if an I/O error occurs.
-
readChar
Reads 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();
- Returns:
- the next byte from the connection
- Throws:
ConnectionException- if an I/O error occurs.
-
read
Reads all the available data from the connection. This call is non-blocking.- Parameters:
destinationStream- for read data- Throws:
ConnectionException- if an I/O error occurs.
-
isConnected
boolean isConnected()Returns true if the connection is open.- Returns:
trueif this connection is open.
-
bytesAvailable
Returns an estimate of the number of bytes that can be read from this connection without blocking.- Returns:
- estimated number of bytes available.
- Throws:
ConnectionException- if an I/O error occurs.
-
toString
String toString()See the classes which implement this method for the format of the description string. -
getSimpleConnectionName
String getSimpleConnectionName()Return a human-readable description of the connection.- Returns:
- a human-readable description of the connection.
-
waitForData
Causes the currently executing thread to sleep untilbytesAvailable()> 0, or for a maximum ofmaxTimeoutmilliseconds.- Parameters:
maxTimeout- maximum time in milliseconds to wait for an initial response from the printer.- Throws:
ConnectionException- if an I/O error occurs.
-
getMaxTimeoutForRead
int getMaxTimeoutForRead()Returns the maximum time, in milliseconds, to wait for any data to be received.- Returns:
- the maximum time, in milliseconds, to wait for any data to be received.
-
getTimeToWaitForMoreData
int getTimeToWaitForMoreData()Returns the maximum time, in milliseconds, to wait in-between reads after the initial read.- Returns:
- the maximum time, in milliseconds, to wait in-between reads after the initial read.
-
setMaxTimeoutForRead
void setMaxTimeoutForRead(int maxTimeoutForRead) Set the maximum time, in milliseconds, to wait for any data to be received- Parameters:
maxTimeoutForRead- the maximum time, in milliseconds, to wait for any data to be received.
-
setTimeToWaitForMoreData
void setTimeToWaitForMoreData(int timeToWaitForMoreData) Set the maximum time, in milliseconds, to wait in-between reads after the initial read.- Parameters:
timeToWaitForMoreData- the maximum time, in milliseconds, to wait in-between reads after the initial read.
-
sendAndWaitForResponse
byte[] sendAndWaitForResponse(byte[] dataToSend, int initialResponseTimeout, int responseCompletionTimeout, String terminator) throws ConnectionException SendsdataToSendand 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.- 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.
-
sendAndWaitForResponse
void sendAndWaitForResponse(OutputStream destinationStream, InputStream sourceStream, int initialResponseTimeout, int responseCompletionTimeout, String terminator) throws ConnectionException Sends 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.- 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.
-
sendAndWaitForValidResponse
byte[] sendAndWaitForValidResponse(byte[] dataToSend, int initialResponseTimeout, int responseCompletionTimeout, ResponseValidator validator) throws ConnectionException SendsdataToSendand 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.- 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.
-
sendAndWaitForValidResponse
void sendAndWaitForValidResponse(OutputStream destinationStream, InputStream sourceStream, int initialResponseTimeout, int responseCompletionTimeout, ResponseValidator validator) throws ConnectionException Sends 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.- 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.
-
getConnectionReestablisher
Returns 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).
- 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.
-