1 /*
2 * @(#)AMF3Decoder.java 0.1 05/11/17
3 *
4 * Copyright 2010 QISI, Inc. All rights reserved.
5 * QISI PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6 */
7 package com.qidea.pushserver.codec;
8 import java.io.ByteArrayInputStream;
9 import org.jboss.netty.buffer.ChannelBuffer;
10 import org.jboss.netty.channel.Channel;
11 import org.jboss.netty.channel.ChannelHandlerContext;
12 import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15 import flex.messaging.io.SerializationContext;
16 import flex.messaging.io.amf.Amf3Input;
17 /**
18 * amf3协议解码类
19 *
20 * @author sunwei
21 * @version 2010-7-21
22 * @since JDK1.5
23 */
24 public class AMF3Decoder extends LengthFieldBasedFrameDecoder
25 {
26 public static final Logger logger = LoggerFactory
27 .getLogger(AMF3Decoder.class);
28 /**
29 *
30 * @param maxFrameLength
31 * 包的最大大小
32 * @param lengthFieldOffset
33 * 包头信息,长度的偏移位
34 * @param lengthFieldLength
35 * 包头信息,长度位数
36 */
37 public AMF3Decoder(int maxFrameLength, int lengthFieldOffset,
38 int lengthFieldLength)
39 {
40 super(maxFrameLength, lengthFieldOffset, lengthFieldLength);
41 }
42 /**
43 *
44 * @param maxFrameLength
45 */
46 public AMF3Decoder(int maxFrameLength)
47 {
48 super(maxFrameLength, 4, 4, 0, 0);
49 }
50 /**
51 *
52 */
53 @Override
54 protected Object decode(ChannelHandlerContext ctx, Channel channel,
55 ChannelBuffer buffer) throws Exception
56 {
57 ChannelBuffer frame = (ChannelBuffer) super
58 .decode(ctx, channel, buffer);
59 if (frame == null)
60 {
61 return null;
62 }
63 //
64 int magicNum = frame.readInt();
65 int dataLength = frame.readInt();
66 logger.info("magic num={},data length={}", magicNum, dataLength);
67 // 读AMF3字节流的内容
68 byte[] content = new byte[frame.readableBytes()];
69 frame.readBytes(content);
70 SerializationContext serializationContext = new SerializationContext();
71 Amf3Input amf3Input = new Amf3Input(serializationContext);
72 amf3Input.setInputStream(new ByteArrayInputStream(content));
73 Object message = amf3Input.readObject();
74 return message;
75 }
76 }
77