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.Collections.Generic;
3using System.IO;
4using System.Text;
6
8{
12 public class Deserializer : IDisposable
13 {
14 private MemoryStream ms;
15
20 public Deserializer(byte[] Data)
21 {
22 this.ms = new MemoryStream(Data);
23 }
24
28 public void Dispose()
29 {
30 this.ms?.Dispose();
31 this.ms = null;
32 }
33
38 public bool ReadBoolean()
39 {
40 return this.ms.ReadByte() != 0;
41 }
42
47 public byte ReadByte()
48 {
49 return (byte)this.ms.ReadByte();
50 }
51
56 public ulong ReadVarUInt64()
57 {
58 ulong Value = 0;
59 int Offset = 0;
60 byte b;
61
62 do
63 {
64 b = (byte)this.ms.ReadByte();
65 Value |= ((ulong)(b & 0x7f)) << Offset;
66 Offset += 7;
67 }
68 while ((b & 0x80) != 0);
69
70 return Value;
71 }
72
77 public byte[] ReadBinary()
78 {
79 ulong Len = this.ReadVarUInt64();
80
81 if (Len == 0)
82 return null;
83
84 Len--;
85 if (Len > int.MaxValue)
86 throw new Exception("Invalid binary serialization.");
87
88 int c = (int)Len;
89 byte[] Bin = new byte[c];
90 this.ms.ReadAll(Bin, 0, c);
91
92 return Bin;
93 }
94
99 public string ReadString()
100 {
101 ulong Len = this.ReadVarUInt64();
102
103 switch (Len)
104 {
105 case 0:
106 return null;
107
108 case 1:
109 return string.Empty;
110
111 default:
112 Len--;
113 if (Len > int.MaxValue)
114 throw new Exception("Invalid string serialization.");
115
116 int c = (int)Len;
117 byte[] Bin = new byte[c];
118 this.ms.ReadAll(Bin, 0, c);
119 return Encoding.UTF8.GetString(Bin);
120 }
121 }
122
127 public sbyte ReadInt8()
128 {
129 return (sbyte)this.ms.ReadByte();
130 }
131
136 public short ReadInt16()
137 {
138 short Value = (short)this.ms.ReadByte();
139 Value |= (short)(this.ms.ReadByte() << 8);
140
141 return Value;
142 }
143
148 public int ReadInt32()
149 {
150 int Value = this.ReadUInt16();
151 Value |= this.ReadInt16() << 16;
152
153 return Value;
154 }
155
160 public long ReadInt64()
161 {
162 long Value = this.ReadUInt32();
163 Value |= ((long)this.ReadInt32()) << 32;
164
165 return Value;
166 }
167
172 public byte ReadUInt8()
173 {
174 return (byte)this.ms.ReadByte();
175 }
176
181 public ushort ReadUInt16()
182 {
183 ushort Value = (ushort)this.ms.ReadByte();
184 Value |= (ushort)(this.ms.ReadByte() << 8);
185
186 return Value;
187 }
188
193 public uint ReadUInt32()
194 {
195 uint Value = this.ReadUInt16();
196 Value |= (uint)(this.ReadUInt16() << 16);
197
198 return Value;
199 }
200
205 public ulong ReadUInt64()
206 {
207 ulong Value = this.ReadUInt32();
208 Value |= ((ulong)this.ReadUInt32()) << 32;
209
210 return Value;
211 }
212
217 public float ReadSingle()
218 {
219 byte[] Bin = new byte[4];
220 this.ms.ReadAll(Bin, 0, 4);
221 return BitConverter.ToSingle(Bin, 0);
222 }
223
228 public double ReadDouble()
229 {
230 byte[] Bin = new byte[8];
231 this.ms.ReadAll(Bin, 0, 8);
232 return BitConverter.ToDouble(Bin, 0);
233 }
234
239 public decimal ReadDecimal()
240 {
241 int[] A = new int[4];
242 int i;
243
244 for (i = 0; i < 4; i++)
245 A[i] = this.ReadInt32();
246
247 return new decimal(A);
248 }
249
254 public char ReadCharacter()
255 {
256 return (char)this.ReadUInt16();
257 }
258
263 public DateTime ReadDateTime()
264 {
265 long Ticks = this.ReadInt64();
266 DateTimeKind Kind = (DateTimeKind)this.ms.ReadByte();
267
268 return new DateTime(Ticks, Kind);
269 }
270
275 public TimeSpan ReadTimeSpan()
276 {
277 long Ticks = this.ReadInt64();
278 return new TimeSpan(Ticks);
279 }
280
285 public DateTimeOffset ReadDateTimeOffset()
286 {
287 DateTime TP = this.ReadDateTime();
288 TimeSpan Offset = this.ReadTimeSpan();
289
290 return new DateTimeOffset(TP, Offset);
291 }
292
297 public Guid ReadGuid()
298 {
299 byte[] Bin = new byte[16];
300 this.ms.ReadAll(Bin, 0, 16);
301 return new Guid(Bin);
302 }
303 }
304}
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:77
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:20
ulong ReadVarUInt64()
Reads a variable-length unsigned integer from the input.
Definition: Deserializer.cs:56
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:47
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:99
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:38