Class ProtocolOutputHandler

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable
    Direct Known Subclasses:
    RawOutputHandler, SMQPOutputHandler

    public abstract class ProtocolOutputHandler
    extends java.io.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
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      protected abstract void addByte​(byte b)
      Add a byte to the current chunk.
      protected abstract void addBytes​(byte[] b, int offset, int len)
      Adds a number of bytes to the current chunk.
      abstract ProtocolOutputHandler create()
      Factory method to create a new ProtocolOutputHandler.
      abstract ProtocolOutputHandler create​(int bufferSize, int extendSize)
      Factory method to create a new ProtocolOutputHandler.
      void flush()  
      protected abstract byte[] getByteArray()
      Returns a reference to the byte array of the current chunk to transmit.
      abstract int getChunkCount()
      Returns the number of chunk the handler has stored.
      protected abstract int getLength()
      Returns the number of bytes to transmit, starting at the offset.
      protected abstract int getOffset()
      Returns the offset in the byte array of the current chunk where transmit should start.
      void invokeOutputListener()
      Invokes the OutputListener.
      protected abstract void markChunkCompleted()
      Marks a chunk completed.
      protected abstract void setBytesWritten​(int written)
      Sets the number of bytes written from the OutputListener.
      void setOutputListener​(OutputListener listener)
      Set the OutputListener.
      void write​(byte[] b, int offset, int len)  
      void write​(int b)  
      • Methods inherited from class java.io.OutputStream

        close, nullOutputStream, write
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ProtocolOutputHandler

        public ProtocolOutputHandler()
    • Method Detail

      • 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 java.io.IOException
        Invokes the OutputListener. Done by the Netwok Swiftlet.
        Throws:
        java.io.IOException - on error.
      • write

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

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

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

        public abstract ProtocolOutputHandler create()
        Factory method to create a new ProtocolOutputHandler. For example, a RawOutputHandler returns a RawOutputHandler here.
        Returns:
        new protocol input handler.
      • 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.