zoukankan      html  css  js  c++  java
  • AMF目前有两种版本,AMF0和AMF3

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

    以上表列出了每种数据类型的表示方法,这样看并不容易理解,下面我就主要讲解一下常用的一些格式:

    1. 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);
    2. Boolean对应的是.net中的bool类型,数据使用1字节表示,和C语言差不多,使用00表示false,使用01表示true。比如十六进制01 01就表示true。
    3. 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);
    4. 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;
      }
    5. MovieClip  Not available in Remoting
    6. Null 就是空对象,该对象只占用一个字节,那就是Null对象标识0x05。
    7. Undefined 也是只占用一个字节0x06.
    8. ReferencedObject  不清楚(可能是类型定义什么的)
    9. 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;
      }
    10. End 查看Object
    11. 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;
      }
    12. 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;
      }
    13. LongString对应的也是string类型,不过和2对应的String不同的是这里使用32位整数来表示字符串的UTF8长度,而String使用的是16位。
    14. TypeAsObject
    15. Recordset
    16. XML是使用类型标识符0x0F后直接跟LongString类型的字符串表示。
    17. TypedObject
    18. AMF3data

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

  • 相关阅读:
    html 上传图片前预览
    php获取当月天数及当月第一天及最后一天、上月第一天及最后一天实现方法
    php 计算 pdf文件页数
    php 获取半年内每个月的订单数量, 总价, 月份
    php 获取两个数组之间不同的值
    小程序支付功能
    关于nginx的Job for nginx.service failed because the control process exited with error code.错误
    linux 安装 Apollo
    MongoDB待续。。。
    ABP vNext...待续
  • 原文地址:https://www.cnblogs.com/jiahuafu/p/1959710.html
Copyright © 2011-2022 走看看