CSLA.NET 5.4.2
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
CslaBinaryReader.cs
Go to the documentation of this file.
1using Csla.Properties;
2using System;
3using System.Collections.Generic;
4using System.IO;
5
7{
13 {
14 private readonly Dictionary<int, string> keywordsDictionary;
15
20 {
21 keywordsDictionary = new Dictionary<int, string>();
22 }
23
30 public List<SerializationInfo> Read(Stream serializationStream)
31 {
32 var returnValue = new List<SerializationInfo>();
33 int childCount, valueCount, referenceId;
34 string systemName, enumTypeName;
35 bool isDirty;
36 object value;
37 this.keywordsDictionary.Clear();
38
39 using (var reader = new BinaryReader(serializationStream))
40 {
41 var totalCount = reader.ReadInt32();
42 for (var counter = 0; counter < totalCount; counter++)
43 {
44 var info = new SerializationInfo()
45 {
46 ReferenceId = reader.ReadInt32(),
47 TypeName = ReadString(reader)
48 };
49
50 childCount = reader.ReadInt32();
51 for (var childCounter = 0; childCounter < childCount; childCounter++)
52 {
53 systemName = ReadString(reader);
54 isDirty = reader.ReadBoolean();
55 referenceId = reader.ReadInt32();
56 info.AddChild(systemName, referenceId, isDirty);
57 }
58
59 valueCount = reader.ReadInt32();
60 for (var valueCounter = 0; valueCounter < valueCount; valueCounter++)
61 {
62 systemName = ReadString(reader);
63 enumTypeName = ReadString(reader);
64 isDirty = reader.ReadBoolean();
65 value = ReadObject(reader);
66 info.AddValue(systemName, value, isDirty, string.IsNullOrEmpty(enumTypeName) ? null : enumTypeName);
67 }
68 returnValue.Add(info);
69 }
70 }
71
72 return returnValue;
73 }
74
75 private string ReadString(BinaryReader reader) =>
76 this.ReadString(reader, (CslaKnownTypes)reader.ReadByte());
77
78 private string ReadString(BinaryReader reader, CslaKnownTypes knownType)
79 {
80 switch (knownType)
81 {
82 case CslaKnownTypes.String:
83 return reader.ReadString();
84 case CslaKnownTypes.StringWithDictionaryKey:
85 var systemString = reader.ReadString();
86 this.keywordsDictionary.Add(reader.ReadInt32(), systemString);
87 return systemString;
88 case CslaKnownTypes.StringDictionaryKey:
89 return this.keywordsDictionary[reader.ReadInt32()];
90 default:
91 throw new ArgumentOutOfRangeException(Resources.UnandledKNownTypeException);
92 }
93 }
94
95 private object ReadObject(BinaryReader reader)
96 {
97 var knownType = (CslaKnownTypes)reader.ReadByte();
98 switch (knownType)
99 {
100 case CslaKnownTypes.IMobileObject:
101 using (MemoryStream arrayBuffer = new MemoryStream(reader.ReadBytes(reader.ReadInt32())))
102 {
103 var formatter = SerializationFormatterFactory.GetFormatter();
104 var obj = formatter.Deserialize(arrayBuffer);
105 return obj;
106 }
107 case CslaKnownTypes.Boolean:
108 return reader.ReadBoolean();
109 case CslaKnownTypes.Char:
110 return reader.ReadChar();
111 case CslaKnownTypes.SByte:
112 return reader.ReadSByte();
113 case CslaKnownTypes.Byte:
114 return reader.ReadByte();
115 case CslaKnownTypes.Int16:
116 return reader.ReadInt16();
117 case CslaKnownTypes.UInt16:
118 return reader.ReadUInt16();
119 case CslaKnownTypes.Int32:
120 return reader.ReadInt32();
121 case CslaKnownTypes.UInt32:
122 return reader.ReadUInt32();
123 case CslaKnownTypes.Int64:
124 return reader.ReadInt64();
125 case CslaKnownTypes.UInt64:
126 return reader.ReadUInt64();
127 case CslaKnownTypes.Single:
128 return reader.ReadSingle();
129 case CslaKnownTypes.Double:
130 return reader.ReadDouble();
131 case CslaKnownTypes.Decimal:
132 var decimalBits = new int[4];
133 for (var counter = 0; counter < 4; counter++)
134 {
135 decimalBits[counter] = reader.ReadInt32();
136 }
137 return new decimal(decimalBits);
138 case CslaKnownTypes.DateTime:
139 return new DateTime(reader.ReadInt64());
140 case CslaKnownTypes.TimeSpan:
141 return new TimeSpan(reader.ReadInt64());
142 case CslaKnownTypes.DateTimeOffset:
143 return new DateTimeOffset(reader.ReadInt64(), new TimeSpan(reader.ReadInt64()));
144 case CslaKnownTypes.Guid:
145 return new Guid(reader.ReadBytes(16)); // 16 bytes in a Guid
146 case CslaKnownTypes.ByteArray:
147 return reader.ReadBytes(reader.ReadInt32());
148 case CslaKnownTypes.ByteArrayArray:
149 var count = reader.ReadInt32();
150 var result = new byte[count][];
151 for (int i = 0; i < count; i++)
152 result[i] = reader.ReadBytes(reader.ReadInt32());
153 return result;
154 case CslaKnownTypes.CharArray:
155 return reader.ReadChars(reader.ReadInt32());
156 case CslaKnownTypes.ListOfInt:
157 var total = reader.ReadInt32();
158 var buffer = new int[total];
159 for (var counter = 0; counter < total; counter++)
160 {
161 buffer[counter] = reader.ReadInt32();
162 }
163 return new List<int>(buffer);
164 case CslaKnownTypes.Null:
165 return null;
166 case CslaKnownTypes.String:
167 case CslaKnownTypes.StringWithDictionaryKey:
168 case CslaKnownTypes.StringDictionaryKey:
169 return ReadString(reader, knownType);
170 default:
171 throw new ArgumentOutOfRangeException(Resources.UnandledKNownTypeException);
172 }
173 }
174 }
175}
A strongly-typed resource class, for looking up localized strings, etc.
static string UnandledKNownTypeException
Looks up a localized string similar to Unhandled CSLA Known type was found.
This is a class that is responsible for deserializing SerializationInfo objects for receiving the dat...
CslaBinaryReader()
Creates new instance of CslaBinaryReader
List< SerializationInfo > Read(Stream serializationStream)
Read a data from a stream, typically MemoryStream, and convert it into a list of SerializationInfo ob...
Object containing the serialization data for a specific object.
Represents a reader class that can be used to read the data sent across the wire in byte array format...
Definition: ICslaReader.cs:12
CslaKnownTypes
This enumeration contains the list of known types that CslaBinaryReader and CslaBinaryWriterknow abou...
@ DateTimeOffset
Date/time plus time zone / DateTimeOffset
@ Guid
Globally unique identifier / Guid