Package fr.dyade.aaa.agent
Class MessageOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- fr.dyade.aaa.agent.MessageOutputStream
-
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
- Direct Known Subclasses:
BufferedMessageOutputStream,ByteArrayMessageOutputStream,UDPNetwork.DatagramOutputStream
public abstract class MessageOutputStream extends OutputStream
Class used to send messages through a stream.This OutputStream is a combination between a ByteArrayOutputStream and a BufferedOutputStream. It allows the replacement of the underlying stream and the serialization of object through an internal ObjectOutputStream.
Be careful this OutputStream is not synchronized.
-
-
Field Summary
Fields Modifier and Type Field Description protected byte[]bufThe internal buffer where data is stored.protected booleancompressedFlowsprotected intcountThe number of valid bytes in the buffer.protected ObjectOutputStreamoosThe internal ObjectOutputStream needed to serialize the notification.private static byteSTREAM_MAGIC1private static byteSTREAM_MAGIC2private static byteSTREAM_VERSION1private static byteSTREAM_VERSION2
-
Constructor Summary
Constructors Constructor Description MessageOutputStream()Creates a new output stream to write data to an unspecified underlying output stream through a buffer with default size.MessageOutputStream(int size)Creates a new output stream to write data to an unspecified underlying output stream through a buffer with specified size.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidwrite(byte[] b)Writesb.lengthbytes to this output stream.abstract voidwrite(byte[] b, int off, int len)Writeslenbytes from the specified byte array starting at offsetoffto this output stream.abstract voidwrite(int b)Writes the specified byte to this output stream.protected abstract voidwriteHeader()Writes the protocol header to this output stream.protected voidwriteInt(int i)Writes an int directly to the buffer.protected voidwriteMessage(Message msg, long time)Writes a message to this output stream.protected voidwriteMessageHeader(Message msg)Writes the message header data to the buffer.protected voidwriteShort(short s)Writes a short directly to the buffer.-
Methods inherited from class java.io.OutputStream
close, flush, nullOutputStream
-
-
-
-
Field Detail
-
oos
protected ObjectOutputStream oos
The internal ObjectOutputStream needed to serialize the notification.
-
buf
protected byte[] buf
The internal buffer where data is stored.
-
count
protected int count
The number of valid bytes in the buffer.
-
compressedFlows
protected boolean compressedFlows
-
STREAM_MAGIC1
private static final byte STREAM_MAGIC1
- See Also:
- Constant Field Values
-
STREAM_MAGIC2
private static final byte STREAM_MAGIC2
- See Also:
- Constant Field Values
-
STREAM_VERSION1
private static final byte STREAM_VERSION1
- See Also:
- Constant Field Values
-
STREAM_VERSION2
private static final byte STREAM_VERSION2
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
MessageOutputStream
public MessageOutputStream() throws IOExceptionCreates a new output stream to write data to an unspecified underlying output stream through a buffer with default size.- Throws:
IOException- an error occurs.
-
MessageOutputStream
public MessageOutputStream(int size) throws IOExceptionCreates a new output stream to write data to an unspecified underlying output stream through a buffer with specified size.- Parameters:
size- the buffer size.- Throws:
IllegalArgumentException- if size is less than 0.IOException- if the internal ObjectOutputStream cannot be correctly initialized.
-
-
Method Detail
-
write
public abstract void write(int b) throws IOExceptionWrites the specified byte to this output stream.- Specified by:
writein classOutputStream- Parameters:
b- the byte to be written.- Throws:
IOException- if an I/O error occurs.
-
write
public final void write(byte[] b) throws IOExceptionWritesb.lengthbytes to this output stream.This method calls its
writemethod of three arguments with the argumentsb,0, andb.length.- Overrides:
writein classOutputStream- Parameters:
b- the data to be written.- Throws:
IOException- if an I/O error occurs.- See Also:
OutputStream.write(byte[], int, int)
-
write
public abstract void write(byte[] b, int off, int len) throws IOExceptionWriteslenbytes from the specified byte array starting at offsetoffto this output stream.- Overrides:
writein classOutputStream- Parameters:
b- the data.off- the start offset in the data.len- the number of bytes to write.- Throws:
IOException- if an I/O error occurs.
-
writeShort
protected final void writeShort(short s)
Writes a short directly to the buffer. Be careful, the buffer must be large enough to contain the short.- Parameters:
s- the data.
-
writeInt
protected final void writeInt(int i)
Writes an int directly to the buffer. Be careful, the buffer must be large enough to contain the int.- Parameters:
i- the data.
-
writeHeader
protected abstract void writeHeader() throws IOExceptionWrites the protocol header to this output stream. This method must be overloaded in subclass.- Throws:
IOException- if an I/O error occurs.
-
writeMessageHeader
protected final void writeMessageHeader(Message msg)
Writes the message header data to the buffer.- Parameters:
msg- The message to write out.
-
writeMessage
protected final void writeMessage(Message msg, long time) throws IOException
Writes a message to this output stream. This method can be overloaded in subclass.- Parameters:
msg- The message to write out.time- The current time in milliseconds, this parameter is used to the handling of notification expiration.- Throws:
IOException- if an I/O error occurs.
-
-