-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerializationUtility.cs
47 lines (43 loc) · 1.72 KB
/
SerializationUtility.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
using System.IO;
namespace Zenvin.Settings.Framework.Serialization {
/// <summary>
/// A class providing utility methods to write and read byte arrays to and from streams.
/// </summary>
public static class SerializationUtility {
/// <summary>
/// Writes a byte array to a stream by means of a given <see cref="BinaryWriter"/>.
/// </summary>
/// <param name="writer"> The <see cref="BinaryWriter"/> to write the array to. </param>
/// <param name="array"> The byte array to write to the stream. May be <see langword="null"/>. </param>
/// <remarks>
/// This method assumes that the <paramref name="writer"/> is valid (i.e. it wraps an open stream that can be written to).
/// </remarks>
public static void WriteArray (this BinaryWriter writer, byte[] array) {
if (array == null) {
writer.Write (0);
return;
}
writer.Write (array.Length);
for (int i = 0; i < array.Length; i++) {
writer.Write (array[i]);
}
}
/// <summary>
/// Reads a byte array from a stream by means of a given <see cref="BinaryReader"/>.
/// </summary>
/// <param name="reader"> The <see cref="BinaryReader"/> used to read the values into the array. </param>
/// <returns> The read byte array. </returns>
/// <remarks>
/// This method assumes that the <paramref name="reader"/> is valid (i.e. it wraps an open stream that can be read from).<br></br>
/// It also assumes that the array was written by (or in the same format used by) <see cref="WriteArray(BinaryWriter, byte[])"/>.
/// </remarks>
public static byte[] ReadArray (this BinaryReader reader) {
int len = reader.ReadInt32 ();
byte[] arr = new byte[len];
for (int i = 0; i < len; i++) {
arr[i] = reader.ReadByte ();
}
return arr;
}
}
}