AMF序列格式详细介绍



AMF目前有两种版本,AMF0和AMF3,他们在数据类型的定义上有细微不同。关于AMF的官方文档参见这里

 

TypeByte codeNotes
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 0x0eRemoting, server-to-client only
XML 0x0f
TypedObject (Class instance) 0×10
AMF3 data 0×11Sent 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
}

以上表列出了每种数据类型的表示方法,这样看并不容易理解,下面我就主要讲解一下常用的一些格式:
0.Number这里指的是double类型,数据用8字节表示,比如十六进制00 40 10 00 00 00 00 00 00就表示的是一个double数4.0,在C#中可以使用如下代码读取该数据:

// 这里的顺序是和amf文件中的顺序正好相反,不要忘记了
byte[] d = new byte[] { 0, 0, 0, 0, 0, 0, 0x10, 0x40 };
double num = BitConverter.ToDouble(d, 0);

1.Boolean对应的是.net中的bool类型,数据使用1字节表示,和C语言差不多,使用00表示false,使用01表示true。比如十六进制01 01就表示true。
2.String相当于.net中的string类型,String所占用的空间有1个类型标识字节和2个表示字符串UTF8长度的字节加上字符串UTF8格式的内容组成。比如十六进制03 00 08 73 68 61 6E 67 67 75 61表示的就是字符串,该字符串长8字节,字符串内容为73 68 61 6E 67 67 75 61,对应的就是“shanggua”。在C#中要读取字符串则使用:

// 03 00 08 73 68 61 6E 67 67 75 61
byte[] buffer = new byte[] { 0x73, 0x68, 0x61, 0x6E, 0x67, 0x67, 0x75, 0x61 };
string str = System.Text.Encoding.UTF8.GetString(buffer);

3.Object在.net中对应的就是Hashtable,内容由UTF8字符串作为Key,其他AMF类型作为Value,该对象由3个字节:00 00 09来表示结束。C#中读取该对象使用如下方法:

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;
}

4.MovieClip  Not available in Remoting
5.Null 就是空对象,该对象只占用一个字节,那就是Null对象标识0x05。
6.Undefined 也是只占用一个字节0x06.
7.ReferencedObject  不清楚(可能是类型定义什么的)
8.MixedArray 相当于Hashtable,与3不同的是该对象定义了Hashtable的大小。读取该对象的C#代码是:

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;
}

9.End 查看Object
10.Array对应的就是.net中的ArrayList对象,该对象首先使用32位整数定义了ArralyList的长度,然后是密集的跟着ArrayList中的对象,读取该对象使用如下函数:

private ArrayList ReadArray()
{
 int size = ReadInt32();
 ArrayList arr = new ArrayList(size);
 for (int i = 0; i < size; ++i)
 {
  arr.Add(ReadData(ReadByte()));
 }
 return arr;
}

11.Date 对应.net中的DateTime数据类型,Date在类型标识符0x0B后使用double来表示从1970/1/1到表示的时间所经过的毫秒数,然后再跟一个ushort的16位无符号整数表示时区。读取Date类型的C#代码为:

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;
}

12.LongString对应的也是string类型,不过和2对应的String不同的是这里使用32位整数来表示字符串的UTF8长度,而String使用的是16位。
13.TypeAsObject
14.Recordset
15.XML是使用类型标识符0x0F后直接跟LongString类型的字符串表示。
16.TypedObject
17.AMF3data

这里大部分代码我都是摘自AMF.net 一个开源的.net AMF序列化和反序列化的库,大家若有兴趣可以到http://sourceforge.net/project/showfiles.php?group_id=159742 去下载。
另外http://osflash.org/documentation/amf/astypes 这个英文网站也对AMF数据类型作了比较详细的介绍。


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值