Class ConnectionA

Object
com.zebra.sdk.comm.ConnectionA
All Implemented Interfaces:
Connection, ConnectionWithWriteLogging
Direct Known Subclasses:
ConnectionStatusA, DriverPrinterConnection, RemoteConnection, TcpConnection, TlsConnection, UsbConnection

public abstract class ConnectionA extends Object implements ConnectionWithWriteLogging
Abstract class which implements the default functionality of Connection.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds the OutputStream to the connection to log all data written to the connected device.
    int
    Returns an estimate of the number of bytes that can be read from this connection without blocking.
    void
    Closes this connection and releases any system resources associated with the connection.
    getConnectionReestablisher(long thresholdTime)
    Returns a ConnectionReestablisher which allows for easy recreation of a connection which may have been closed.
    Returns the printer's manufacturer.
    int
    Gets the maximum number of bytes to write at one time
    int
    Returns the maximum time, in milliseconds, to wait for any data to be received.
    int
    Returns the maximum time, in milliseconds, to wait in-between reads after the initial read.
    boolean
    Returns true if the connection is open.
    void
    Opens the connection to a device.
    byte[]
    Reads all the available data from the connection.
    byte[]
    read(int maxBytesToRead)
    Reads maxBytesToRead of the available data from the connection.
    byte[]
    read(int maxBytesToRead, boolean exitOnFirstRead)
    Reads maxBytesToRead of the available data from the connection.
    void
    read(OutputStream destinationStream)
    Reads all the available data from the connection.
    int
    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(OutputStream destinationStream, 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(OutputStream destinationStream, InputStream sourceStream, int initialResponseTimeout, int responseCompletionTimeout, ResponseValidator validator)
    Sends data from sourceStream and writes the response data to destinationStream.
    void
    setMaxDataToWrite(int maxDataSize)
    Sets the maximum number of bytes to write at one time
    void
    setMaxTimeoutForRead(int maxTimeoutForRead)
    Set the maximum time, in milliseconds, to wait for any data to be received
    void
    setReadTimeout(int timeout)
    Sets the underlying socket read timeout value
    void
    setTimeToWaitForMoreData(int timeToWaitForMoreData)
    Set the maximum time, in milliseconds, to wait in-between reads after the initial read.
    void
    waitForData(int maxTimeout)
    Causes the currently executing thread to sleep until Connection.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(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, wait

    Methods inherited from interface com.zebra.sdk.comm.Connection

    getSimpleConnectionName, toString
  • Method Details

    • open

      public void open() throws ConnectionException
      Description copied from interface: Connection
      Opens the connection to a device. If the 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 Connection.close() to free up system resources.
      Specified by:
      open in interface Connection
      Throws:
      ConnectionException - if the connection cannot be established.
      See Also:
    • close

      public void close() throws ConnectionException
      Description copied from interface: Connection
      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.
      Specified by:
      close in interface Connection
      Throws:
      ConnectionException - if an I/O error occurs.
      See Also:
    • write

      public void write(byte[] data) throws ConnectionException
      Description copied from interface: Connection
      Writes 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.
      Specified by:
      write in interface Connection
      Parameters:
      data - the data.
      Throws:
      ConnectionException - if an I/O error occurs.
      See Also:
    • write

      public void write(InputStream dataSource) throws ConnectionException
      Description copied from interface: Connection
      Writes all available bytes from the data source 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.
      Specified by:
      write in interface Connection
      Parameters:
      dataSource - the data.
      Throws:
      ConnectionException - if an I/O error occurs.
      See Also:
    • write

      public void write(byte[] data, int offset, int length) throws ConnectionException
      Description copied from interface: Connection
      Writes 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.
      Specified by:
      write in interface Connection
      Parameters:
      data - the data.
      offset - the start offset in the data.
      length - number of bytes to write.
      Throws:
      ConnectionException - if an I/O error occurs.
      See Also:
    • isConnected

      public boolean isConnected()
      Description copied from interface: Connection
      Returns true if the connection is open.
      Specified by:
      isConnected in interface Connection
      Returns:
      true if this connection is open.
      See Also:
    • read

      public byte[] read() throws ConnectionException
      Description copied from interface: Connection
      Reads all the available data from the connection. This call is non-blocking.
       byte[] data = printerConnection.read();
       
      Specified by:
      read in interface Connection
      Returns:
      the bytes read from the connection.
      Throws:
      ConnectionException - if an I/O error occurs.
      See Also:
    • read

      public byte[] read(int maxBytesToRead) throws ConnectionException
      Reads maxBytesToRead of 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

      public void read(OutputStream destinationStream) throws ConnectionException
      Description copied from interface: Connection
      Reads all the available data from the connection. This call is non-blocking.
      Specified by:
      read in interface Connection
      Parameters:
      destinationStream - for read data
      Throws:
      ConnectionException - if an I/O error occurs.
      See Also:
    • readChar

      public int readChar() throws ConnectionException
      Description copied from interface: Connection
      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();
       
      Specified by:
      readChar in interface Connection
      Returns:
      the next byte from the connection
      Throws:
      ConnectionException - if an I/O error occurs.
      See Also:
    • bytesAvailable

      public int bytesAvailable() throws ConnectionException
      Description copied from interface: Connection
      Returns an estimate of the number of bytes that can be read from this connection without blocking.
      Specified by:
      bytesAvailable in interface Connection
      Returns:
      estimated number of bytes available.
      Throws:
      ConnectionException - if an I/O error occurs.
      See Also:
    • waitForData

      public void waitForData(int maxTimeout) throws ConnectionException
      Description copied from interface: Connection
      Causes the currently executing thread to sleep until Connection.bytesAvailable() > 0, or for a maximum of maxTimeout milliseconds.
      Specified by:
      waitForData in interface Connection
      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: Connection
      Returns the maximum time, in milliseconds, to wait for any data to be received.
      Specified by:
      getMaxTimeoutForRead in interface Connection
      Returns:
      the maximum time, in milliseconds, to wait for any data to be received.
      See Also:
    • getTimeToWaitForMoreData

      public int getTimeToWaitForMoreData()
      Description copied from interface: Connection
      Returns the maximum time, in milliseconds, to wait in-between reads after the initial read.
      Specified by:
      getTimeToWaitForMoreData in interface Connection
      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: Connection
      Sends 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.
      Specified by:
      sendAndWaitForResponse in interface Connection
      Parameters:
      dataToSend - byte array of data to send
      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 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.
      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: Connection
      Sends data from 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.
      Specified by:
      sendAndWaitForResponse in interface Connection
      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. Use null if 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: Connection
      Sends 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.
      Specified by:
      sendAndWaitForValidResponse in interface Connection
      Parameters:
      dataToSend - byte array of data to send
      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 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: Connection
      Sends data from 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.
      Specified by:
      sendAndWaitForValidResponse in interface Connection
      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: Connection
      Set the maximum time, in milliseconds, to wait for any data to be received
      Specified by:
      setMaxTimeoutForRead in interface Connection
      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: Connection
      Set the maximum time, in milliseconds, to wait in-between reads after the initial read.
      Specified by:
      setTimeToWaitForMoreData in interface Connection
      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: Connection
      Returns a ConnectionReestablisher which 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:
      getConnectionReestablisher in interface Connection
      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

      public void addWriteLogStream(OutputStream logStream)
      Description copied from interface: ConnectionWithWriteLogging
      Adds the OutputStream to the connection to log all data written to the connected device.
      Specified by:
      addWriteLogStream in interface ConnectionWithWriteLogging
      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

      public byte[] read(int maxBytesToRead, boolean exitOnFirstRead) throws ConnectionException
      Reads maxBytesToRead of the available data from the connection.
      Parameters:
      maxBytesToRead - number of bytes to read
      exitOnFirstRead - true to exit on first data read
      Returns:
      the bytes read from the connection.
      Throws:
      ConnectionException - if an I/O error occurs.
    • getManufacturer

      public String getManufacturer()
      Returns the printer's manufacturer.
      Returns:
      The printer's manufacturer.
    • setReadTimeout

      public void setReadTimeout(int timeout) throws ConnectionException
      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