1 20 package org.apache.mina.filter.codec.netty; 21 22 import net.gleamynode.netty2.Message; 23 import net.gleamynode.netty2.MessageParseException; 24 import net.gleamynode.netty2.MessageRecognizer; 25 26 import org.apache.mina.common.ByteBuffer; 27 import org.apache.mina.common.IoSession; 28 import org.apache.mina.filter.codec.ProtocolDecoder; 29 import org.apache.mina.filter.codec.ProtocolDecoderAdapter; 30 import org.apache.mina.filter.codec.ProtocolDecoderException; 31 import org.apache.mina.filter.codec.ProtocolDecoderOutput; 32 33 40 public class NettyDecoder extends ProtocolDecoderAdapter { 41 private final MessageRecognizer recognizer; 42 43 private java.nio.ByteBuffer readBuf = java.nio.ByteBuffer.allocate(1024); 44 45 private Message readingMessage; 46 47 50 public NettyDecoder(MessageRecognizer recognizer) { 51 if (recognizer == null) 52 throw new NullPointerException (); 53 54 this.recognizer = recognizer; 55 } 56 57 private void put(ByteBuffer in) { 58 if (in.remaining() > readBuf.remaining()) 60 expand((readBuf.position() + in.remaining()) * 3 / 2); 61 readBuf.put(in.buf()); 62 } 63 64 private void expand(int newCapacity) { 65 java.nio.ByteBuffer newBuf = java.nio.ByteBuffer.allocate(newCapacity); 66 readBuf.flip(); 67 newBuf.put(readBuf); 68 readBuf = newBuf; 69 } 70 71 public void decode(IoSession session, ByteBuffer in, 72 ProtocolDecoderOutput out) throws Exception { 73 put(in); 74 75 Message m = readingMessage; 76 try { 77 for (;;) { 78 readBuf.flip(); 79 if (m == null) { 80 int limit = readBuf.limit(); 81 boolean failed = true; 82 try { 83 m = recognizer.recognize(readBuf); 84 failed = false; 85 } finally { 86 if (failed) { 87 readBuf.clear(); 89 break; 90 } else { 91 if (m == null) { 92 readBuf.limit(readBuf.capacity()); 93 readBuf.position(limit); 94 break; } else { 96 readBuf.limit(limit); 98 readBuf.position(0); 99 } 100 } 101 } 102 } 103 104 if (m != null) { 105 try { 106 if (m.read(readBuf)) { 107 out.write(m); 108 m = null; 109 } else { 110 break; 111 } 112 } finally { 113 if (readBuf.hasRemaining()) { 114 readBuf.compact(); 115 } else { 116 readBuf.clear(); 117 break; 118 } 119 } 120 } 121 } 122 } catch (MessageParseException e) { 123 m = null; throw new ProtocolDecoderException("Failed to decode.", e); 125 } finally { 126 readingMessage = m; 127 } 128 } 129 } 130 | Popular Tags |