Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
BinarySerializationReader.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.IO;
5 using SiliconStudio.Core.IO;
6 
7 namespace SiliconStudio.Core.Serialization
8 {
9  /// <summary>
10  /// Implements <see cref="SerializationStream"/> as a binary reader.
11  /// </summary>
13  {
14  /// <summary>
15  /// Initializes a new instance of the <see cref="BinarySerializationReader"/> class.
16  /// </summary>
17  /// <param name="inputStream">The input stream.</param>
18  public BinarySerializationReader(Stream inputStream)
19  {
20  Reader = new BinaryReader(inputStream);
21  NativeStream = inputStream.ToNativeStream();
22  }
23 
24  private BinaryReader Reader { get; set; }
25 
26  /// <inheritdoc />
27  public override void Serialize(ref bool value)
28  {
29  int result = NativeStream.ReadByte();
30  if (result == -1)
31  throw new EndOfStreamException();
32  value = result != 0;
33  }
34 
35  /// <inheritdoc />
36  public unsafe override void Serialize(ref float value)
37  {
38  fixed (float* valuePtr = &value)
39  *((uint*)valuePtr) = NativeStream.ReadUInt32();
40  }
41 
42  /// <inheritdoc />
43  public unsafe override void Serialize(ref double value)
44  {
45  fixed (double* valuePtr = &value)
46  *((ulong*)valuePtr) = NativeStream.ReadUInt64();
47  }
48 
49  /// <inheritdoc />
50  public override void Serialize(ref short value)
51  {
52  value = (short)NativeStream.ReadUInt16();
53  }
54 
55  /// <inheritdoc />
56  public override void Serialize(ref int value)
57  {
58  value = (int)NativeStream.ReadUInt32();
59  }
60 
61  /// <inheritdoc />
62  public override void Serialize(ref long value)
63  {
64  value = (long)NativeStream.ReadUInt64();
65  }
66 
67  /// <inheritdoc />
68  public override void Serialize(ref ushort value)
69  {
70  value = NativeStream.ReadUInt16();
71  }
72 
73  /// <inheritdoc />
74  public override void Serialize(ref uint value)
75  {
76  value = NativeStream.ReadUInt32();
77  }
78 
79  /// <inheritdoc />
80  public override void Serialize(ref ulong value)
81  {
82  value = NativeStream.ReadUInt64();
83  }
84 
85  /// <inheritdoc />
86  public override void Serialize(ref string value)
87  {
88  value = Reader.ReadString();
89  }
90 
91  /// <inheritdoc />
92  public override void Serialize(ref char value)
93  {
94  value = Reader.ReadChar();
95  }
96 
97  /// <inheritdoc />
98  public override void Serialize(ref byte value)
99  {
100  int result = NativeStream.ReadByte();
101  if (result == -1)
102  throw new EndOfStreamException();
103  value = (byte)result;
104  }
105 
106  /// <inheritdoc />
107  public override void Serialize(ref sbyte value)
108  {
109  int result = NativeStream.ReadByte();
110  if (result == -1)
111  throw new EndOfStreamException();
112  value = (sbyte)(byte)result;
113  }
114 
115  /// <inheritdoc />
116  public override void Serialize(byte[] values, int offset, int count)
117  {
118  Reader.Read(values, offset, count);
119  }
120 
121  /// <inheritdoc/>
122  public override void Serialize(IntPtr memory, int count)
123  {
124  NativeStream.Read(memory, count);
125  }
126 
127  /// <inheritdoc />
128  public override void Flush()
129  {
130 
131  }
132  }
133 }
unsafe override void Serialize(ref double value)
Serializes the specified double value. The value to serialize
override void Serialize(ref long value)
Serializes the specified long value. The value to serialize
virtual unsafe ushort ReadUInt16()
Definition: NativeStream.cs:18
override void Flush()
Flushes all recent writes (for better batching). Please note that if only Serialize has been used (no...
override void Serialize(ref uint value)
Serializes the specified unsigned integer value. The value to serialize
unsafe override void Serialize(ref float value)
Serializes the specified float value. The value to serialize
override void Serialize(ref string value)
Serializes the specified string value. The value to serialize
override void Serialize(ref sbyte value)
Serializes the specified signed byte value. The value to serialize
virtual unsafe ulong ReadUInt64()
Definition: NativeStream.cs:50
_In_ size_t count
Definition: DirectXTexP.h:174
Base class for implementation of SerializationStream.
Implements SerializationStream as a binary reader.
override void Serialize(ref ulong value)
Serializes the specified unsigned long value. The value to serialize
override void Serialize(ref byte value)
Serializes the specified byte value. The value to serialize
override void Serialize(ref short value)
Serializes the specified short value. The value to serialize
override void Serialize(ref int value)
Serializes the specified integer value. The value to serialize
override void Serialize(byte[] values, int offset, int count)
Serializes the specified byte array. The buffer to serialize.The starting offset in the buffer to beg...
A Stream with additional methods for native read and write operations using IntPtr.
Definition: NativeStream.cs:11
override void Serialize(ref char value)
Serializes the specified char value. The value to serialize
override void Serialize(ref ushort value)
Serializes the specified ushort value. The value to serialize
virtual unsafe uint ReadUInt32()
Definition: NativeStream.cs:34
override void Serialize(ref bool value)
Serializes the specified boolean value. The value to serialize
BinarySerializationReader(Stream inputStream)
Initializes a new instance of the BinarySerializationReader class.
override void Serialize(IntPtr memory, int count)
Serializes the specified memory area. The memory area to serialize.The size, in bytes, to serialize.