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.netty;
21  
22  import java.nio.ByteBuffer;
23  
24  import net.gleamynode.netty2.Message;
25  import net.gleamynode.netty2.MessageParseException;
26  import net.gleamynode.netty2.MessageRecognizer;
27  
28  import org.apache.mina.core.buffer.IoBuffer;
29  import org.apache.mina.core.session.IoSession;
30  import org.apache.mina.filter.codec.ProtocolDecoder;
31  import org.apache.mina.filter.codec.ProtocolDecoderAdapter;
32  import org.apache.mina.filter.codec.ProtocolDecoderException;
33  import org.apache.mina.filter.codec.ProtocolDecoderOutput;
34  
35  /**
36   * A MINA {@link ProtocolDecoder} that decodes buffers into
37   * Netty2 {@link Message}s using specified {@link MessageRecognizer}s.
38   *
39   * @author The Apache MINA Project (dev@mina.apache.org)
40   * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $,
41   */
42  public class NettyDecoder extends ProtocolDecoderAdapter {
43      private final MessageRecognizer recognizer;
44  
45      private ByteBuffer readBuf = ByteBuffer.allocate(1024);
46  
47      private Message readingMessage;
48  
49      /**
50       * Creates a new instance with the specified {@link MessageRecognizer}.
51       */
52      public NettyDecoder(MessageRecognizer recognizer) {
53          if (recognizer == null) {
54              throw new NullPointerException();
55          }
56  
57          this.recognizer = recognizer;
58      }
59  
60      private void put(IoBuffer in) {
61          // copy to read buffer
62          if (in.remaining() > readBuf.remaining()) {
63              expand((readBuf.position() + in.remaining()) * 3 / 2);
64          }
65          readBuf.put(in.buf());
66      }
67  
68      private void expand(int newCapacity) {
69          ByteBuffer newBuf = ByteBuffer.allocate(newCapacity);
70          readBuf.flip();
71          newBuf.put(readBuf);
72          readBuf = newBuf;
73      }
74  
75      public void decode(IoSession session, IoBuffer in,
76              ProtocolDecoderOutput out) throws Exception {
77          put(in);
78  
79          Message m = readingMessage;
80          try {
81              for (;;) {
82                  readBuf.flip();
83                  if (m == null) {
84                      int limit = readBuf.limit();
85                      boolean failed = true;
86                      try {
87                          m = recognizer.recognize(readBuf);
88                          failed = false;
89                      } finally {
90                          if (failed) {
91                              // clear the read buffer if failed to recognize
92                              readBuf.clear();
93                              break;
94                          } else {
95                              if (m == null) {
96                                  readBuf.limit(readBuf.capacity());
97                                  readBuf.position(limit);
98                                  break; // finish decoding
99                              } else {
100                                 // reset buffer for read
101                                 readBuf.limit(limit);
102                                 readBuf.position(0);
103                             }
104                         }
105                     }
106                 }
107 
108                 if (m != null) {
109                     try {
110                         if (m.read(readBuf)) {
111                             out.write(m);
112                             m = null;
113                         } else {
114                             break;
115                         }
116                     } finally {
117                         if (readBuf.hasRemaining()) {
118                             readBuf.compact();
119                         } else {
120                             readBuf.clear();
121                             break;
122                         }
123                     }
124                 }
125             }
126         } catch (MessageParseException e) {
127             m = null; // discard reading message
128             throw new ProtocolDecoderException("Failed to decode.", e);
129         } finally {
130             readingMessage = m;
131         }
132     }
133 }