查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.handler.codec.embedder;
17  
18  import static org.jboss.netty.channel.Channels.*;
19  
20  import org.jboss.netty.buffer.ChannelBuffer;
21  import org.jboss.netty.buffer.ChannelBufferFactory;
22  import org.jboss.netty.buffer.ChannelBuffers;
23  import org.jboss.netty.channel.ChannelPipeline;
24  import org.jboss.netty.channel.ChannelUpstreamHandler;
25  import org.jboss.netty.handler.codec.base64.Base64Decoder;
26  import org.jboss.netty.handler.codec.string.StringDecoder;
27  
28  /**
29   * A helper that wraps a decoder so that it can be used without doing actual
30   * I/O in unit tests or higher level codecs.  For example, you can decode a
31   * Base64-encoded {@link ChannelBuffer} with {@link Base64Decoder} and
32   * {@link StringDecoder} without setting up the {@link ChannelPipeline} and
33   * other mock objects by yourself:
34   * <pre>
35   * {@link ChannelBuffer} base64Data = {@link ChannelBuffers}.copiedBuffer("Zm9vYmFy", CharsetUtil.US_ASCII);
36   *
37   * {@link DecoderEmbedder}&lt;String&gt; embedder = new {@link DecoderEmbedder}&lt;String&gt;(
38   *         new {@link Base64Decoder}(), new {@link StringDecoder}());
39   *
40   * embedder.offer(base64Data);
41   *
42   * String decoded = embedder.poll();
43   * assert decoded.equals("foobar");
44   * </pre>
45   * @apiviz.landmark
46   * @see EncoderEmbedder
47   */
48  public class DecoderEmbedder<E> extends AbstractCodecEmbedder<E> {
49  
50      /**
51       * Creates a new embedder whose pipeline is composed of the specified
52       * handlers.
53       */
54      public DecoderEmbedder(ChannelUpstreamHandler... handlers) {
55          super(handlers);
56      }
57  
58      /**
59       * Creates a new embedder whose pipeline is composed of the specified
60       * handlers.
61       *
62       * @param bufferFactory the {@link ChannelBufferFactory} to be used when
63       *                      creating a new buffer.
64       */
65      public DecoderEmbedder(ChannelBufferFactory bufferFactory, ChannelUpstreamHandler... handlers) {
66          super(bufferFactory, handlers);
67      }
68  
69      public boolean offer(Object input) {
70          fireMessageReceived(getChannel(), input);
71          return !isEmpty();
72      }
73  }