public AmfFormatter() { stream = new MemoryStream(); writer = new FlashorbBinaryWriter( stream ); objectSerializer = new ObjectSerializer(); referenceCache = new ReferenceCache(); }
public static void writeUTF( FlashorbBinaryWriter writer, string content, bool extendedutf ) { //int utfLength = UTF8Encoding.UTF8.GetByteCount( content ); byte[] buffer = UTF8Encoding.UTF8.GetBytes( content ); if( extendedutf ) { writer.WriteVarInt( (int) (buffer.Length << 1 | 0x1) ); } else { writer.Write( (byte) (buffer.Length >> 8 & 0xFF) ); writer.Write( (byte) (buffer.Length >> 0 & 0xFF) ); } if( buffer.Length > 0 ) writer.Write( buffer ); /* //dervived from java class DataOutputStream.writeUTF int strLength = content.Length; uint utfLength = 0; char[] charr = content.ToCharArray(); int c, count = 0; for (int i = 0; i < strLength; i++) { c = charr[ i ]; if( (c >= 0x0001) && (c <= 0x007F) ) utfLength++; else if( c > 0x07FF ) utfLength += 3; else utfLength += 2; } if( !extendedutf && utfLength > 65535 ) throw new ApplicationException( "utf data format exception" ); byte[] bytearr = new byte[ extendedutf ? utfLength : utfLength + 2 ]; if( extendedutf ) { writer.WriteVarInt( (int) (utfLength << 1 | 0x1) ); } else { bytearr[ count++ ] = (byte) ( utfLength >> 8 & 0xFF ); bytearr[ count++ ] = (byte) ( utfLength >> 0 & 0xFF ); } for (int i = 0; i < strLength; i++) { c = charr[ i ]; if ( (c >= 0x0001) && (c <= 0x007F) ) { bytearr[ count++ ] = (byte) c; } else if (c > 0x07FF) { bytearr[ count++ ] = (byte) (0xE0 | c >> 12 & 0x0F); bytearr[ count++ ] = (byte) (0x80 | c >> 6 & 0x3F); bytearr[ count++ ] = (byte) (0x80 | c >> 0 & 0x3F); } else { bytearr[ count++ ] = (byte) (0xC0 | c >> 6 & 0x1F); bytearr[ count++ ] = (byte) (0x80 | c >> 0 & 0x3F); } } writer.Write( bytearr ); * */ }
public void WriteObject(string className, IDictionary objectFields, IProtocolFormatter writer) { IEnumerator en = objectFields.Keys.GetEnumerator(); V3ReferenceCache cache = (V3ReferenceCache)writer.GetReferenceCache(); String traitsClassId = className; List <String> toRemove = new List <String>(); while (en.MoveNext()) { String fieldName = en.Current.ToString(); object obj = objectFields[fieldName]; if (obj != null && obj is ICollection && ((ICollection)obj).Count == 0) { toRemove.Add(fieldName); continue; } if (obj != null && obj.GetType().IsArray&& ((Object[])obj).Length == 0) { toRemove.Add(fieldName); continue; } } foreach (Object key in toRemove) { objectFields.Remove(key); } en = objectFields.Keys.GetEnumerator(); if (traitsClassId == null) { StringBuilder sb = new StringBuilder(); while (en.MoveNext()) { sb.Append(en.Current.ToString()); sb.Append("-"); } traitsClassId = sb.ToString(); en.Reset(); } if (cache.HasTraits(traitsClassId)) { writer.DirectWriteBytes(new byte[] { (byte)Datatypes.OBJECT_DATATYPE_V3 }); int traitId = (int)cache.GetTraitsId(traitsClassId); byte[] bytes = FlashorbBinaryWriter.GetVarIntBytes(0x1 | traitId << 2); writer.DirectWriteBytes(bytes); } else { writer.BeginWriteNamedObject(className, objectFields.Count); if (className == null) { cache.AddToTraitsCache(traitsClassId); } while (en.MoveNext()) { string fieldName = en.Current.ToString(); if (Log.isLogging(LoggingConstants.DEBUG)) { Log.log(LoggingConstants.DEBUG, "serializing property/field : " + fieldName); } writer.WriteFieldName(fieldName); } en.Reset(); } while (en.MoveNext()) { Object fieldName = en.Current; if (Log.isLogging(LoggingConstants.DEBUG)) { Log.log(LoggingConstants.DEBUG, "serializing property/field : " + fieldName); } //writer.BeginWriteFieldValue(); //try //{ //MessageWriter.writeObject( objectFields[ fieldName ], writer ); //Log.log( "REFCACHE", "WRITING FIELD " + fieldName ); object obj = objectFields[fieldName]; if (obj != null && obj is ICollection && ((ICollection)obj).Count == 0) { continue; } if (obj != null && obj.GetType().IsArray&& ((Object[])obj).Length == 0) { continue; } ITypeWriter typeWriter = MessageWriter.getWriter(obj, writer); typeWriter.write(obj, writer); //Log.log( "REFCACHE", "DONE WRITING FIELD " + fieldName ); //} //catch( Exception exception ) //{ // if( Log.isLogging( LoggingConstants.ERROR ) ) // Log.log( LoggingConstants.ERROR, "unable to serialize object's field " + fieldName, exception ); //} //finally //{ // writer.EndWriteFieldValue(); //} } writer.EndWriteNamedObject(); }
public static void writeUTF( FlashorbBinaryWriter writer, string content ) { writeUTF( writer, content, false ); }