1 /***
2 *
3 */
4 package org.apache.mina.protocol;
5
6 import org.apache.mina.common.ByteBuffer;
7 import org.apache.mina.protocol.ProtocolEncoderOutput;
8 import org.apache.mina.util.Queue;
9
10 /***
11 * A {@link ProtocolEncoderOutput} based on queue.
12 *
13 * @author The Apache Directory Project (dev@directory.apache.org)
14 * @version $Rev: 357871 $, $Date: 2005-12-20 10:56:40 +0900 (Tue, 20 Dec 2005) $
15 */
16 public class SimpleProtocolEncoderOutput implements ProtocolEncoderOutput
17 {
18
19 private final Queue bufferQueue = new Queue();
20
21 public SimpleProtocolEncoderOutput()
22 {
23 }
24
25 public Queue getBufferQueue()
26 {
27 return bufferQueue;
28 }
29
30 public void write( ByteBuffer buf )
31 {
32 bufferQueue.push( buf );
33 }
34
35 public void mergeAll()
36 {
37 int sum = 0;
38 final int size = bufferQueue.size();
39
40 if( size < 2 )
41 {
42
43 return;
44 }
45
46
47 for( int i = size - 1; i >= 0; i -- )
48 {
49 sum += ( ( ByteBuffer ) bufferQueue.get( i ) ).remaining();
50 }
51
52
53 ByteBuffer newBuf = ByteBuffer.allocate( sum );
54
55
56 for( ;; )
57 {
58 ByteBuffer buf = ( ByteBuffer ) bufferQueue.pop();
59 if( buf == null )
60 {
61 break;
62 }
63
64 newBuf.put( buf );
65 buf.release();
66 }
67
68
69 newBuf.flip();
70 bufferQueue.push(newBuf);
71 }
72 }