Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Serializer.cs
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Text;
5
7{
11 public class Serializer : IDisposable
12 {
13 private MemoryStream ms;
14
18 public Serializer()
19 {
20 this.ms = new MemoryStream();
21 }
22
26 public void Dispose()
27 {
28 this.ms?.Dispose();
29 this.ms = null;
30 }
31
36 public byte[] ToArray()
37 {
38 return this.ms.ToArray();
39 }
40
45 public void WriteBoolean(bool Value)
46 {
47 this.ms.WriteByte(Value ? (byte)1 : (byte)0);
48 }
49
54 public void WriteByte(byte Value)
55 {
56 this.ms.WriteByte(Value);
57 }
58
63 public void WriteVarUInt64(ulong Value)
64 {
65 byte b;
66
67 do
68 {
69 b = (byte)(Value & 0x7f);
70 Value >>= 7;
71 if (Value != 0)
72 b |= 0x80;
73
74 this.ms.WriteByte(b);
75 }
76 while (Value != 0);
77 }
78
83 public void WriteBinary(byte[] Value)
84 {
85 if (Value is null)
86 this.ms.WriteByte(0);
87 else
88 {
89 this.WriteVarUInt64((ulong)Value.Length + 1);
90 this.ms.Write(Value, 0, Value.Length);
91 }
92 }
93
98 public void WriteRaw(byte[] Binary)
99 {
100 this.ms.Write(Binary, 0, Binary.Length);
101 }
102
109 public void WriteRaw(byte[] Binary, int Offset, int Count)
110 {
111 this.ms.Write(Binary, Offset, Count);
112 }
113
118 public void WriteString(string Value)
119 {
120 if (Value is null)
121 this.ms.WriteByte(0);
122 else if (string.IsNullOrEmpty(Value))
123 this.ms.WriteByte(1);
124 else
125 {
126 byte[] Bin = Encoding.UTF8.GetBytes(Value);
127
128 this.WriteVarUInt64((ulong)Bin.Length + 1);
129 this.ms.Write(Bin, 0, Bin.Length);
130 }
131 }
132
137 public void WriteInt8(sbyte Value)
138 {
139 this.ms.WriteByte((byte)Value);
140 }
141
146 public void WriteInt16(short Value)
147 {
148 this.ms.WriteByte((byte)Value);
149 Value >>= 8;
150 this.ms.WriteByte((byte)Value);
151 }
152
157 public void WriteInt32(int Value)
158 {
159 byte[] Bin = new byte[4];
160 int i;
161
162 for (i = 0; i < 4; i++)
163 {
164 Bin[i] = (byte)Value;
165 Value >>= 8;
166 }
167
168 this.ms.Write(Bin, 0, 4);
169 }
170
175 public void WriteInt64(long Value)
176 {
177 byte[] Bin = new byte[8];
178 int i;
179
180 for (i = 0; i < 8; i++)
181 {
182 Bin[i] = (byte)Value;
183 Value >>= 8;
184 }
185
186 this.ms.Write(Bin, 0, 8);
187 }
188
193 public void WriteUInt8(byte Value)
194 {
195 this.ms.WriteByte(Value);
196 }
197
202 public void WriteUInt16(ushort Value)
203 {
204 this.ms.WriteByte((byte)Value);
205 Value >>= 8;
206 this.ms.WriteByte((byte)Value);
207 }
208
213 public void WriteUInt32(uint Value)
214 {
215 byte[] Bin = new byte[4];
216 int i;
217
218 for (i = 0; i < 4; i++)
219 {
220 Bin[i] = (byte)Value;
221 Value >>= 8;
222 }
223
224 this.ms.Write(Bin, 0, 4);
225 }
226
231 public void WriteUInt64(ulong Value)
232 {
233 byte[] Bin = new byte[8];
234 int i;
235
236 for (i = 0; i < 8; i++)
237 {
238 Bin[i] = (byte)Value;
239 Value >>= 8;
240 }
241
242 this.ms.Write(Bin, 0, 8);
243 }
244
249 public void WriteSingle(float Value)
250 {
251 byte[] Bin = BitConverter.GetBytes(Value);
252 this.ms.Write(Bin, 0, Bin.Length);
253 }
254
259 public void WriteDouble(double Value)
260 {
261 byte[] Bin = BitConverter.GetBytes(Value);
262 this.ms.Write(Bin, 0, Bin.Length);
263 }
264
269 public void WriteDecimal(decimal Value)
270 {
271 int[] A = decimal.GetBits(Value);
272 int i;
273
274 for (i = 0; i < 4; i++)
275 this.WriteInt32(A[i]);
276 }
277
282 public void WriteCharacter(char Value)
283 {
284 this.WriteUInt16(Value);
285 }
286
291 public void WriteDateTime(DateTime Value)
292 {
293 this.WriteInt64(Value.Ticks);
294 this.ms.WriteByte((byte)Value.Kind);
295 }
296
301 public void WriteTimeSpan(TimeSpan Value)
302 {
303 this.WriteInt64(Value.Ticks);
304 }
305
310 public void WriteDateTimeOffset(DateTimeOffset Value)
311 {
312 this.WriteDateTime(Value.DateTime);
313 this.WriteTimeSpan(Value.Offset);
314 }
315
320 public void WriteGuid(Guid Value)
321 {
322 byte[] Bin = Value.ToByteArray();
323 this.ms.Write(Bin, 0, Bin.Length);
324 }
325 }
326}
void WriteUInt32(uint Value)
Writes a 32-bit unsigned integer to the output.
Definition: Serializer.cs:213
void WriteUInt64(ulong Value)
Writes a 64-bit unsigned integer to the output.
Definition: Serializer.cs:231
void WriteRaw(byte[] Binary)
Writes raw binary data to the output.
Definition: Serializer.cs:98
void WriteInt8(sbyte Value)
Writes a 8-bit signed integer to the output.
Definition: Serializer.cs:137
void WriteBoolean(bool Value)
Writes a boolean value to the output.
Definition: Serializer.cs:45
byte[] ToArray()
Returns the binary output.
Definition: Serializer.cs:36
void WriteInt32(int Value)
Writes a 32-bit signed integer to the output.
Definition: Serializer.cs:157
void WriteDateTimeOffset(DateTimeOffset Value)
Writes a DateTimeOffset to the output.
Definition: Serializer.cs:310
void WriteDateTime(DateTime Value)
Writes a DateTime to the output.
Definition: Serializer.cs:291
void WriteCharacter(char Value)
Writes a character to the output.
Definition: Serializer.cs:282
void WriteTimeSpan(TimeSpan Value)
Writes a TimeSpan to the output.
Definition: Serializer.cs:301
void WriteDecimal(decimal Value)
Writes a decimal number to the output.
Definition: Serializer.cs:269
void WriteVarUInt64(ulong Value)
Writes a variable-length unsigned integer to the output.
Definition: Serializer.cs:63
void WriteBinary(byte[] Value)
Writes binary data to the output.
Definition: Serializer.cs:83
void WriteInt16(short Value)
Writes a 16-bit signed integer to the output.
Definition: Serializer.cs:146
void WriteGuid(Guid Value)
Writes a Guid to the output.
Definition: Serializer.cs:320
void WriteUInt8(byte Value)
Writes a 8-bit unsigned integer to the output.
Definition: Serializer.cs:193
void WriteString(string Value)
Writes a string to the output.
Definition: Serializer.cs:118
void WriteSingle(float Value)
Writes a single-precision floating point number to the output.
Definition: Serializer.cs:249
void WriteRaw(byte[] Binary, int Offset, int Count)
Writes raw binary data to the output.
Definition: Serializer.cs:109
void WriteByte(byte Value)
Writes a byte to the output.
Definition: Serializer.cs:54
void WriteUInt16(ushort Value)
Writes a 16-bit unsigned integer to the output.
Definition: Serializer.cs:202
void WriteDouble(double Value)
Writes a double-precision floating point number to the output.
Definition: Serializer.cs:259
void WriteInt64(long Value)
Writes a 64-bit signed integer to the output.
Definition: Serializer.cs:175