Package com.zebra.sdk.comm
Class ConnectionA
Object
com.zebra.sdk.comm.ConnectionA
- All Implemented Interfaces:
Connection,ConnectionWithWriteLogging
- Direct Known Subclasses:
ConnectionStatusA,DriverPrinterConnection,RemoteConnection,TcpConnection,TlsConnection,UsbConnection
Abstract class which implements the default functionality of
Connection.-
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 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.Returns the printer's manufacturer.intGets the maximum number of bytes to write at one timeintReturns the maximum time, in milliseconds, to wait for any data to be received.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.byte[]read(int maxBytesToRead) ReadsmaxBytesToReadof the available data from the connection.byte[]read(int maxBytesToRead, boolean exitOnFirstRead) ReadsmaxBytesToReadof 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.voidsetMaxDataToWrite(int maxDataSize) Sets the maximum number of bytes to write at one timevoidsetMaxTimeoutForRead(int maxTimeoutForRead) Set the maximum time, in milliseconds, to wait for any data to be receivedvoidsetReadTimeout(int timeout) Sets the underlying socket read timeout valuevoidsetTimeToWaitForMoreData(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
Description copied from interface:ConnectionOpens 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 callConnection.close()to free up system resources.- Specified by:
openin interfaceConnection- Throws:
ConnectionException- if the connection cannot be established.- See Also:
-
close
Description copied from interface:ConnectionCloses this connection and 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.- See Also:
-
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: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:
-
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:
-
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:
-
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
ReadsmaxBytesToReadof the available data from the connection. This call is non-blocking.- Parameters:
maxBytesToRead- number of bytes to read- Returns:
- the bytes read from the connection.
- Throws:
ConnectionException- if an I/O error occurs.
-
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:
-
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()Description copied from interface:ConnectionReturns the maximum time, in milliseconds, to wait for any data to be received.- Specified by:
getMaxTimeoutForReadin interfaceConnection- Returns:
- the maximum time, in milliseconds, to wait for any data to be received.
- See Also:
-
getTimeToWaitForMoreData
public int getTimeToWaitForMoreData()Description copied from interface:ConnectionReturns the maximum time, in milliseconds, to wait in-between reads after the initial read.- Specified by:
getTimeToWaitForMoreDatain interfaceConnection- Returns:
- the maximum time, in milliseconds, to wait in-between reads after the initial read.
- See Also:
-
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:
-
setMaxTimeoutForRead
public void setMaxTimeoutForRead(int maxTimeoutForRead) Description copied from interface:ConnectionSet the maximum time, in milliseconds, to wait for any data to be received- Specified by:
setMaxTimeoutForReadin interfaceConnection- Parameters:
maxTimeoutForRead- the maximum time, in milliseconds, to wait for any data to be received.- See Also:
-
setTimeToWaitForMoreData
public void setTimeToWaitForMoreData(int timeToWaitForMoreData) Description copied from interface:ConnectionSet the maximum time, in milliseconds, to wait in-between reads after the initial read.- Specified by:
setTimeToWaitForMoreDatain interfaceConnection- Parameters:
timeToWaitForMoreData- the maximum time, in milliseconds, to wait in-between reads after the initial read.- See Also:
-
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:
-
setMaxDataToWrite
public void setMaxDataToWrite(int maxDataSize) Sets the maximum number of bytes to write at one time- Parameters:
maxDataSize- maximum number of bytes to write at one time
-
getMaxDataToWrite
public int getMaxDataToWrite()Gets the maximum number of bytes to write at one time- Returns:
- maximum number of bytes to write at one time
-
read
ReadsmaxBytesToReadof the available data from the connection.- Parameters:
maxBytesToRead- number of bytes to readexitOnFirstRead- true to exit on first data read- Returns:
- the bytes read from the connection.
- Throws:
ConnectionException- if an I/O error occurs.
-
getManufacturer
Returns the printer's manufacturer.- Returns:
- The printer's manufacturer.
-
setReadTimeout
Sets the underlying socket read timeout value- Parameters:
timeout- The read timeout in milliseconds- Throws:
ConnectionException- If an error occurs while setting the read timeout
-