Package com.swiftmq.net.protocol
Class ProtocolOutputHandler
java.lang.Object
java.io.OutputStream
com.swiftmq.net.protocol.ProtocolOutputHandler
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
- Direct Known Subclasses:
RawOutputHandler
,SMQPOutputHandler
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected 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
(int bufferSize, int extendSize) Factory method to create a new ProtocolOutputHandler.void
flush()
protected abstract byte[]
Returns a reference to the byte array of the current chunk to transmit.abstract int
Returns the number of chunk the handler has stored.protected abstract int
Returns the number of bytes to transmit, starting at the offset.protected abstract int
Returns the offset in the byte array of the current chunk where transmit should start.final void
Invokes the OutputListener.protected abstract void
Marks a chunk completed.protected abstract void
setBytesWritten
(int written) Sets the number of bytes written from the OutputListener.final 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
-
Constructor Details
-
ProtocolOutputHandler
public ProtocolOutputHandler()
-
-
Method Details
-
setOutputListener
Set the OutputListener. The OutputListener is implemented by the Network Swiftlet.- Parameters:
listener
- listenr.
-
invokeOutputListener
Invokes the OutputListener. Done by the Netwok Swiftlet.- Throws:
IOException
- on error.
-
write
- Specified by:
write
in classOutputStream
- Throws:
IOException
-
write
- Overrides:
write
in classOutputStream
- Throws:
IOException
-
flush
- Specified by:
flush
in interfaceFlushable
- Overrides:
flush
in classOutputStream
- Throws:
IOException
-
create
Factory method to create a new ProtocolOutputHandler. For example, a RawOutputHandler returns a RawOutputHandler here.- Parameters:
bufferSize
- initial buffer sizeextendSize
- 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 duringflush()
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 duringinvokeOutputListener()
.- Returns:
- byte array.
-
getOffset
protected abstract int getOffset()Returns the offset in the byte array of the current chunk where transmit should start. Called duringinvokeOutputListener()
.- Returns:
- offset.
-
getLength
protected abstract int getLength()Returns the number of bytes to transmit, starting at the offset. Called duringinvokeOutputListener()
.- 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 tooffset+written
and the length tolength-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 fromwrite(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 fromwrite(b[],offset,len)
.- Parameters:
b
- byte array.offset
- offset.len
- length.
-