查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.filter.codec;
21  
22  import java.util.Queue;
23  import java.util.concurrent.ConcurrentLinkedQueue;
24  
25  import org.apache.mina.core.buffer.IoBuffer;
26  
27  /**
28   * A {@link ProtocolEncoderOutput} based on queue.
29   *
30   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
31   */
32  public abstract class AbstractProtocolEncoderOutput implements ProtocolEncoderOutput {
33      private final Queue<Object> messageQueue = new ConcurrentLinkedQueue<Object>();
34  
35      private boolean buffersOnly = true;
36  
37      public AbstractProtocolEncoderOutput() {
38          // Do nothing
39      }
40  
41      public Queue<Object> getMessageQueue() {
42          return messageQueue;
43      }
44  
45      public void write(Object encodedMessage) {
46          if (encodedMessage instanceof IoBuffer) {
47              IoBuffer buf = (IoBuffer) encodedMessage;
48              if (buf.hasRemaining()) {
49                  messageQueue.offer(buf);
50              } else {
51                  throw new IllegalArgumentException("buf is empty. Forgot to call flip()?");
52              }
53          } else {
54              messageQueue.offer(encodedMessage);
55              buffersOnly = false;
56          }
57      }
58  
59      public void mergeAll() {
60          if (!buffersOnly) {
61              throw new IllegalStateException("the encoded message list contains a non-buffer.");
62          }
63  
64          final int size = messageQueue.size();
65  
66          if (size < 2) {
67              // no need to merge!
68              return;
69          }
70  
71          // Get the size of merged BB
72          int sum = 0;
73          for (Object b : messageQueue) {
74              sum += ((IoBuffer) b).remaining();
75          }
76  
77          // Allocate a new BB that will contain all fragments
78          IoBuffer newBuf = IoBuffer.allocate(sum);
79  
80          // and merge all.
81          for (;;) {
82              IoBuffer buf = (IoBuffer) messageQueue.poll();
83              if (buf == null) {
84                  break;
85              }
86  
87              newBuf.put(buf);
88          }
89  
90          // Push the new buffer finally.
91          newBuf.flip();
92          messageQueue.add(newBuf);
93      }
94  }