查看本类的 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 java.util.Collection;
19  
20  import org.jboss.netty.channel.ChannelPipeline;
21  
22  /**
23   * A helper that wraps an encoder or a decoder (codec) so that they can be used
24   * without doing actual I/O in unit tests or higher level codecs.  Please refer
25   * to {@link EncoderEmbedder} and {@link DecoderEmbedder} for more information.
26   */
27  public interface CodecEmbedder<E> {
28      /**
29       * Offers an input object to the pipeline of this embedder.
30       *
31       * @return {@code true} if and only if there is something to read in the
32       *         product queue (see {@link #poll()} and {@link #peek()})
33       */
34      boolean offer(Object input);
35  
36      /**
37       * Signals the pipeline that the encoding or decoding has been finished and
38       * no more data will be offered.
39       *
40       * @return {@code true} if and only if there is something to read in the
41       *         product queue (see {@link #poll()} and {@link #peek()})
42       */
43      boolean finish();
44  
45      /**
46       * Consumes an encoded or decoded output from the product queue. The output
47       * object is generated by the offered input objects.
48       *
49       * @return an encoded or decoded object.
50       *         {@code null} if and only if there is no output object left in the
51       *         product queue.
52       */
53      E poll();
54  
55      /**
56       * Reads an encoded or decoded output from the head of the product queue.
57       * The difference from {@link #poll()} is that it does not remove the
58       * retrieved object from the product queue.
59       *
60       * @return an encoded or decoded object.
61       *         {@code null} if and only if there is no output object left in the
62       *         product queue.
63       */
64      E peek();
65  
66      /**
67       * Consumes all encoded or decoded output from the product queue.  The
68       * output object is generated by the offered input objects.  The behavior
69       * of this method is identical with {@link Collection#toArray()} except that
70       * the product queue is cleared.
71       *
72       * @return an array of all encoded or decoded objects.
73       *         An empty array is returned if and only if there is no output
74       *         object left in the product queue.
75       */
76      Object[] pollAll();
77  
78      /**
79       * Consumes all encoded or decoded output from the product queue.  The
80       * output object is generated by the offered input objects.  The behavior
81       * of this method is identical with {@link Collection#toArray(Object[])}
82       * except that the product queue is cleared.
83       *
84       * @return an array of all encoded or decoded objects.
85       *         An empty array is returned if and only if there is no output
86       *         object left in the product queue.
87       */
88      <T> T[] pollAll(T[] a);
89  
90      /**
91       * Returns the number of encoded or decoded output in the product queue.
92       */
93      int size();
94  
95      /**
96       * Returns the {@link ChannelPipeline} that handles the input.
97       */
98      ChannelPipeline getPipeline();
99  }