public interface Connection
Modifier and Type | Method and Description |
---|---|
int |
bytesAvailable()
Returns an estimate of the number of bytes that can be read from this connection without blocking.
|
void |
close()
Closes this connection and releases any system resources associated with the connection.
|
ConnectionReestablisher |
getConnectionReestablisher(long thresholdTime)
Returns a
ConnectionReestablisher which allows for easy recreation of a connection which may have
been closed. |
int |
getMaxTimeoutForRead()
Returns the maximum time, in milliseconds, to wait for any data to be received.
|
String |
getSimpleConnectionName()
Return a human-readable description of the connection.
|
int |
getTimeToWaitForMoreData()
Returns the maximum time, in milliseconds, to wait in-between reads after the initial read.
|
boolean |
isConnected()
Returns true if the connection is open.
|
void |
open()
Opens the connection to a device.
|
byte[] |
read()
Reads all the available data from the connection.
|
void |
read(java.io.OutputStream destinationStream)
Reads all the available data from the connection.
|
int |
readChar()
Reads the next byte of data from the connection, similar to a Java InputStream.
|
byte[] |
sendAndWaitForResponse(byte[] dataToSend,
int initialResponseTimeout,
int responseCompletionTimeout,
String terminator)
Sends
dataToSend and returns the response data. |
void |
sendAndWaitForResponse(java.io.OutputStream destinationStream,
java.io.InputStream sourceStream,
int initialResponseTimeout,
int responseCompletionTimeout,
String terminator)
Sends data from
sourceStream and writes the response data to destinationStream. |
byte[] |
sendAndWaitForValidResponse(byte[] dataToSend,
int initialResponseTimeout,
int responseCompletionTimeout,
ResponseValidator validator)
Sends
dataToSend and returns the response data. |
void |
sendAndWaitForValidResponse(java.io.OutputStream destinationStream,
java.io.InputStream sourceStream,
int initialResponseTimeout,
int responseCompletionTimeout,
ResponseValidator validator)
Sends data from
sourceStream and writes the response data to destinationStream. |
void |
setMaxTimeoutForRead(int maxTimeoutForRead)
Set the maximum time, in milliseconds, to wait for any data to be received
|
void |
setTimeToWaitForMoreData(int timeToWaitForMoreData)
Set the maximum time, in milliseconds, to wait in-between reads after the initial read.
|
String |
toString()
See the classes which implement this method for the format of the description string.
|
void |
waitForData(int maxTimeout)
Causes the currently executing thread to sleep until
bytesAvailable() > 0, or for a maximum of
maxTimeout milliseconds. |
void |
write(byte[] data)
Writes
data.length bytes from the specified byte array to this output stream. |
void |
write(byte[] data,
int offset,
int length)
Writes
length bytes from data starting at offset . |
void |
write(java.io.InputStream dataSource)
Writes all available bytes from the data source to this output stream.
|
void open() throws ConnectionException
open
method 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 call close()
to free up system resources.ConnectionException
- if the connection cannot be established.void close() throws ConnectionException
ConnectionException
- if an I/O error occurs.void write(byte[] data) throws ConnectionException
data.length
bytes from the specified byte array to this output stream. The connection must be
open before this method is called. If write
is called when a connection is closed, a
ConnectionException
is thrown.data
- the data.ConnectionException
- if an I/O error occurs.OutputStream.write(byte[])
void write(byte[] data, int offset, int length) throws ConnectionException
length
bytes from data
starting at offset
. The connection must be
open before this method is called. If write
is called when a connection is closed, a
ConnectionException
is thrown.data
- the data.offset
- the start offset in the data
.length
- number of bytes to write.ConnectionException
- if an I/O error occurs.OutputStream.write(byte[], int, int)
void write(java.io.InputStream dataSource) throws ConnectionException
write
is called when a connection is closed, a ConnectionException
is thrown.dataSource
- the data.ConnectionException
- if an I/O error occurs.byte[] read() throws ConnectionException
byte[] data = printerConnection.read();
ConnectionException
- if an I/O error occurs.int readChar() throws ConnectionException
int singleCharacter = printerConnection.readChar();
ConnectionException
- if an I/O error occurs.void read(java.io.OutputStream destinationStream) throws ConnectionException
destinationStream
- for read dataConnectionException
- if an I/O error occurs.boolean isConnected()
true
if this connection is open.int bytesAvailable() throws ConnectionException
ConnectionException
- if an I/O error occurs.String toString()
toString
in class Object
String getSimpleConnectionName()
void waitForData(int maxTimeout) throws ConnectionException
bytesAvailable()
> 0, or for a maximum of
maxTimeout
milliseconds.maxTimeout
- maximum time in milliseconds to wait for an initial response from the printer.ConnectionException
- if an I/O error occurs.int getMaxTimeoutForRead()
int getTimeToWaitForMoreData()
void setMaxTimeoutForRead(int maxTimeoutForRead)
maxTimeoutForRead
- the maximum time, in milliseconds, to wait for any data to be received.void setTimeToWaitForMoreData(int timeToWaitForMoreData)
timeToWaitForMoreData
- the maximum time, in milliseconds, to wait in-between reads after the initial read.byte[] sendAndWaitForResponse(byte[] dataToSend, int initialResponseTimeout, int responseCompletionTimeout, String terminator) throws ConnectionException
dataToSend
and returns the response data. The software returns immediately if the data
received contains terminator
. The connection must be open before this method is called. If
sendAndWaitForResponse
is called when a connection is closed, a ConnectionException
is
thrown.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. Use
null
if no terminator is desired.ConnectionException
- if an I/O error occurs.void sendAndWaitForResponse(java.io.OutputStream destinationStream, java.io.InputStream sourceStream, int initialResponseTimeout, int responseCompletionTimeout, String terminator) throws ConnectionException
sourceStream
and writes the response data to destinationStream. The software returns
immediately if the data received contains terminator
. The connection must be open before this method
is called. If sendAndWaitForResponse
is called when a connection is closed, a
ConnectionException
is thrown.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. Use
null
if no terminator is desired.ConnectionException
- if an I/O error occurs.byte[] sendAndWaitForValidResponse(byte[] dataToSend, int initialResponseTimeout, int responseCompletionTimeout, ResponseValidator validator) throws ConnectionException
dataToSend
and returns the response data. The software returns immediately if the data
received satisfies validator
. The connection must be open before this method is called. If
sendAndWaitForResponse
is called when a connection is closed, a ConnectionException
is
thrown.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.ConnectionException
- if an I/O error occurs.void sendAndWaitForValidResponse(java.io.OutputStream destinationStream, java.io.InputStream sourceStream, int initialResponseTimeout, int responseCompletionTimeout, ResponseValidator validator) throws ConnectionException
sourceStream
and writes the response data to destinationStream. The software returns
immediately if the data received satisfies validator
. The connection must be open before this method
is called. If sendAndWaitForResponse
is called when a connection is closed, a
ConnectionException
is thrown.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.ConnectionException
- if an I/O error occurs.ConnectionReestablisher getConnectionReestablisher(long thresholdTime) throws ConnectionException
ConnectionReestablisher
which allows for easy recreation of a connection which may have
been closed.
thresholdTime
- how long the Connection reestablisher will wait before attempting to reconnection to the
printerConnectionException
- if the ConnectionReestablisher could not be created.
© 2017 ZIH Corp. All Rights Reserved.