本文共 3578 字,大约阅读时间需要 11 分钟。
AMF目前有两种版本,AMF0和AMF3,他们在数据类型的定义上有细微不同。关于AMF的官方文档参见。
Type | Byte code | Notes |
---|---|---|
Number | 0×00 | |
Boolean | 0×01 | |
String | 0×02 | |
Object | 0×03 | |
MovieClip | 0×04 | Not available in Remoting |
Null | 0×05 | |
Undefined | 0×06 | |
Reference | 0×07 | |
MixedArray | 0×08 | |
EndOfObject | 0×09 | See Object |
Array | 0x0a | |
Date | 0x0b | |
LongString | 0x0c | |
Unsupported | 0x0d | |
Recordset | 0x0e | Remoting, server-to-client only |
XML | 0x0f | |
TypedObject (Class instance) | 0×10 | |
AMF3 data | 0×11 | Sent by Flash player 9+ |
对应的枚举就是:
public enum DataType{ Number = 0x00, // 0 Boolean = 0x01, // 1 String = 0x02, // 2 UntypedObject = 0x03, // 3 MovieClip = 0x04, // 4 Null = 0x05, // 5 Undefined = 0x06, // 6 ReferencedObject = 0x07, // 7 MixedArray = 0x08, // 8 End = 0x09, // 9 Array = 0x10, // 10 Date = 0x11, // 11 LongString = 0x12, // 12 TypeAsObject = 0x13, // 13 Recordset = 0x14, // 14 Xml = 0x15, // 15 TypedObject = 0x16, // 16 AMF3data = 0x17 // 17}
以上表列出了每种数据类型的表示方法,这样看并不容易理解,下面我就主要讲解一下常用的一些格式:
// 这里的顺序是和amf文件中的顺序正好相反,不要忘记了byte[] d = new byte[] { 0, 0, 0, 0, 0, 0, 0x10, 0x40 };double num = BitConverter.ToDouble(d, 0);
// 03 00 08 73 68 61 6E 67 67 75 61byte[] buffer = new byte[] { 0x73, 0x68, 0x61, 0x6E, 0x67, 0x67, 0x75, 0x61 };string str = System.Text.Encoding.UTF8.GetString(buffer);
private Hashtable ReadUntypedObject(){ Hashtable hash = new Hashtable(); string key = ReadShortString(); for (byte type = ReadByte(); type != 9; type = ReadByte()) { hash.Add(key, ReadData(type)); key = ReadShortString(); } return hash;}
private Hashtable ReadDictionary(){ int size = ReadInt32(); Hashtable hash = new Hashtable(size); string key = ReadShortString(); for (byte type = ReadByte(); type != 9; type = ReadByte()) { object value = ReadData(type); hash.Add(key, value); key = ReadShortString(); } return hash;}
private ArrayList ReadArray(){ int size = ReadInt32(); ArrayList arr = new ArrayList(size); for (int i = 0; i < size; ++i) { arr.Add(ReadData(ReadByte())); } return arr;}
private DateTime ReadDate(){ double ms = ReadDouble(); DateTime BaseDate = new DateTime(1970, 1, 1); DateTime date = BaseDate.AddMilliseconds(ms); ReadUInt16(); // get's the timezone return date;}
这里大部分代码我都是摘自AMF.net 一个开源的.net AMF序列化和反序列化的库,大家若有兴趣可以到 去下载。
另外 这个英文网站也对AMF数据类型作了比较详细的介绍。本文转自jiahuafu博客园博客,原文链接http://www.cnblogs.com/jiahuafu/archive/2011/02/21/1959710.html如需转载请自行联系原作者
jiahuafu