Class EncoderEmbedder<E>
- java.lang.Object
-
- org.jboss.netty.handler.codec.embedder.EncoderEmbedder<E>
-
- All Implemented Interfaces:
CodecEmbedder<E>
public class EncoderEmbedder<E> extends Object
A helper that wraps an encoder so that it can be used without doing actual I/O in unit tests or higher level codecs. For example, you can encode aString
into a Base64-encodedChannelBuffer
withBase64Encoder
andStringEncoder
without setting up theChannelPipeline
and other mock objects by yourself:String data = "foobar";
EncoderEmbedder
<ChannelBuffer
> embedder = newEncoderEmbedder
<>( newBase64Encoder
(), newStringEncoder
()); embedder.offer(data);ChannelBuffer
encoded = embedder.poll(); assert encoded.toString(CharsetUtil
.US_ASCII).equals("Zm9vYmFy");- See Also:
DecoderEmbedder
-
-
Constructor Summary
Constructors Constructor Description EncoderEmbedder(ChannelBufferFactory bufferFactory, ChannelDownstreamHandler... handlers)
Creates a new embedder whose pipeline is composed of the specified handlers.EncoderEmbedder(ChannelDownstreamHandler... handlers)
Creates a new embedder whose pipeline is composed of the specified handlers.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
finish()
Signals the pipeline that the encoding or decoding has been finished and no more data will be offered.protected Channel
getChannel()
Returns the virtualChannel
which will be used as a mock during encoding and decoding.ChannelPipeline
getPipeline()
Returns theChannelPipeline
that handles the input.protected boolean
isEmpty()
Returnstrue
if and only if the produce queue is empty and thereforeCodecEmbedder.poll()
will returnnull
.boolean
offer(Object input)
Offers an input object to the pipeline of this embedder.E
peek()
Reads an encoded or decoded output from the head of the product queue.E
poll()
Consumes an encoded or decoded output from the product queue.Object[]
pollAll()
Consumes all encoded or decoded output from the product queue.<T> T[]
pollAll(T[] a)
Consumes all encoded or decoded output from the product queue.int
size()
Returns the number of encoded or decoded output in the product queue.
-
-
-
Constructor Detail
-
EncoderEmbedder
public EncoderEmbedder(ChannelDownstreamHandler... handlers)
Creates a new embedder whose pipeline is composed of the specified handlers.
-
EncoderEmbedder
public EncoderEmbedder(ChannelBufferFactory bufferFactory, ChannelDownstreamHandler... handlers)
Creates a new embedder whose pipeline is composed of the specified handlers.- Parameters:
bufferFactory
- theChannelBufferFactory
to be used when creating a new buffer.
-
-
Method Detail
-
offer
public boolean offer(Object input)
Description copied from interface:CodecEmbedder
Offers an input object to the pipeline of this embedder.- Returns:
true
if and only if there is something to read in the product queue (seeCodecEmbedder.poll()
andCodecEmbedder.peek()
)
-
finish
public boolean finish()
Description copied from interface:CodecEmbedder
Signals the pipeline that the encoding or decoding has been finished and no more data will be offered.- Specified by:
finish
in interfaceCodecEmbedder<E>
- Returns:
true
if and only if there is something to read in the product queue (seeCodecEmbedder.poll()
andCodecEmbedder.peek()
)
-
getChannel
protected final Channel getChannel()
Returns the virtualChannel
which will be used as a mock during encoding and decoding.
-
isEmpty
protected final boolean isEmpty()
Returnstrue
if and only if the produce queue is empty and thereforeCodecEmbedder.poll()
will returnnull
.
-
poll
public final E poll()
Description copied from interface:CodecEmbedder
Consumes an encoded or decoded output from the product queue. The output object is generated by the offered input objects.- Specified by:
poll
in interfaceCodecEmbedder<E>
- Returns:
- an encoded or decoded object.
null
if and only if there is no output object left in the product queue.
-
peek
public final E peek()
Description copied from interface:CodecEmbedder
Reads an encoded or decoded output from the head of the product queue. The difference fromCodecEmbedder.poll()
is that it does not remove the retrieved object from the product queue.- Specified by:
peek
in interfaceCodecEmbedder<E>
- Returns:
- an encoded or decoded object.
null
if and only if there is no output object left in the product queue.
-
pollAll
public final Object[] pollAll()
Description copied from interface:CodecEmbedder
Consumes all encoded or decoded output from the product queue. The output object is generated by the offered input objects. The behavior of this method is identical withCollection.toArray()
except that the product queue is cleared.- Specified by:
pollAll
in interfaceCodecEmbedder<E>
- Returns:
- an array of all encoded or decoded objects. An empty array is returned if and only if there is no output object left in the product queue.
-
pollAll
public final <T> T[] pollAll(T[] a)
Description copied from interface:CodecEmbedder
Consumes all encoded or decoded output from the product queue. The output object is generated by the offered input objects. The behavior of this method is identical withCollection.toArray(Object[])
except that the product queue is cleared.- Specified by:
pollAll
in interfaceCodecEmbedder<E>
- Returns:
- an array of all encoded or decoded objects. An empty array is returned if and only if there is no output object left in the product queue.
-
size
public final int size()
Description copied from interface:CodecEmbedder
Returns the number of encoded or decoded output in the product queue.- Specified by:
size
in interfaceCodecEmbedder<E>
-
getPipeline
public ChannelPipeline getPipeline()
Description copied from interface:CodecEmbedder
Returns theChannelPipeline
that handles the input.- Specified by:
getPipeline
in interfaceCodecEmbedder<E>
-
-