public override object ReadValue(HessianReader reader, HessianContext context, Type objectType, byte initialOctet) { if (0x80 <= initialOctet && initialOctet <= 0xbf) { return(initialOctet - 0x90); } else if (0xc0 <= initialOctet && initialOctet <= 0xcf) { var b0 = reader.ReadByte(); return(((initialOctet - 0xc8) << 8) + b0); } else if (0xd0 <= initialOctet && initialOctet <= 0xd7) { var s = reader.ReadUInt16(); return(((initialOctet - 0xd4) << 16) + s); } else if (Constants.BC_INT == initialOctet) { return(reader.ReadInt32()); } else { throw Exceptions.UnExpectedInitialOctet(this, initialOctet); } }
public override object ReadValue(HessianReader reader, HessianContext context, Type objectType, byte initialOctet) { if (initialOctet == Constants.BC_DOUBLE_ZERO) { return(0d); } else if (initialOctet == Constants.BC_DOUBLE_ONE) { return(1d); } else if (initialOctet == Constants.BC_DOUBLE_BYTE) { return((double)reader.ReadSByte()); } else if (initialOctet == Constants.BC_DOUBLE_SHORT) { return((double)reader.ReadInt16()); } else if (initialOctet == Constants.BC_DOUBLE_MILL) { return(reader.ReadInt32() / 1000.0d); } else if (initialOctet == Constants.BC_DOUBLE) { return(reader.ReadDouble()); } else { throw Exceptions.UnExpectedInitialOctet(this, initialOctet); } }
public override object ReadValue(HessianReader reader, HessianContext context, Type objectType, byte initialOctet) { var builder = new StringBuilder(); int len = 0; while (Constants.BC_STRING_CHUNK == initialOctet) { len = reader.ReadUInt16(); builder.Append(reader.ReadString(len)); initialOctet = reader.ReadByte(); } if (0x00 <= initialOctet && initialOctet <= 0x1f) { len = initialOctet; } else if (0x30 <= initialOctet && initialOctet <= 0x34) { var b0 = reader.ReadByte(); len = ((initialOctet - 0x30) << 8) + b0; } else if (Constants.BC_STRING == initialOctet) { len = reader.ReadUInt16(); } else { throw Exceptions.UnExpectedInitialOctet(this, initialOctet); } builder.Append(reader.ReadString(len)); return(builder.ToString()); }
public override object ReadValue(HessianReader reader, HessianContext context, Type objectType, byte initialOctet) { using (var stream = new MemoryStream()) { while (initialOctet == Constants.BC_BINARY_CHUNK) { int len = reader.ReadInt16(); FillBuffer(reader, stream, len); initialOctet = reader.ReadByte(); } if (initialOctet == Constants.BC_BINARY) { int len = reader.ReadInt16(); FillBuffer(reader, stream, len); } else if (Constants.BC_BINARY_DIRECT <= initialOctet && initialOctet <= Constants.BC_BINARY_DIRECT + Constants.BINARY_DIRECT_MAX) { FillBuffer(reader, stream, initialOctet - Constants.BC_BINARY_DIRECT); } else { throw Exceptions.UnExpectedInitialOctet(this, initialOctet); } return(stream.ToArray()); } }
public override object ReadValue(HessianReader reader, HessianContext context, Type objectType, byte initialOctet) { if (0xd8 <= initialOctet && initialOctet <= 0xef) { return((long)(initialOctet - 0xe0)); } else if (0xf0 <= initialOctet && initialOctet <= 0xff) { var b0 = reader.ReadByte(); return((long)(((initialOctet - 0xf8) << 8) + b0)); } else if (0x38 <= initialOctet && initialOctet <= 0x3f) { var s = reader.ReadUInt16(); return((long)(((initialOctet - 0x3c) << 16) + s)); } else if (Constants.BC_LONG_INT == initialOctet) { return((long)reader.ReadInt32()); } else if (Constants.BC_LONG == initialOctet) { return(reader.ReadInt64()); } else { throw Exceptions.UnExpectedInitialOctet(this, initialOctet); } }
private void FillBuffer(HessianReader reader, Stream destStream, int length) { if (_buffer == null || _buffer.Length < length) { _buffer = new byte[length]; } var readLength = reader.Read(_buffer, 0, length); Exceptions.ThrowIfUnExpectedDataLength(length, readLength); destStream.Write(_buffer, 0, length); }
public virtual object ReadValue(HessianReader reader, HessianContext context, Type objectType) { var initialOctet = reader.ReadByte(); if (initialOctet == Constants.BC_NULL) { return(null); } return(ReadValue(reader, context, objectType, initialOctet)); }
public override object ReadValue(HessianReader reader, HessianContext context, Type objectType, byte initialOctet) { if (Constants.BC_REF == initialOctet) { var index = (int)IntConverter.ReadValue(reader, context, typeof(int)); return(context.ValueRefs.GetItem(index)); } else { return(ReadValueNotExisted(reader, context, objectType, initialOctet)); } }
public override object ReadValue(HessianReader reader, HessianContext context, Type objectType, byte initialOctet) { if (initialOctet == Constants.BC_TRUE) { return(true); } else if (initialOctet == Constants.BC_FALSE) { return(false); } else { throw Exceptions.UnExpectedInitialOctet(this, initialOctet); } }
public override object ReadValueNotExisted(HessianReader reader, HessianContext context, Type objectType, byte initialOctet) { int len = 0; Type elementType = null; if (0x70 <= initialOctet && initialOctet <= 0x77) { elementType = ((Type)TypeConverter.ReadValue(reader, context, null)).GetElementType(); len = initialOctet - 0x70; } else if (0x78 <= initialOctet && initialOctet <= 0x7f) { elementType = typeof(object); len = initialOctet - 0x78; } else if (Constants.BC_LIST_FIXED_UNTYPED == initialOctet) { elementType = typeof(object); len = (int)IntConverter.ReadValue(reader, context, typeof(int)); } else if (Constants.BC_LIST_FIXED == initialOctet) { elementType = ((Type)TypeConverter.ReadValue(reader, context, null)).GetElementType(); len = (int)IntConverter.ReadValue(reader, context, typeof(int)); } var array = Array.CreateInstance(elementType, len); context.ValueRefs.AddItem(array); HessianConverter itemConverter = null; if (elementType == typeof(object)) { itemConverter = AutoConverter; } else { itemConverter = AutoConverter.GetConverter(elementType); } for (int i = 0; i < len; i++) { var item = itemConverter.ReadValue(reader, context, elementType); array.SetValue(item, i); } return(array); }
public void TypeRef() { Serializer.AutoReset = false; Serializer.Serialize(Stream, typeof(string)); Serializer.Serialize(Stream, typeof(string)); Stream.Seek(0, System.IO.SeekOrigin.Begin); var reader = new HessianReader(Stream); var context = new HessianContext(); var type1 = (Type)HessianConverter.TypeConverter.ReadValue(reader, context, null); Assert.Equal(typeof(string), type1); var type2 = (Type)HessianConverter.TypeConverter.ReadValue(reader, context, null); Assert.Same(type1, type2); }
public override object ReadValue(HessianReader reader, HessianContext context, Type objectType, byte initialOctet) { if (initialOctet == Constants.BC_DATE) { var timeStamp = reader.ReadInt64(); return(BeginDate.AddTicks(timeStamp * 10000)); } else if (initialOctet == Constants.BC_DATE_MINUTE) { var minutes = reader.ReadInt32(); return(BeginDate.AddMinutes(minutes)); } else { throw Exceptions.UnExpectedInitialOctet(this, initialOctet); } }
public override object ReadValueNotExisted(HessianReader reader, HessianContext context, Type objectType, byte initialOctet) { if (ClassDefinitionConverter.CanRead(initialOctet)) { ClassDefinitionConverter.ReadValue(reader, context, typeof(ClassDefinition), initialOctet); initialOctet = reader.ReadByte(); } int definitionIndex = 0; if (0x60 <= initialOctet && initialOctet <= 0x6f) { definitionIndex = initialOctet - Constants.BC_OBJECT_DIRECT; } else if (Constants.BC_OBJECT == initialOctet) { definitionIndex = (int)IntConverter.ReadValue(reader, context, typeof(int)); } var definition = context.ClassRefs.GetItem(definitionIndex); object value = null; if (definition.Type.IsEnum) { definition.VisitFields(field => { var fieldValue = AutoConverter.ReadValue(reader, context, field.Type); if (field.Name == "name") { value = Enum.Parse(definition.Type, (string)fieldValue); context.ValueRefs.AddItem(value); } }); } else { value = Activator.CreateInstance(definition.Type); context.ValueRefs.AddItem(value); definition.VisitFields(field => { var fieldValue = AutoConverter.ReadValue(reader, context, field.Type); field.Setter(value, fieldValue); }); } return(value); }
public override object ReadValue(HessianReader reader, HessianContext context, Type objectType, byte initialOctet) { if (Constants.BC_REF == initialOctet) { var index = (int)IntConverter.ReadValue(reader, context, typeof(int)); return(context.ValueRefs.GetItem(index)); } foreach (var converter in ConverterCache.Values) { if (converter != this && !(converter is ClassDefinitionConverter) && converter.CanRead(initialOctet)) { return(converter.ReadValue(reader, context, objectType, initialOctet)); } } throw Exceptions.UnExpectedInitialOctet(this, initialOctet); }
public void ReadClassDefinition() { Serializer.AutoReset = false; var writer = new HessianWriter(Stream); var context = new HessianContext(); HessianConverter.ClassDefinitionConverter.WriteClassDefinition(writer, context, ClassDefinition.ForType(typeof(Car)), out int index); Stream.Seek(0, System.IO.SeekOrigin.Begin); var reader = new HessianReader(Stream); context = new HessianContext(); var definition = (ClassDefinition)HessianConverter.ClassDefinitionConverter.ReadValue(reader, context, typeof(ClassDefinition)); Assert.Equal(typeof(Car), definition.Type); Assert.Equal(2, definition.FieldNames.Count); Assert.Equal(nameof(Car.color), definition.FieldNames[0]); Assert.Equal(nameof(Car.model), definition.FieldNames[1]); }
public override object ReadValue(HessianReader reader, HessianContext context, Type objectType, byte initialOctet) { if (StringConverter.CanRead(initialOctet)) { var typeName = (string)StringConverter.ReadValue(reader, context, typeof(string), initialOctet); var type = GetType(typeName); context.TypeRefs.AddItem(type); return(type); } else if (IntConverter.CanRead(initialOctet)) { var typeIndex = (int)IntConverter.ReadValue(reader, context, typeof(int), initialOctet); return(context.TypeRefs.GetItem(typeIndex)); } else { throw Exceptions.UnExpectedInitialOctet(this, initialOctet); } }
public override object ReadValue(HessianReader reader, HessianContext context, Type objectType, byte initialOctet) { if (Constants.BC_OBJECT_DEF != initialOctet) { throw Exceptions.UnExpectedInitialOctet(this, initialOctet); } var type = (Type)TypeConverter.ReadValue(reader, context, null); var len = (int)IntConverter.ReadValue(reader, context, typeof(int)); var fieldNames = new List <string>(len); for (int i = 0; i < len; i++) { var fieldName = (string)StringConverter.ReadValue(reader, context, typeof(string)); fieldNames.Add(fieldName); } var definition = ClassDefinition.FromHessianDefinition(type, fieldNames); context.ClassRefs.AddItem(definition); return(definition); }
public override object ReadValueNotExisted(HessianReader reader, HessianContext context, Type objectType, byte initialOctet) { if (Constants.BC_MAP_UNTYPED == initialOctet) { var dict = new Dictionary <object, object>(); context.ValueRefs.AddItem(dict); initialOctet = reader.ReadByte(); while (Constants.BC_END != initialOctet) { var key = AutoConverter.ReadValue(reader, context, typeof(object), initialOctet); var value = AutoConverter.ReadValue(reader, context, typeof(object)); dict.Add(key, value); initialOctet = reader.ReadByte(); } return(dict); } else { throw Exceptions.UnExpectedInitialOctet(this, initialOctet); } }
public override object ReadValueNotExisted(HessianReader reader, HessianContext context, Type objectType, byte initialOctet) { Type listType = null; Type elementType = null; HessianConverter itemConverter = null; if (Constants.BC_LIST_VARIABLE_UNTYPED == initialOctet) { listType = typeof(List <object>); elementType = typeof(object); itemConverter = AutoConverter; } else if (Constants.BC_LIST_VARIABLE == initialOctet) { listType = (Type)TypeConverter.ReadValue(reader, context, null); elementType = listType.GenericTypeArguments[0]; itemConverter = AutoConverter.GetConverter(elementType); } else { throw Exceptions.UnExpectedInitialOctet(this, initialOctet); } var addMethod = listType.GetMethod("Add"); var list = Activator.CreateInstance(listType); context.ValueRefs.AddItem(list); initialOctet = reader.ReadByte(); var parameters = new object[1]; while (Constants.BC_END != initialOctet) { parameters[0] = itemConverter.ReadValue(reader, context, elementType, initialOctet); addMethod.Invoke(list, parameters); initialOctet = reader.ReadByte(); } return(list); }
public override object ReadValue(HessianReader reader, HessianContext context, Type objectType) { throw new NotImplementedException(); }
public virtual object ReadValue(HessianReader reader, HessianContext context, Type objectType, byte initialOctet) { return(null); }
public abstract object ReadValueNotExisted(HessianReader reader, HessianContext context, Type objectType, byte initialOctet);