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   */
41  public class NettyDecoder extends ProtocolDecoderAdapter {
42      private final MessageRecognizer recognizer;
43  
44      private ByteBuffer readBuf = ByteBuffer.allocate(1024);
45  
46      private Message readingMessage;
47  
48      /**
49       * Creates a new instance with the specified {@link MessageRecognizer}.
50       */
51      public NettyDecoder(MessageRecognizer recognizer) {
52          if (recognizer == null) {
53              throw new NullPointerException();
54          }
55  
56          this.recognizer = recognizer;
57      }
58  
59      private void put(IoBuffer in) {
60          // copy to read buffer
61          if (in.remaining() > readBuf.remaining()) {
62              expand((readBuf.position() + in.remaining()) * 3 / 2);
63          }
64          readBuf.put(in.buf());
65      }
66  
67      private void expand(int newCapacity) {
68          ByteBuffer newBuf = ByteBuffer.allocate(newCapacity);
69          readBuf.flip();
70          newBuf.put(readBuf);
71          readBuf = newBuf;
72      }
73  
74      public void decode(IoSession session, IoBuffer in,
75              ProtocolDecoderOutput out) throws Exception {
76          put(in);
77  
78          Message m = readingMessage;
79          try {
80              for (;;) {
81                  readBuf.flip();
82                  if (m == null) {
83                      int limit = readBuf.limit();
84                      boolean failed = true;
85                      try {
86                          m = recognizer.recognize(readBuf);
87                          failed = false;
88                      } finally {
89                          if (failed) {
90                              // clear the read buffer if failed to recognize
91                              readBuf.clear();
92                              break;
93                          } else {
94                              if (m == null) {
95                                  readBuf.limit(readBuf.capacity());
96                                  readBuf.position(limit);
97                                  break; // finish decoding
98                              } else {
99                                  // reset buffer for read
100                                 readBuf.limit(limit);
101                                 readBuf.position(0);
102                             }
103                         }
104                     }
105                 }
106 
107                 if (m != null) {
108                     try {
109                         if (m.read(readBuf)) {
110                             out.write(m);
111                             m = null;
112                         } else {
113                             break;
114                         }
115                     } finally {
116                         if (readBuf.hasRemaining()) {
117                             readBuf.compact();
118                         } else {
119                             readBuf.clear();
120                             break;
121                         }
122                     }
123                 }
124             }
125         } catch (MessageParseException e) {
126             m = null; // discard reading message
127             throw new ProtocolDecoderException("Failed to decode.", e);
128         } finally {
129             readingMessage = m;
130         }
131     }
132 }