Package rocks.xmpp.util
Class XmppStreamEncoder
- java.lang.Object
-
- rocks.xmpp.util.XmppStreamEncoder
-
- All Implemented Interfaces:
WriterInterceptor
public final class XmppStreamEncoder extends Object implements WriterInterceptor
Encodes XMPP elements to binary data.This class is capable to encode elements to either an
OutputStream
or to aByteBuffer
.Encoding is thread-safe, as long as the supplied
Marshaller
is not shared by another thread, e.g. if a thread-localMarshaller
is supplied.
-
-
Constructor Summary
Constructors Constructor Description XmppStreamEncoder(XMLOutputFactory outputFactory, Supplier<javax.xml.bind.Marshaller> marshaller, Function<StreamElement,Boolean> writeStreamNamespace)
Creates the XMPP encoder.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ByteBuffer
encode(StreamElement streamElement)
Encodes an XMPP element to a byte buffer.void
encode(StreamElement streamElement, Writer writer)
Encodes an XMPP element to aWriter
.void
process(StreamElement streamElement, Writer writer, WriterInterceptorChain chain)
The stream element which will be written.
-
-
-
Constructor Detail
-
XmppStreamEncoder
public XmppStreamEncoder(XMLOutputFactory outputFactory, Supplier<javax.xml.bind.Marshaller> marshaller, Function<StreamElement,Boolean> writeStreamNamespace)
Creates the XMPP encoder.Because
Marshaller
is not thread-safe, it is recommended to pass aThreadLocal<Marshaller>
to this constructor, which ensures thread-safety during marshalling.- Parameters:
outputFactory
- The XML output factory.marshaller
- Supplies the marshaller which will convert objects to XML.writeStreamNamespace
- If the stream namespace should be written in the root element.
-
-
Method Detail
-
encode
public final ByteBuffer encode(StreamElement streamElement) throws StreamErrorException
Encodes an XMPP element to a byte buffer.The returned
ByteBuffer
is ready to be read, i.e. its position is 0.- Parameters:
streamElement
- The stream element.- Returns:
- The byte buffer.
- Throws:
StreamErrorException
- If the element could not be marshalled.
-
encode
public final void encode(StreamElement streamElement, Writer writer) throws StreamErrorException
Encodes an XMPP element to aWriter
.- Parameters:
streamElement
- The stream element.writer
- The writer to write to.- Throws:
StreamErrorException
- If the element could not be marshalled.
-
process
public void process(StreamElement streamElement, Writer writer, WriterInterceptorChain chain) throws Exception
Description copied from interface:WriterInterceptor
The stream element which will be written.- Specified by:
process
in interfaceWriterInterceptor
- Parameters:
streamElement
- The stream element which will be written.writer
- The writer to which the stream element is written.chain
- The writer chain, which allows to proceed to the next interceptor.- Throws:
Exception
- Any exception happening during interception.
-
-