-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathByteSerializer.cs
35 lines (28 loc) · 999 Bytes
/
ByteSerializer.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
using System;
using System.IO;
using System.Runtime.Serialization;
namespace CSync.Util;
/// <summary>
/// Responsible for serializing to and from bytes via a <see cref="MemoryStream"/>.<br></br>
/// Uses <see cref="DataContractSerializer"/> as a fast and safer alternative to BinaryFormatter.
/// </summary>
[Serializable]
public class ByteSerializer<T> {
[NonSerialized]
static readonly DataContractSerializer Serializer = new(typeof(T));
// Ensures the size of an integer is correct for the current system.
public static int IntSize => sizeof(int);
public static byte[] SerializeToBytes(T val) {
using MemoryStream stream = new();
Serializer.WriteObject(stream, val);
return stream.ToArray();
}
public static T DeserializeFromBytes(byte[] data) {
using MemoryStream stream = new(data);
try {
return (T) Serializer.ReadObject(stream);
} catch (Exception) {
return default;
}
}
}