View Javadoc

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.support;
21  
22  import org.apache.mina.common.ByteBuffer;
23  import org.apache.mina.common.WriteFuture;
24  import org.apache.mina.filter.codec.ProtocolEncoderOutput;
25  import org.apache.mina.util.Queue;
26  
27  /**
28   * A {@link ProtocolEncoderOutput} based on queue.
29   *
30   * @author The Apache Directory Project (mina-dev@directory.apache.org)
31   * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (Fri, 13 Jul 2007) $
32   */
33  public abstract class SimpleProtocolEncoderOutput implements
34          ProtocolEncoderOutput {
35      private final Queue bufferQueue = new Queue();
36  
37      public SimpleProtocolEncoderOutput() {
38      }
39  
40      public Queue getBufferQueue() {
41          return bufferQueue;
42      }
43  
44      public void write(ByteBuffer buf) {
45          bufferQueue.push(buf);
46      }
47  
48      public void mergeAll() {
49          int sum = 0;
50          final int size = bufferQueue.size();
51  
52          if (size < 2) {
53              // no need to merge!
54              return;
55          }
56  
57          // Get the size of merged BB
58          for (int i = size - 1; i >= 0; i--) {
59              sum += ((ByteBuffer) bufferQueue.get(i)).remaining();
60          }
61  
62          // Allocate a new BB that will contain all fragments
63          ByteBuffer newBuf = ByteBuffer.allocate(sum);
64  
65          // and merge all.
66          for (;;) {
67              ByteBuffer buf = (ByteBuffer) bufferQueue.pop();
68              if (buf == null) {
69                  break;
70              }
71  
72              newBuf.put(buf);
73              buf.release();
74          }
75  
76          // Push the new buffer finally.
77          newBuf.flip();
78          bufferQueue.push(newBuf);
79      }
80  
81      public WriteFuture flush() {
82          Queue bufferQueue = this.bufferQueue;
83          WriteFuture future = null;
84          if (bufferQueue.isEmpty()) {
85              return null;
86          } else {
87              for (;;) {
88                  ByteBuffer buf = (ByteBuffer) bufferQueue.pop();
89                  if (buf == null) {
90                      break;
91                  }
92  
93                  // Flush only when the buffer has remaining.
94                  if (buf.hasRemaining()) {
95                      future = doFlush(buf);
96                  }
97              }
98          }
99  
100         return future;
101     }
102 
103     protected abstract WriteFuture doFlush(ByteBuffer buf);
104 }