-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
BinarySystemHandlers.cs
796 lines (677 loc) · 29.6 KB
/
BinarySystemHandlers.cs
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
namespace Apache.Ignite.Core.Impl.Binary
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Apache.Ignite.Core.Impl.Binary.IO;
using Apache.Ignite.Core.Impl.Common;
/**
* <summary>Collection of predefined handlers for various system types.</summary>
*/
internal static class BinarySystemHandlers
{
/** Write handlers. */
private static readonly CopyOnWriteConcurrentDictionary<Type, IBinarySystemWriteHandler> WriteHandlers =
new CopyOnWriteConcurrentDictionary<Type, IBinarySystemWriteHandler>();
/** Read handlers. */
private static readonly IBinarySystemReader[] ReadHandlers = new IBinarySystemReader[255];
/** Type ids. */
private static readonly Dictionary<Type, byte> TypeIds = new Dictionary<Type, byte>
{
{typeof (bool), BinaryUtils.TypeBool},
{typeof (byte), BinaryUtils.TypeByte},
{typeof (sbyte), BinaryUtils.TypeByte},
{typeof (short), BinaryUtils.TypeShort},
{typeof (ushort), BinaryUtils.TypeShort},
{typeof (char), BinaryUtils.TypeChar},
{typeof (int), BinaryUtils.TypeInt},
{typeof (uint), BinaryUtils.TypeInt},
{typeof (long), BinaryUtils.TypeLong},
{typeof (ulong), BinaryUtils.TypeLong},
{typeof (float), BinaryUtils.TypeFloat},
{typeof (double), BinaryUtils.TypeDouble},
{typeof (string), BinaryUtils.TypeString},
{typeof (decimal), BinaryUtils.TypeDecimal},
{typeof (Guid), BinaryUtils.TypeGuid},
{typeof (Guid?), BinaryUtils.TypeGuid},
{typeof (ArrayList), BinaryUtils.TypeCollection},
{typeof (Hashtable), BinaryUtils.TypeDictionary},
{typeof (bool[]), BinaryUtils.TypeArrayBool},
{typeof (byte[]), BinaryUtils.TypeArrayByte},
{typeof (sbyte[]), BinaryUtils.TypeArrayByte},
{typeof (short[]), BinaryUtils.TypeArrayShort},
{typeof (ushort[]), BinaryUtils.TypeArrayShort},
{typeof (char[]), BinaryUtils.TypeArrayChar},
{typeof (int[]), BinaryUtils.TypeArrayInt},
{typeof (uint[]), BinaryUtils.TypeArrayInt},
{typeof (long[]), BinaryUtils.TypeArrayLong},
{typeof (ulong[]), BinaryUtils.TypeArrayLong},
{typeof (float[]), BinaryUtils.TypeArrayFloat},
{typeof (double[]), BinaryUtils.TypeArrayDouble},
{typeof (string[]), BinaryUtils.TypeArrayString},
{typeof (decimal?[]), BinaryUtils.TypeArrayDecimal},
{typeof (Guid?[]), BinaryUtils.TypeArrayGuid},
{typeof (object[]), BinaryUtils.TypeArray}
};
/// <summary>
/// Initializes the <see cref="BinarySystemHandlers"/> class.
/// </summary>
[SuppressMessage("Microsoft.Performance", "CA1810:InitializeReferenceTypeStaticFieldsInline",
Justification = "Readability.")]
static BinarySystemHandlers()
{
// 1. Primitives.
ReadHandlers[BinaryUtils.TypeBool] = new BinarySystemReader<bool>(s => s.ReadBool());
ReadHandlers[BinaryUtils.TypeByte] = new BinarySystemReader<byte>(s => s.ReadByte());
ReadHandlers[BinaryUtils.TypeShort] = new BinarySystemReader<short>(s => s.ReadShort());
ReadHandlers[BinaryUtils.TypeChar] = new BinarySystemReader<char>(s => s.ReadChar());
ReadHandlers[BinaryUtils.TypeInt] = new BinarySystemReader<int>(s => s.ReadInt());
ReadHandlers[BinaryUtils.TypeLong] = new BinarySystemReader<long>(s => s.ReadLong());
ReadHandlers[BinaryUtils.TypeFloat] = new BinarySystemReader<float>(s => s.ReadFloat());
ReadHandlers[BinaryUtils.TypeDouble] = new BinarySystemReader<double>(s => s.ReadDouble());
ReadHandlers[BinaryUtils.TypeDecimal] = new BinarySystemReader<decimal?>(BinaryUtils.ReadDecimal);
// 2. Date.
ReadHandlers[BinaryUtils.TypeTimestamp] = new BinarySystemReader<DateTime?>(BinaryUtils.ReadTimestamp);
// 3. String.
ReadHandlers[BinaryUtils.TypeString] = new BinarySystemReader<string>(BinaryUtils.ReadString);
// 4. Guid.
ReadHandlers[BinaryUtils.TypeGuid] = new BinarySystemReader<Guid?>(s => BinaryUtils.ReadGuid(s));
// 5. Primitive arrays.
ReadHandlers[BinaryUtils.TypeArrayBool] = new BinarySystemReader<bool[]>(BinaryUtils.ReadBooleanArray);
ReadHandlers[BinaryUtils.TypeArrayByte] =
new BinarySystemDualReader<byte[], sbyte[]>(BinaryUtils.ReadByteArray, BinaryUtils.ReadSbyteArray);
ReadHandlers[BinaryUtils.TypeArrayShort] =
new BinarySystemDualReader<short[], ushort[]>(BinaryUtils.ReadShortArray,
BinaryUtils.ReadUshortArray);
ReadHandlers[BinaryUtils.TypeArrayChar] =
new BinarySystemReader<char[]>(BinaryUtils.ReadCharArray);
ReadHandlers[BinaryUtils.TypeArrayInt] =
new BinarySystemDualReader<int[], uint[]>(BinaryUtils.ReadIntArray, BinaryUtils.ReadUintArray);
ReadHandlers[BinaryUtils.TypeArrayLong] =
new BinarySystemDualReader<long[], ulong[]>(BinaryUtils.ReadLongArray,
BinaryUtils.ReadUlongArray);
ReadHandlers[BinaryUtils.TypeArrayFloat] =
new BinarySystemReader<float[]>(BinaryUtils.ReadFloatArray);
ReadHandlers[BinaryUtils.TypeArrayDouble] =
new BinarySystemReader<double[]>(BinaryUtils.ReadDoubleArray);
ReadHandlers[BinaryUtils.TypeArrayDecimal] =
new BinarySystemReader<decimal?[]>(BinaryUtils.ReadDecimalArray);
// 6. Date array.
ReadHandlers[BinaryUtils.TypeArrayTimestamp] =
new BinarySystemReader<DateTime?[]>(BinaryUtils.ReadTimestampArray);
// 7. String array.
ReadHandlers[BinaryUtils.TypeArrayString] = new BinarySystemTypedArrayReader<string>();
// 8. Guid array.
ReadHandlers[BinaryUtils.TypeArrayGuid] = new BinarySystemTypedArrayReader<Guid?>();
// 9. Array.
ReadHandlers[BinaryUtils.TypeArray] = new BinarySystemReader(ReadArray);
// 11. Arbitrary collection.
ReadHandlers[BinaryUtils.TypeCollection] = new BinarySystemReader(ReadCollection);
// 13. Arbitrary dictionary.
ReadHandlers[BinaryUtils.TypeDictionary] = new BinarySystemReader(ReadDictionary);
// 14. Enum.
ReadHandlers[BinaryUtils.TypeArrayEnum] = new BinarySystemReader(ReadEnumArray);
}
/// <summary>
/// Try getting write handler for type.
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static IBinarySystemWriteHandler GetWriteHandler(Type type)
{
return WriteHandlers.GetOrAdd(type, t =>
{
return FindWriteHandler(t);
});
}
/// <summary>
/// Find write handler for type.
/// </summary>
/// <param name="type">Type.</param>
/// <returns>
/// Write handler or NULL.
/// </returns>
private static IBinarySystemWriteHandler FindWriteHandler(Type type)
{
// 1. Well-known types.
if (type == typeof(string))
return new BinarySystemWriteHandler<string>(WriteString, false);
if (type == typeof(decimal))
return new BinarySystemWriteHandler<decimal>(WriteDecimal, false);
if (type == typeof(Guid))
return new BinarySystemWriteHandler<Guid>(WriteGuid, false);
if (type == typeof (BinaryObject))
return new BinarySystemWriteHandler<BinaryObject>(WriteBinary, false);
if (type == typeof (BinaryEnum))
return new BinarySystemWriteHandler<BinaryEnum>(WriteBinaryEnum, false);
if (type.IsEnum)
{
var underlyingType = Enum.GetUnderlyingType(type);
if (underlyingType == typeof(int))
return new BinarySystemWriteHandler<int>((w, i) => w.WriteEnum(i, type), false);
if (underlyingType == typeof(uint))
return new BinarySystemWriteHandler<uint>((w, i) => w.WriteEnum(unchecked((int) i), type), false);
if (underlyingType == typeof(byte))
return new BinarySystemWriteHandler<byte>((w, i) => w.WriteEnum(i, type), false);
if (underlyingType == typeof(sbyte))
return new BinarySystemWriteHandler<sbyte>((w, i) => w.WriteEnum(i, type), false);
if (underlyingType == typeof(short))
return new BinarySystemWriteHandler<short>((w, i) => w.WriteEnum(i, type), false);
if (underlyingType == typeof(ushort))
return new BinarySystemWriteHandler<ushort>((w, i) => w.WriteEnum(i, type), false);
return null; // Other enums, such as long and ulong, can't be expressed as int.
}
if (type == typeof(Ignite))
return new BinarySystemWriteHandler<object>(WriteIgnite, false);
// All types below can be written as handles.
if (type == typeof (ArrayList))
return new BinarySystemWriteHandler<ICollection>(WriteArrayList, true);
if (type == typeof (Hashtable))
return new BinarySystemWriteHandler<IDictionary>(WriteHashtable, true);
if (type.IsArray)
{
// We know how to write any array type.
Type elemType = type.GetElementType();
// Primitives.
if (elemType == typeof (bool))
return new BinarySystemWriteHandler<bool[]>(WriteBoolArray, true);
if (elemType == typeof(byte))
return new BinarySystemWriteHandler<byte[]>(WriteByteArray, true);
if (elemType == typeof(short))
return new BinarySystemWriteHandler<short[]>(WriteShortArray, true);
if (elemType == typeof(char))
return new BinarySystemWriteHandler<char[]>(WriteCharArray, true);
if (elemType == typeof(int))
return new BinarySystemWriteHandler<int[]>(WriteIntArray, true);
if (elemType == typeof(long))
return new BinarySystemWriteHandler<long[]>(WriteLongArray, true);
if (elemType == typeof(float))
return new BinarySystemWriteHandler<float[]>(WriteFloatArray, true);
if (elemType == typeof(double))
return new BinarySystemWriteHandler<double[]>(WriteDoubleArray, true);
// Non-CLS primitives.
if (elemType == typeof(sbyte))
return new BinarySystemWriteHandler<byte[]>(WriteByteArray, true);
if (elemType == typeof(ushort))
return new BinarySystemWriteHandler<short[]>(WriteShortArray, true);
if (elemType == typeof(uint))
return new BinarySystemWriteHandler<int[]>(WriteIntArray, true);
if (elemType == typeof(ulong))
return new BinarySystemWriteHandler<long[]>(WriteLongArray, true);
// Special types.
if (elemType == typeof (decimal?))
return new BinarySystemWriteHandler<decimal?[]>(WriteDecimalArray, true);
if (elemType == typeof(string))
return new BinarySystemWriteHandler<string[]>(WriteStringArray, true);
if (elemType == typeof(Guid?))
return new BinarySystemWriteHandler<Guid?[]>(WriteGuidArray, true);
// Enums.
if (BinaryUtils.IsIgniteEnum(elemType) || elemType == typeof(BinaryEnum))
return new BinarySystemWriteHandler<object>(WriteEnumArray, true);
// Object array.
return new BinarySystemWriteHandler<object>(WriteArray, true);
}
return null;
}
/// <summary>
/// Find write handler for type.
/// </summary>
/// <param name="type">Type.</param>
/// <returns>Write handler or NULL.</returns>
public static byte GetTypeId(Type type)
{
byte res;
if (TypeIds.TryGetValue(type, out res))
return res;
if (BinaryUtils.IsIgniteEnum(type))
return BinaryUtils.TypeEnum;
if (type.IsArray && BinaryUtils.IsIgniteEnum(type.GetElementType()))
return BinaryUtils.TypeArrayEnum;
return BinaryUtils.TypeObject;
}
/// <summary>
/// Reads an object of predefined type.
/// </summary>
public static bool TryReadSystemType<T>(byte typeId, BinaryReader ctx, out T res)
{
var handler = ReadHandlers[typeId];
if (handler == null)
{
res = default(T);
return false;
}
res = handler.Read<T>(ctx);
return true;
}
/// <summary>
/// Write decimal.
/// </summary>
/// <param name="ctx">Context.</param>
/// <param name="obj">Value.</param>
private static void WriteDecimal(BinaryWriter ctx, decimal obj)
{
ctx.Stream.WriteByte(BinaryUtils.TypeDecimal);
BinaryUtils.WriteDecimal(obj, ctx.Stream);
}
/// <summary>
/// Write string.
/// </summary>
/// <param name="ctx">Context.</param>
/// <param name="obj">Object.</param>
private static void WriteString(BinaryWriter ctx, string obj)
{
ctx.Stream.WriteByte(BinaryUtils.TypeString);
BinaryUtils.WriteString(obj, ctx.Stream);
}
/// <summary>
/// Write Guid.
/// </summary>
/// <param name="ctx">Context.</param>
/// <param name="obj">Value.</param>
private static void WriteGuid(BinaryWriter ctx, Guid obj)
{
ctx.Stream.WriteByte(BinaryUtils.TypeGuid);
BinaryUtils.WriteGuid(obj, ctx.Stream);
}
/// <summary>
/// Write boolaen array.
/// </summary>
/// <param name="ctx">Context.</param>
/// <param name="obj">Value.</param>
private static void WriteBoolArray(BinaryWriter ctx, bool[] obj)
{
ctx.Stream.WriteByte(BinaryUtils.TypeArrayBool);
BinaryUtils.WriteBooleanArray(obj, ctx.Stream);
}
/// <summary>
/// Write byte array.
/// </summary>
/// <param name="ctx">Context.</param>
/// <param name="obj">Value.</param>
private static void WriteByteArray(BinaryWriter ctx, byte[] obj)
{
ctx.Stream.WriteByte(BinaryUtils.TypeArrayByte);
BinaryUtils.WriteByteArray(obj, ctx.Stream);
}
/// <summary>
/// Write short array.
/// </summary>
/// <param name="ctx">Context.</param>
/// <param name="obj">Value.</param>
private static void WriteShortArray(BinaryWriter ctx, short[] obj)
{
ctx.Stream.WriteByte(BinaryUtils.TypeArrayShort);
BinaryUtils.WriteShortArray(obj, ctx.Stream);
}
/// <summary>
/// Write char array.
/// </summary>
/// <param name="ctx">Context.</param>
/// <param name="obj">Value.</param>
private static void WriteCharArray(BinaryWriter ctx, object obj)
{
ctx.Stream.WriteByte(BinaryUtils.TypeArrayChar);
BinaryUtils.WriteCharArray((char[])obj, ctx.Stream);
}
/// <summary>
/// Write int array.
/// </summary>
/// <param name="ctx">Context.</param>
/// <param name="obj">Value.</param>
private static void WriteIntArray(BinaryWriter ctx, int[] obj)
{
ctx.Stream.WriteByte(BinaryUtils.TypeArrayInt);
BinaryUtils.WriteIntArray(obj, ctx.Stream);
}
/// <summary>
/// Write long array.
/// </summary>
/// <param name="ctx">Context.</param>
/// <param name="obj">Value.</param>
private static void WriteLongArray(BinaryWriter ctx, long[] obj)
{
ctx.Stream.WriteByte(BinaryUtils.TypeArrayLong);
BinaryUtils.WriteLongArray(obj, ctx.Stream);
}
/// <summary>
/// Write float array.
/// </summary>
/// <param name="ctx">Context.</param>
/// <param name="obj">Value.</param>
private static void WriteFloatArray(BinaryWriter ctx, float[] obj)
{
ctx.Stream.WriteByte(BinaryUtils.TypeArrayFloat);
BinaryUtils.WriteFloatArray(obj, ctx.Stream);
}
/// <summary>
/// Write double array.
/// </summary>
/// <param name="ctx">Context.</param>
/// <param name="obj">Value.</param>
private static void WriteDoubleArray(BinaryWriter ctx, double[] obj)
{
ctx.Stream.WriteByte(BinaryUtils.TypeArrayDouble);
BinaryUtils.WriteDoubleArray(obj, ctx.Stream);
}
/// <summary>
/// Write decimal array.
/// </summary>
/// <param name="ctx">Context.</param>
/// <param name="obj">Value.</param>
private static void WriteDecimalArray(BinaryWriter ctx, decimal?[] obj)
{
ctx.Stream.WriteByte(BinaryUtils.TypeArrayDecimal);
BinaryUtils.WriteDecimalArray(obj, ctx.Stream);
}
/// <summary>
/// Write string array.
/// </summary>
/// <param name="ctx">Context.</param>
/// <param name="obj">Value.</param>
private static void WriteStringArray(BinaryWriter ctx, string[] obj)
{
ctx.Stream.WriteByte(BinaryUtils.TypeArrayString);
BinaryUtils.WriteStringArray(obj, ctx.Stream);
}
/// <summary>
/// Write nullable GUID array.
/// </summary>
/// <param name="ctx">Context.</param>
/// <param name="obj">Value.</param>
private static void WriteGuidArray(BinaryWriter ctx, Guid?[] obj)
{
ctx.Stream.WriteByte(BinaryUtils.TypeArrayGuid);
BinaryUtils.WriteGuidArray(obj, ctx.Stream);
}
/// <summary>
/// Writes the enum array.
/// </summary>
private static void WriteEnumArray(BinaryWriter ctx, object obj)
{
ctx.Stream.WriteByte(BinaryUtils.TypeArrayEnum);
BinaryUtils.WriteArray((Array) obj, ctx);
}
/// <summary>
/// Writes the array.
/// </summary>
private static void WriteArray(BinaryWriter ctx, object obj)
{
ctx.Stream.WriteByte(BinaryUtils.TypeArray);
BinaryUtils.WriteArray((Array) obj, ctx);
}
/**
* <summary>Write ArrayList.</summary>
*/
private static void WriteArrayList(BinaryWriter ctx, ICollection obj)
{
ctx.Stream.WriteByte(BinaryUtils.TypeCollection);
BinaryUtils.WriteCollection(obj, ctx, BinaryUtils.CollectionArrayList);
}
/**
* <summary>Write Hashtable.</summary>
*/
private static void WriteHashtable(BinaryWriter ctx, IDictionary obj)
{
ctx.Stream.WriteByte(BinaryUtils.TypeDictionary);
BinaryUtils.WriteDictionary(obj, ctx, BinaryUtils.MapHashMap);
}
/**
* <summary>Write binary object.</summary>
*/
private static void WriteBinary(BinaryWriter ctx, BinaryObject obj)
{
ctx.Stream.WriteByte(BinaryUtils.TypeBinary);
BinaryUtils.WriteBinary(ctx.Stream, obj);
}
/// <summary>
/// Write enum.
/// </summary>
private static void WriteBinaryEnum(BinaryWriter ctx, BinaryEnum obj)
{
var binEnum = obj;
ctx.Stream.WriteByte(BinaryUtils.TypeBinaryEnum);
ctx.WriteInt(binEnum.TypeId);
ctx.WriteInt(binEnum.EnumValue);
}
/**
* <summary>Read enum array.</summary>
*/
private static object ReadEnumArray(BinaryReader ctx, Type type)
{
var elemType = type.GetElementType() ?? typeof(object);
return BinaryUtils.ReadTypedArray(ctx, true, elemType);
}
/// <summary>
/// Reads the array.
/// </summary>
private static object ReadArray(BinaryReader ctx, Type type)
{
var elemType = type.GetElementType();
if (elemType == null)
{
if (ctx.Mode == BinaryMode.ForceBinary)
{
// Forced binary mode: use object because primitives are not represented as IBinaryObject.
elemType = typeof(object);
}
else
{
// Infer element type from typeId.
var typeId = ctx.ReadInt();
if (typeId != BinaryUtils.ObjTypeId)
{
elemType = ctx.Marshaller.GetDescriptor(true, typeId, true).Type;
}
return BinaryUtils.ReadTypedArray(ctx, false, elemType ?? typeof(object));
}
}
// Element type is known, no need to check typeId.
// In case of incompatible types we'll get exception either way.
return BinaryUtils.ReadTypedArray(ctx, true, elemType);
}
/**
* <summary>Read collection.</summary>
*/
private static object ReadCollection(BinaryReader ctx, Type type)
{
return BinaryUtils.ReadCollection(ctx, null, null);
}
/**
* <summary>Read dictionary.</summary>
*/
private static object ReadDictionary(BinaryReader ctx, Type type)
{
return BinaryUtils.ReadDictionary(ctx, null);
}
/// <summary>
/// Write Ignite.
/// </summary>
private static void WriteIgnite(BinaryWriter ctx, object obj)
{
ctx.Stream.WriteByte(BinaryUtils.HdrNull);
}
/**
* <summary>Read delegate.</summary>
* <param name="ctx">Read context.</param>
* <param name="type">Type.</param>
*/
private delegate object BinarySystemReadDelegate(BinaryReader ctx, Type type);
/// <summary>
/// System type reader.
/// </summary>
private interface IBinarySystemReader
{
/// <summary>
/// Reads a value of specified type from reader.
/// </summary>
T Read<T>(BinaryReader ctx);
}
/// <summary>
/// System type generic reader.
/// </summary>
private interface IBinarySystemReader<out T>
{
/// <summary>
/// Reads a value of specified type from reader.
/// </summary>
T Read(BinaryReader ctx);
}
/// <summary>
/// Default reader with boxing.
/// </summary>
private class BinarySystemReader : IBinarySystemReader
{
/** */
private readonly BinarySystemReadDelegate _readDelegate;
/// <summary>
/// Initializes a new instance of the <see cref="BinarySystemReader"/> class.
/// </summary>
/// <param name="readDelegate">The read delegate.</param>
public BinarySystemReader(BinarySystemReadDelegate readDelegate)
{
Debug.Assert(readDelegate != null);
_readDelegate = readDelegate;
}
/** <inheritdoc /> */
public T Read<T>(BinaryReader ctx)
{
return (T)_readDelegate(ctx, typeof(T));
}
}
/// <summary>
/// Reader without boxing.
/// </summary>
private class BinarySystemReader<T> : IBinarySystemReader
{
/** */
private readonly Func<IBinaryStream, T> _readDelegate;
/// <summary>
/// Initializes a new instance of the <see cref="BinarySystemReader{T}"/> class.
/// </summary>
/// <param name="readDelegate">The read delegate.</param>
public BinarySystemReader(Func<IBinaryStream, T> readDelegate)
{
Debug.Assert(readDelegate != null);
_readDelegate = readDelegate;
}
/** <inheritdoc /> */
public TResult Read<TResult>(BinaryReader ctx)
{
return TypeCaster<TResult>.Cast(_readDelegate(ctx.Stream));
}
}
/// <summary>
/// Reader without boxing.
/// </summary>
private class BinarySystemTypedArrayReader<T> : IBinarySystemReader
{
public TResult Read<TResult>(BinaryReader ctx)
{
return TypeCaster<TResult>.Cast(BinaryUtils.ReadArray<T>(ctx, false));
}
}
/// <summary>
/// Reader with selection based on requested type.
/// </summary>
private class BinarySystemDualReader<T1, T2> : IBinarySystemReader, IBinarySystemReader<T2>
{
/** */
private readonly Func<IBinaryStream, T1> _readDelegate1;
/** */
private readonly Func<IBinaryStream, T2> _readDelegate2;
/// <summary>
/// Initializes a new instance of the <see cref="BinarySystemDualReader{T1,T2}"/> class.
/// </summary>
/// <param name="readDelegate1">The read delegate1.</param>
/// <param name="readDelegate2">The read delegate2.</param>
public BinarySystemDualReader(Func<IBinaryStream, T1> readDelegate1, Func<IBinaryStream, T2> readDelegate2)
{
Debug.Assert(readDelegate1 != null);
Debug.Assert(readDelegate2 != null);
_readDelegate1 = readDelegate1;
_readDelegate2 = readDelegate2;
}
/** <inheritdoc /> */
[SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods")]
T2 IBinarySystemReader<T2>.Read(BinaryReader ctx)
{
return _readDelegate2(ctx.Stream);
}
/** <inheritdoc /> */
public T Read<T>(BinaryReader ctx)
{
// Can't use "as" because of variance.
// For example, IBinarySystemReader<byte[]> can be cast to IBinarySystemReader<sbyte[]>, which
// will cause incorrect behavior.
if (typeof (T) == typeof (T2))
return ((IBinarySystemReader<T>) this).Read(ctx);
return TypeCaster<T>.Cast(_readDelegate1(ctx.Stream));
}
}
}
/// <summary>
/// Write delegate + handles flag.
/// </summary>
internal interface IBinarySystemWriteHandler
{
/// <summary>
/// Gets a value indicating whether this handler supports handles.
/// </summary>
bool SupportsHandles { get; }
/// <summary>
/// Writes object to a specified writer.
/// </summary>
/// <param name="writer">The writer.</param>
/// <param name="obj">The object.</param>
void Write<T>(BinaryWriter writer, T obj);
}
/// <summary>
/// Write delegate + handles flag.
/// </summary>
internal class BinarySystemWriteHandler<T1> : IBinarySystemWriteHandler
{
/** */
private readonly Action<BinaryWriter, T1> _writeAction;
/** */
private readonly bool _supportsHandles;
/// <summary>
/// Initializes a new instance of the <see cref="BinarySystemWriteHandler{T1}" /> class.
/// </summary>
/// <param name="writeAction">The write action.</param>
/// <param name="supportsHandles">Handles flag.</param>
public BinarySystemWriteHandler(Action<BinaryWriter, T1> writeAction, bool supportsHandles)
{
Debug.Assert(writeAction != null);
_writeAction = writeAction;
_supportsHandles = supportsHandles;
}
/** <inheritdoc /> */
public void Write<T>(BinaryWriter writer, T obj)
{
_writeAction(writer, TypeCaster<T1>.Cast(obj));
}
/** <inheritdoc /> */
public bool SupportsHandles
{
get { return _supportsHandles; }
}
}
}