-
Notifications
You must be signed in to change notification settings - Fork 16
/
BinarySerializerAdapter.cs
168 lines (152 loc) · 5.8 KB
/
BinarySerializerAdapter.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
using System;
using System.Diagnostics.Contracts;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;
namespace CrispyWaffle.Serialization.Adapters
{
/// <summary>
/// Class BinarySerializerAdapter. This class cannot be inherited.
/// </summary>
/// <seealso cref="ISerializerAdapter" />
public sealed class BinarySerializerAdapter : BaseSerializerAdapter
{
/// <summary>
/// Deserialize a stream to a generic type.
/// </summary>
/// <typeparam name="T">Generic type parameter.</typeparam>
/// <param name="stream">The serialized object as stream.</param>
/// <param name="encoding">(Optional) Determines the encoding to read the stream (not used in BinarySerializerProvider).</param>
/// <returns>A T.</returns>
[Pure]
public override T DeserializeFromStream<T>(Stream stream, Encoding encoding = null)
where T : class
{
#pragma warning disable SYSLIB0011
var formatter = new BinaryFormatter();
#pragma warning disable S5773
var result = formatter.Deserialize(stream);
#pragma warning restore S5773
stream.Close();
return (T)result;
}
/// <summary>
/// Deserializes.
/// </summary>
/// <typeparam name="T">Generic type parameter.</typeparam>
/// <param name="serialized">The serialized.</param>
/// <returns>A T.</returns>
[Pure]
public override T Deserialize<T>(object serialized)
where T : class
{
return DeserializeFromStream<T>((Stream)serialized);
}
/// <summary>
/// Loads the given file and Deserialize its.
/// </summary>
/// <typeparam name="T">Generic type parameter.</typeparam>
/// <param name="file">The file.</param>
/// <returns>A T.</returns>
/// <exception cref="ArgumentNullException">file - Supply a valid filename.</exception>
/// <exception cref="LocalFileNotFoundException">Throws when the file doesn't exist.</exception>
[Pure]
public override T Load<T>(string file)
where T : class
{
if (string.IsNullOrWhiteSpace(file))
{
throw new ArgumentNullException(nameof(file), "Supply a valid filename");
}
if (!File.Exists(file))
{
throw new LocalFileNotFoundException(
file,
Path.GetDirectoryName(Path.GetFullPath(file))
);
}
var fileName = Path.GetFileName(file);
var folder = Path.GetDirectoryName(file);
var stream = LoadInternal(file, fileName, folder);
return Deserialize<T>(stream);
}
/// <summary>
/// Loads the internal.
/// </summary>
/// <param name="file">The file.</param>
/// <param name="fileName">Name of the file.</param>
/// <param name="folder">The folder.</param>
/// <returns>Stream.</returns>
private static Stream LoadInternal(string file, string fileName, string folder)
{
var autoResetEvent = new AutoResetEvent(false);
while (true)
{
try
{
return new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.None);
}
catch (IOException)
{
HandleLoadIoException(fileName, folder, autoResetEvent);
}
}
}
/// <summary>
/// Handles the load io exception.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <param name="folder">The folder.</param>
/// <param name="autoResetEvent">The automatic reset event.</param>
private static void HandleLoadIoException(
string fileName,
string folder,
AutoResetEvent autoResetEvent
)
{
FileSystemWatcher watcher = null;
try
{
watcher = new FileSystemWatcher
{
Filter = fileName,
Path = folder,
NotifyFilter =
NotifyFilters.Attributes
| NotifyFilters.DirectoryName
| NotifyFilters.FileName
| NotifyFilters.LastWrite
| NotifyFilters.Size,
EnableRaisingEvents = true
};
watcher.Changed += (_, _) => autoResetEvent.Set();
autoResetEvent.WaitOne(new TimeSpan(0, 0, 0, 30));
}
finally
{
watcher?.Dispose();
}
}
/// <summary>
/// Serializes.
/// </summary>
/// <typeparam name="T">Generic type parameter.</typeparam>
/// <param name="deserialized">The deserialized.</param>
/// <param name="stream">[out] The stream.</param>
public override void Serialize<T>(T deserialized, out Stream stream)
where T : class
{
stream = new MemoryStream();
using (var streamTemp = new MemoryStream())
{
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(streamTemp, deserialized);
streamTemp.Seek(0, SeekOrigin.Begin);
streamTemp.CopyTo(stream);
stream.Seek(0, SeekOrigin.Begin);
}
}
}
}