My favorites
▼
|
Sign in
csharptest-net
CSharpTest.Net's Code Library
Project Home
Downloads
Issues
Source
Repository:
default
wiki
Checkout
Browse
Changes
Clones
Source path:
hg
/
src
/
Library
/
Serialization
/
PrimitiveSerializer.cs
Branch:
default
Tag:
<none>
v1.10.1024.336
v1.10.1124.358
v1.10.420.164
v1.10.607.213
v1.10.913.269
v1.11.426.305
v1.11.924.348
v1.9.1004.144
‹59104bd26e63
4e0bd400d42a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#region Copyright 2011 by Roger Knapp, Licensed under the Apache License, Version 2.0
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
using System;
using System.IO;
namespace CSharpTest.Net.Serialization
{
/// <summary>
/// Provides simple implementations of ISerializer<T> for the primitive .Net types.
/// </summary>
public class PrimitiveSerializer :
ISerializer<string>,
ISerializer<bool>,
ISerializer<byte>,
ISerializer<sbyte>,
ISerializer<byte[]>,
ISerializer<char>,
ISerializer<DateTime>,
ISerializer<TimeSpan>,
ISerializer<short>,
ISerializer<ushort>,
ISerializer<int>,
ISerializer<uint>,
ISerializer<long>,
ISerializer<ulong>,
ISerializer<double>,
ISerializer<float>,
ISerializer<Guid>,
ISerializer<IntPtr>,
ISerializer<UIntPtr>
{
#region Static singleton accessors
/// <summary> Gets a singleton of the PrimitiveSerializer </summary>
public static readonly PrimitiveSerializer Instance = new PrimitiveSerializer();
/// <summary> Gets a typed version of the PrimitiveSerializer </summary>
public static readonly ISerializer<string> String = LimitedSerializer.Unlimited;
/// <summary> Gets a typed version of the PrimitiveSerializer </summary>
public static readonly ISerializer<bool> Boolean = Instance;
/// <summary> Gets a typed version of the PrimitiveSerializer </summary>
public static readonly ISerializer<byte> Byte = Instance;
/// <summary> Gets a typed version of the PrimitiveSerializer </summary>
public static readonly ISerializer<sbyte> SByte = Instance;
/// <summary> Gets a typed version of the PrimitiveSerializer </summary>
public static readonly ISerializer<byte[]> Bytes = LimitedSerializer.Unlimited;
/// <summary> Gets a typed version of the PrimitiveSerializer </summary>
public static readonly ISerializer<char> Char = Instance;
/// <summary> Gets a typed version of the PrimitiveSerializer </summary>
public static readonly ISerializer<DateTime> DateTime = Instance;
/// <summary> Gets a typed version of the PrimitiveSerializer </summary>
public static readonly ISerializer<TimeSpan> TimeSpan = Instance;
/// <summary> Gets a typed version of the PrimitiveSerializer </summary>
public static readonly ISerializer<short> Int16 = Instance;
/// <summary> Gets a typed version of the PrimitiveSerializer </summary>
public static readonly ISerializer<ushort> UInt16 = Instance;
/// <summary> Gets a typed version of the PrimitiveSerializer </summary>
public static readonly ISerializer<int> Int32 = Instance;
/// <summary> Gets a typed version of the PrimitiveSerializer </summary>
public static readonly ISerializer<uint> UInt32 = Instance;
/// <summary> Gets a typed version of the PrimitiveSerializer </summary>
public static readonly ISerializer<long> Int64 = Instance;
/// <summary> Gets a typed version of the PrimitiveSerializer </summary>
public static readonly ISerializer<ulong> UInt64 = Instance;
/// <summary> Gets a typed version of the PrimitiveSerializer </summary>
public static readonly ISerializer<double> Double = Instance;
/// <summary> Gets a typed version of the PrimitiveSerializer </summary>
public static readonly ISerializer<float> Float = Instance;
/// <summary> Gets a typed version of the PrimitiveSerializer </summary>
public static readonly ISerializer<Guid> Guid = Instance;
/// <summary> Gets a typed version of the PrimitiveSerializer </summary>
public static readonly ISerializer<IntPtr> IntPtr = Instance;
/// <summary> Gets a typed version of the PrimitiveSerializer </summary>
public static readonly ISerializer<UIntPtr> UIntPtr = Instance;
#endregion
#region ISerializer<string> Members
void ISerializer<string>.WriteTo(string value, Stream stream)
{
String.WriteTo(value, stream);
}
string ISerializer<string>.ReadFrom(Stream stream)
{
return String.ReadFrom(stream);
}
#endregion
#region ISerializer<bool> Members
void ISerializer<bool>.WriteTo(bool value, Stream stream)
{
const byte bTrue = 1;
const byte bFalse = 0;
stream.WriteByte(value ? bTrue : bFalse);
}
bool ISerializer<bool>.ReadFrom(Stream stream)
{
int result = stream.ReadByte();
Check.Assert<InvalidDataException>(result != -1);
return result == 1;
}
#endregion
#region ISerializer<byte> Members
void ISerializer<byte>.WriteTo(byte value, Stream stream)
{
stream.WriteByte(value);
}
byte ISerializer<byte>.ReadFrom(Stream stream)
{
int result = stream.ReadByte();
Check.Assert<InvalidDataException>(result != -1);
return unchecked((byte)result);
}
#endregion
#region ISerializer<sbyte> Members
void ISerializer<sbyte>.WriteTo(sbyte value, Stream stream)
{
stream.WriteByte(unchecked((byte)value));
}
sbyte ISerializer<sbyte>.ReadFrom(Stream stream)
{
int result = stream.ReadByte();
Check.Assert<InvalidDataException>(result != -1);
return unchecked((sbyte)result);
}
#endregion
#region ISerializer<byte[]> Members
void ISerializer<byte[]>.WriteTo(byte[] value, Stream stream)
{
Bytes.WriteTo(value, stream);
}
byte[] ISerializer<byte[]>.ReadFrom(Stream stream)
{
return Bytes.ReadFrom(stream);
}
#endregion
#region ISerializer<char> Members
void ISerializer<char>.WriteTo(char value, Stream stream)
{
VariantNumberSerializer.Int32.WriteTo(value, stream);
}
char ISerializer<char>.ReadFrom(Stream stream)
{
return unchecked((char)VariantNumberSerializer.Int32.ReadFrom(stream));
}
#endregion
#region ISerializer<DateTime> Members
void ISerializer<DateTime>.WriteTo(DateTime value, Stream stream)
{
((ISerializer<long>)this).WriteTo(value.ToBinary(), stream);
}
DateTime ISerializer<DateTime>.ReadFrom(Stream stream)
{
return System.DateTime.FromBinary(((ISerializer<long>)this).ReadFrom(stream));
}
#endregion
#region ISerializer<TimeSpan> Members
void ISerializer<TimeSpan>.WriteTo(TimeSpan value, Stream stream)
{
((ISerializer<long>)this).WriteTo(value.Ticks, stream);
}
TimeSpan ISerializer<TimeSpan>.ReadFrom(Stream stream)
{
return new TimeSpan(((ISerializer<long>)this).ReadFrom(stream));
}
#endregion
#region ISerializer<short> Members
void ISerializer<short>.WriteTo(short value, Stream stream)
{
((ISerializer<ushort>)this).WriteTo(unchecked((ushort)value), stream);
}
short ISerializer<short>.ReadFrom(Stream stream)
{
return unchecked((short)((ISerializer<ushort>)this).ReadFrom(stream));
}
#endregion
#region ISerializer<ushort> Members
void ISerializer<ushort>.WriteTo(ushort value, Stream stream)
{
unchecked
{
stream.WriteByte((byte)(value >> 8));
stream.WriteByte((byte)value);
}
}
ushort ISerializer<ushort>.ReadFrom(Stream stream)
{
unchecked
{
int b1 = stream.ReadByte();
int b2 = stream.ReadByte();
Check.Assert<InvalidDataException>(b2 != -1);
return (ushort)((b1 << 8) | b2);
}
}
#endregion
#region ISerializer<int> Members
void ISerializer<int>.WriteTo(int value, Stream stream)
{
((ISerializer<uint>)this).WriteTo(unchecked((uint)value), stream);
}
int ISerializer<int>.ReadFrom(Stream stream)
{
return unchecked((int)((ISerializer<uint>)this).ReadFrom(stream));
}
#endregion
#region ISerializer<uint> Members
void ISerializer<uint>.WriteTo(uint value, Stream stream)
{
unchecked
{
stream.WriteByte((byte)(value >> 24));
stream.WriteByte((byte)(value >> 16));
stream.WriteByte((byte)(value >> 8));
stream.WriteByte((byte)value);
}
}
uint ISerializer<uint>.ReadFrom(Stream stream)
{
unchecked
{
int b1 = stream.ReadByte();
int b2 = stream.ReadByte();
int b3 = stream.ReadByte();
int b4 = stream.ReadByte();
Check.Assert<InvalidDataException>(b4 != -1);
return (
(((uint)b1) << 24) |
(((uint)b2) << 16) |
(((uint)b3) << 8) |
(((uint)b4) << 0)
);
}
}
#endregion
#region ISerializer<long> Members
void ISerializer<long>.WriteTo(long value, Stream stream)
{
((ISerializer<ulong>)this).WriteTo(unchecked((ulong)value), stream);
}
long ISerializer<long>.ReadFrom(Stream stream)
{
return unchecked((long)((ISerializer<ulong>)this).ReadFrom(stream));
}
#endregion
#region ISerializer<ulong> Members
void ISerializer<ulong>.WriteTo(ulong value, Stream stream)
{
unchecked
{
stream.WriteByte((byte)(value >> 56));
stream.WriteByte((byte)(value >> 48));
stream.WriteByte((byte)(value >> 40));
stream.WriteByte((byte)(value >> 32));
stream.WriteByte((byte)(value >> 24));
stream.WriteByte((byte)(value >> 16));
stream.WriteByte((byte)(value >> 8));
stream.WriteByte((byte)value);
}
}
ulong ISerializer<ulong>.ReadFrom(Stream stream)
{
unchecked
{
int b1 = stream.ReadByte();
int b2 = stream.ReadByte();
int b3 = stream.ReadByte();
int b4 = stream.ReadByte();
int b5 = stream.ReadByte();
int b6 = stream.ReadByte();
int b7 = stream.ReadByte();
int b8 = stream.ReadByte();
Check.Assert<InvalidDataException>(b8 != -1);
return (
(((ulong)b1) << 56) |
(((ulong)b2) << 48) |
(((ulong)b3) << 40) |
(((ulong)b4) << 32) |
(((ulong)b5) << 24) |
(((ulong)b6) << 16) |
(((ulong)b7) << 8) |
(((ulong)b8) << 0)
);
}
}
#endregion
#region ISerializer<double> Members
void ISerializer<double>.WriteTo(double value, Stream stream)
{
((ISerializer<long>)this).WriteTo(BitConverter.DoubleToInt64Bits(value), stream);
}
double ISerializer<double>.ReadFrom(Stream stream)
{
return BitConverter.Int64BitsToDouble(((ISerializer<long>)this).ReadFrom(stream));
}
#endregion
#region ISerializer<float> Members
void ISerializer<float>.WriteTo(float value, Stream stream)
{
((ISerializer<long>)this).WriteTo(BitConverter.DoubleToInt64Bits(value), stream);
}
float ISerializer<float>.ReadFrom(Stream stream)
{
return unchecked((float)BitConverter.Int64BitsToDouble(((ISerializer<long>)this).ReadFrom(stream)));
}
#endregion
#region ISerializer<Guid> Members
void ISerializer<Guid>.WriteTo(Guid value, Stream stream)
{
stream.Write(value.ToByteArray(), 0, 16);
}
Guid ISerializer<Guid>.ReadFrom(Stream stream)
{
byte[] tmp = new byte[16];
int len, bytesRead = 0;
while (bytesRead < 16 && 0 != (len = stream.Read(tmp, bytesRead, 16 - bytesRead)))
bytesRead += len;
Check.Assert<InvalidDataException>(16 == bytesRead);
return new Guid(tmp);
}
#endregion
#region ISerializer<IntPtr> Members
void ISerializer<IntPtr>.WriteTo(IntPtr value, Stream stream)
{
((ISerializer<long>)this).WriteTo(value.ToInt64(), stream);
}
IntPtr ISerializer<IntPtr>.ReadFrom(Stream stream)
{
return new IntPtr(((ISerializer<long>)this).ReadFrom(stream));
}
#endregion
#region ISerializer<UIntPtr> Members
void ISerializer<UIntPtr>.WriteTo(UIntPtr value, Stream stream)
{
((ISerializer<ulong>)this).WriteTo(value.ToUInt64(), stream);
}
UIntPtr ISerializer<UIntPtr>.ReadFrom(Stream stream)
{
return new UIntPtr(((ISerializer<ulong>)this).ReadFrom(stream));
}
#endregion
}
}
Show details
Hide details
Change log
2902ff73dbaf
by rogerk on Sep 24, 2011
Diff
Release version 1.11.924.348
Go to:
...sTree.Test/BPlusTree.Test.csproj
...ree.Test/TestBPlusTreeOptions.cs
...Collections/BPlusTree.Options.cs
...sTree/Properties/AssemblyInfo.cs
...PlusTree/Storage/Storage.Disk.cs
/src/CSBuild.exe
/src/CSBuild4.exe
/src/CSBuild4.exe.config
/src/Library/Bases/Comparable.cs
...y/Commands/CommandInterpreter.cs
.../Library/Commands/HelpDisplay.cs
/src/Library/Crypto/AESCryptoKey.cs
/src/Library/Crypto/Hash.cs
...brary/Crypto/HashDerivedBytes.cs
/src/Library/Crypto/HashStream.cs
...brary/Crypto/ModifiedRijndael.cs
/src/Library/Crypto/PBKDF2.cs
/src/Library/Crypto/PasswordKey.cs
...y/Crypto/SecureTransferClient.cs
.../Crypto/SecureTransferMessage.cs
...y/Crypto/SecureTransferServer.cs
/src/Library/Data/CsvReader.cs
.../Library/Html/XhtmlValidation.cs
...brary/Html/XmlLightAttributes.cs
...Library/Html/XmlLightDocument.cs
.../Library/Html/XmlLightElement.cs
...brary/Html/XmlLightInterfaces.cs
/src/Library/IO/AggregateStream.cs
/src/Library/IO/BaseStream.cs
/src/Library/IO/ClampedStream.cs
/src/Library/IO/CombinedStream.cs
.../Library/IO/MarshallingStream.cs
/src/Library/IO/NonClosingStream.cs
/src/Library/IO/ReplaceFile.cs
...rary/IO/SegmentedMemoryStream.cs
/src/Library/IO/StreamCache.cs
/src/Library/IO/TransactFile.cs
...y/Interfaces/DefaultFactories.cs
...ry/IpcChannel/IpcEventMessage.cs
...Library.Test/Library.Test.csproj
...brary.Test/TestCmdInterpreter.cs
...ry/Library.Test/TestCsvReader.cs
...y/Library.Test/TestEncryption.cs
...ry/Library.Test/TestFactories.cs
...brary.Test/TestFragmentedFile.cs
...Library/Library.Test/TestHash.cs
...y/Library.Test/TestHtmlParser.cs
...ary/Library.Test/TestPassword.cs
.../Library.Test/TestReplaceFile.cs
...brary.Test/TestSecureTransfer.cs
Project members,
sign in
to write a code review
Older revisions
59104bd26e63
by rogerk on Apr 26, 2011
Diff
Release version 1.11.426.305
All revisions of this file
File info
Size: 14673 bytes, 410 lines
View raw file
Powered by
Google Project Hosting