Class ProtocolOutputHandler

java.lang.Object
java.io.OutputStream
com.swiftmq.net.protocol.ProtocolOutputHandler
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable
Direct Known Subclasses:
RawOutputHandler, SMQPOutputHandler

public abstract class ProtocolOutputHandler extends OutputStream
A ProtocolOutputHandler is the complement to a ProtocolInputHandler and responsible for protocol specific packaging before data is sent to the network. The ProtocolOutputHandler is an OutputStream and will be used as such from the Swiftlets. These just write to the OutputStream and call flush() at the end which leads to a call to markChunkCompleted() and then to an invocation of an registered OutputListener which performs the write to the network. This could be done either blocking or non-blocking. The OutputListener returns the number of bytes written to the network and the invocation continues until all bytes have been written.
Author:
IIT GmbH, Bremen/Germany, Copyright (c) 2000-2002, All Rights Reserved
  • Constructor Details

    • ProtocolOutputHandler

      public ProtocolOutputHandler()
  • Method Details

    • setOutputListener

      public final void setOutputListener(OutputListener listener)
      Set the OutputListener. The OutputListener is implemented by the Network Swiftlet.
      Parameters:
      listener - listenr.
    • invokeOutputListener

      public final void invokeOutputListener() throws IOException
      Invokes the OutputListener. Done by the Netwok Swiftlet.
      Throws:
      IOException - on error.
    • write

      public void write(int b) throws IOException
      Specified by:
      write in class OutputStream
      Throws:
      IOException
    • write

      public void write(byte[] b, int offset, int len) throws IOException
      Overrides:
      write in class OutputStream
      Throws:
      IOException
    • flush

      public void flush() throws IOException
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class OutputStream
      Throws:
      IOException
    • create

      public abstract ProtocolOutputHandler create(int bufferSize, int extendSize)
      Factory method to create a new ProtocolOutputHandler. For example, a RawOutputHandler returns a RawOutputHandler here.
      Parameters:
      bufferSize - initial buffer size
      extendSize - extend size
      Returns:
      new protocol input handler.
    • getChunkCount

      public abstract int getChunkCount()
      Returns the number of chunk the handler has stored.
      Returns:
      number of chunks.
    • markChunkCompleted

      protected abstract void markChunkCompleted()
      Marks a chunk completed. Called during flush() If a protocol transmits byte streams with a length field in front, this is the place to determine the length.
    • getByteArray

      protected abstract byte[] getByteArray()
      Returns a reference to the byte array of the current chunk to transmit. Called during invokeOutputListener().
      Returns:
      byte array.
    • getOffset

      protected abstract int getOffset()
      Returns the offset in the byte array of the current chunk where transmit should start. Called during invokeOutputListener().
      Returns:
      offset.
    • getLength

      protected abstract int getLength()
      Returns the number of bytes to transmit, starting at the offset. Called during invokeOutputListener().
      Returns:
      length.
    • setBytesWritten

      protected abstract void setBytesWritten(int written)
      Sets the number of bytes written from the OutputListener. This is the actual number of bytes written from the OutputListener to the network. The offset then must be set to offset+written and the length to length-written. If all bytes of the current chunk have been written, the chunk can be destroyed or marked for reuse.
      Parameters:
      written - number of written bytes.
    • addByte

      protected abstract void addByte(byte b)
      Add a byte to the current chunk. Called from write(b).
      Parameters:
      b - byte.
    • addBytes

      protected abstract void addBytes(byte[] b, int offset, int len)
      Adds a number of bytes to the current chunk. Called from write(b[],offset,len).
      Parameters:
      b - byte array.
      offset - offset.
      len - length.