Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Deserializer.cs
1using System;
2using System.IO;
3using System.Text;
5
7{
11 public class Deserializer : IDisposable
12 {
13 private MemoryStream ms;
14
19 public Deserializer(byte[] Data)
20 {
21 this.ms = new MemoryStream(Data);
22 }
23
27 public void Dispose()
28 {
29 this.ms?.Dispose();
30 this.ms = null;
31 }
32
37 public bool ReadBoolean()
38 {
39 return this.ms.ReadByte() != 0;
40 }
41
46 public byte ReadByte()
47 {
48 return (byte)this.ms.ReadByte();
49 }
50
55 public ulong ReadVarUInt64()
56 {
57 ulong Value = 0;
58 int Offset = 0;
59 byte b;
60
61 do
62 {
63 b = (byte)this.ms.ReadByte();
64 Value |= ((ulong)(b & 0x7f)) << Offset;
65 Offset += 7;
66 }
67 while ((b & 0x80) != 0);
68
69 return Value;
70 }
71
76 public byte[] ReadBinary()
77 {
78 ulong Len = this.ReadVarUInt64();
79
80 if (Len == 0)
81 return null;
82
83 Len--;
84 if (Len > int.MaxValue)
85 throw new Exception("Invalid binary serialization.");
86
87 int c = (int)Len;
88 byte[] Bin = new byte[c];
89 this.ms.ReadAll(Bin, 0, c);
90
91 return Bin;
92 }
93
98 public string ReadString()
99 {
100 ulong Len = this.ReadVarUInt64();
101
102 switch (Len)
103 {
104 case 0:
105 return null;
106
107 case 1:
108 return string.Empty;
109
110 default:
111 Len--;
112 if (Len > int.MaxValue)
113 throw new Exception("Invalid string serialization.");
114
115 int c = (int)Len;
116 byte[] Bin = new byte[c];
117 this.ms.ReadAll(Bin, 0, c);
118 return Encoding.UTF8.GetString(Bin);
119 }
120 }
121
126 public sbyte ReadInt8()
127 {
128 return (sbyte)this.ms.ReadByte();
129 }
130
135 public short ReadInt16()
136 {
137 short Value = (short)this.ms.ReadByte();
138 Value |= (short)(this.ms.ReadByte() << 8);
139
140 return Value;
141 }
142
147 public int ReadInt32()
148 {
149 int Value = this.ReadUInt16();
150 Value |= this.ReadInt16() << 16;
151
152 return Value;
153 }
154
159 public long ReadInt64()
160 {
161 long Value = this.ReadUInt32();
162 Value |= ((long)this.ReadInt32()) << 32;
163
164 return Value;
165 }
166
171 public byte ReadUInt8()
172 {
173 return (byte)this.ms.ReadByte();
174 }
175
180 public ushort ReadUInt16()
181 {
182 ushort Value = (ushort)this.ms.ReadByte();
183 Value |= (ushort)(this.ms.ReadByte() << 8);
184
185 return Value;
186 }
187
192 public uint ReadUInt32()
193 {
194 uint Value = this.ReadUInt16();
195 Value |= (uint)(this.ReadUInt16() << 16);
196
197 return Value;
198 }
199
204 public ulong ReadUInt64()
205 {
206 ulong Value = this.ReadUInt32();
207 Value |= ((ulong)this.ReadUInt32()) << 32;
208
209 return Value;
210 }
211
216 public float ReadSingle()
217 {
218 byte[] Bin = new byte[4];
219 this.ms.ReadAll(Bin, 0, 4);
220 return BitConverter.ToSingle(Bin, 0);
221 }
222
227 public double ReadDouble()
228 {
229 byte[] Bin = new byte[8];
230 this.ms.ReadAll(Bin, 0, 8);
231 return BitConverter.ToDouble(Bin, 0);
232 }
233
238 public decimal ReadDecimal()
239 {
240 int[] A = new int[4];
241 int i;
242
243 for (i = 0; i < 4; i++)
244 A[i] = this.ReadInt32();
245
246 return new decimal(A);
247 }
248
253 public char ReadCharacter()
254 {
255 return (char)this.ReadUInt16();
256 }
257
262 public DateTime ReadDateTime()
263 {
264 long Ticks = this.ReadInt64();
265 DateTimeKind Kind = (DateTimeKind)this.ms.ReadByte();
266
267 return new DateTime(Ticks, Kind);
268 }
269
274 public TimeSpan ReadTimeSpan()
275 {
276 long Ticks = this.ReadInt64();
277 return new TimeSpan(Ticks);
278 }
279
284 public DateTimeOffset ReadDateTimeOffset()
285 {
286 DateTime TP = this.ReadDateTime();
287 TimeSpan Offset = this.ReadTimeSpan();
288
289 return new DateTimeOffset(TP, Offset);
290 }
291
296 public Guid ReadGuid()
297 {
298 byte[] Bin = new byte[16];
299 this.ms.ReadAll(Bin, 0, 16);
300 return new Guid(Bin);
301 }
302 }
303}
byte ReadUInt8()
Reads a 8-bit unsigned integer from the input.
short ReadInt16()
Reads a 16-bit signed integer from the input.
long ReadInt64()
Reads a 64-bit signed integer from the input.
byte[] ReadBinary()
Reads binary data from the input.
Definition: Deserializer.cs:76
decimal ReadDecimal()
Reads a decimal number from the input.
ulong ReadUInt64()
Reads a 64-bit unsigned integer from the input.
ushort ReadUInt16()
Reads a 16-bit unsigned integer from the input.
Deserializer(byte[] Data)
Cluster serializer
Definition: Deserializer.cs:19
ulong ReadVarUInt64()
Reads a variable-length unsigned integer from the input.
Definition: Deserializer.cs:55
int ReadInt32()
Reads a 32-bit signed integer from the input.
char ReadCharacter()
Reads a character from the input.
double ReadDouble()
Reads a double-precision floating point number from the input.
byte ReadByte()
Reads a byte from the input.
Definition: Deserializer.cs:46
Guid ReadGuid()
Reads a Guid from the input.
DateTime ReadDateTime()
Reads a DateTime from the input.
uint ReadUInt32()
Reads a 32-bit unsigned integer from the input.
TimeSpan ReadTimeSpan()
Reads a TimeSpan from the input.
sbyte ReadInt8()
Reads a 8-bit signed integer from the input.
string ReadString()
Reads a string from the input.
Definition: Deserializer.cs:98
float ReadSingle()
Reads a single-precision floating point number from the input.
DateTimeOffset ReadDateTimeOffset()
Reads a DateTimeOffset from the input.
bool ReadBoolean()
Reads a boolean value from the input.
Definition: Deserializer.cs:37