protected static void WriteAmf(Stream stm, object obj) { if (obj == null) { stm.WriteByte((byte)DataType.Null); } else if (obj is byte || obj is int || obj is uint || obj is float || obj is double) // 只列举了常用的 { stm.WriteByte((byte)DataType.Number); CDataHelper.BE_WriteDouble(stm, double.Parse(obj.ToString())); } else if (obj is bool) { stm.WriteByte((byte)DataType.Boolean); stm.WriteByte((byte)((bool)obj ? 1 : 0)); } else if (obj is string) { string str = obj as string; int len = Encoding.UTF8.GetByteCount(str); if (len > 0xffff) { stm.WriteByte((byte)DataType.LongString); CDataHelper.BE_WriteLongStr(stm, str); } else { stm.WriteByte((byte)DataType.String); CDataHelper.BE_WriteShortStr(stm, str); } } else if (obj is Array) { stm.WriteByte((byte)DataType.Array); Array ary = obj as Array; CDataHelper.BE_WriteUInt32(stm, (uint)ary.Length); foreach (object o in ary) { WriteAmf(stm, o); } } else if (obj is IDictionary) { stm.WriteByte((byte)DataType.Object); foreach (DictionaryEntry e in obj as IDictionary) { CDataHelper.BE_WriteShortStr(stm, e.Key.ToString()); WriteAmf(stm, e.Value); } CDataHelper.BE_WriteShortStr(stm, ""); // write only a short(0) stm.WriteByte((byte)DataType.EndOfObject); } else { Trace.Assert(false, "暂未处理的数据类型"); stm.WriteByte((byte)DataType.Unsupported); } }
protected static void WriteAmf(Stream stm, object obj) { if (obj == null) { stm.WriteByte((byte)DataType.Null); } else if (obj is byte || (obj is int && (uint)(int)obj < MaxU29) || (obj is uint && (uint)obj < MaxU29)) // U29无符号整形 { stm.WriteByte((byte)DataType.Integer); WriteInt(stm, uint.Parse(obj.ToString())); } else if (obj is int || obj is uint || obj is float || obj is double) // 只列举了常用的 { stm.WriteByte((byte)DataType.Double); CDataHelper.BE_WriteDouble(stm, double.Parse(obj.ToString())); } else if (obj is bool) { if ((bool)obj) { stm.WriteByte((byte)DataType.True); } else { stm.WriteByte((byte)DataType.False); } } else if (obj is string) { stm.WriteByte((byte)DataType.String); WriteString(stm, obj as string); } else if (obj is CMixArray) { stm.WriteByte((byte)DataType.Array); CMixArray ary = obj as CMixArray; uint head = ((uint)ary.FixedLength << 1) | 1; WriteInt(stm, head); foreach (KeyValuePair <string, object> pair in ary.Dynamic) { WriteString(stm, pair.Key); WriteAmf(stm, pair.Value); } WriteString(stm, ""); foreach (object o in ary.Fixed) { WriteAmf(stm, o); } } else if (obj is Array) { stm.WriteByte((byte)DataType.Array); Array ary = obj as Array; uint head = ((uint)ary.Length << 1) | 1; WriteInt(stm, head); WriteString(stm, ""); foreach (object o in ary) { WriteAmf(stm, o); } } else if (obj is IDictionary) { stm.WriteByte((byte)DataType.Object); IDictionary dic = obj as IDictionary; uint head = 0x0B; WriteInt(stm, head); if (obj is CNameObjDict) { WriteString(stm, (obj as CNameObjDict).className); } else { WriteString(stm, ""); } foreach (DictionaryEntry e in obj as IDictionary) { if (e.Key.ToString() == "" && e.Value is string) //解析时为了好看放进去的ClassName { continue; } WriteString(stm, e.Key.ToString()); WriteAmf(stm, e.Value); } WriteString(stm, ""); } else { Trace.Assert(false, "暂未处理的数据类型"); stm.WriteByte((byte)DataType.Undefined); } }