-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUtil.cs
255 lines (226 loc) · 8.53 KB
/
Util.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
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Ionic.Zlib;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Threading;
using System.Xml.Serialization;
using Ionic.Zip;
namespace DeltaZip
{
static class Util
{
static XmlSerializer fileSerializer;
public static XmlSerializer FileSerializer {
get {
if (fileSerializer == null) {
fileSerializer = new XmlSerializer(typeof(List<File>), new XmlRootAttribute("Files"));
}
return fileSerializer;
}
}
static XmlSerializer workingCopySerializer;
public static XmlSerializer WorkingCopySerializer {
get {
if (workingCopySerializer == null) {
workingCopySerializer = new XmlSerializer(typeof(List<WorkingFile>), new XmlRootAttribute("Files"));
}
return workingCopySerializer;
}
}
// Is it worth compressing the data?
public static bool IsCompressable(MemoryStream stream)
{
if (Settings.CompressionLevel == 0)
return false;
if (stream.Length < 8 * Settings.CompressableTestSkip)
return true;
long inputSize = 0;
MemoryStream deflated = new MemoryStream();
DeflateStream defStream = new DeflateStream(deflated, CompressionMode.Compress, (CompressionLevel)Settings.CompressionLevel, true);
for (int i = 0; i + Settings.CompressableTestSize < stream.Length ;i += Settings.CompressableTestSkip) {
inputSize += Settings.CompressableTestSize;
defStream.Write(stream.GetBuffer(), i, Settings.CompressableTestSize);
}
defStream.Close();
float compression = (float)deflated.Length / (float)(inputSize + 1);
return compression <= Settings.CompressionTreshold;
}
public static string BytesToStr(byte[] bytes)
{
StringBuilder str = new StringBuilder();
foreach (byte b in bytes)
str.AppendFormat("{0:X2}", b);
return str.ToString();
}
public static unsafe void WriteInt(MemoryStream dst, int i)
{
dst.Position = dst.Length;
dst.SetLength(dst.Length + sizeof(int));
Marshal.Copy(new IntPtr(&i), dst.GetBuffer(), (int)dst.Position, sizeof(int));
dst.Position = dst.Length;
}
public static unsafe void WriteArray<T>(MemoryStream dst, T[] array) where T : struct
{
int size = array.Length * Marshal.SizeOf(typeof(T));
WriteInt(dst, size);
dst.Position = dst.Length;
dst.SetLength(dst.Length + size);
GCHandle gch = GCHandle.Alloc(array, GCHandleType.Pinned);
IntPtr ptrArray = gch.AddrOfPinnedObject();
Marshal.Copy(ptrArray, dst.GetBuffer(), (int)dst.Position, size);
dst.Position = dst.Length;
gch.Free();
}
public static unsafe void WriteStream(MemoryStream dst, MemoryStream src)
{
WriteInt(dst, (int)src.Length);
src.WriteTo(dst);
}
public static unsafe int ReadInt(MemoryStream src)
{
int i;
Marshal.Copy(src.GetBuffer(), (int)src.Position, new IntPtr(&i), sizeof(int));
src.Position += sizeof(int);
return i;
}
public static unsafe T[] ReadArray<T>(MemoryStream src)
{
int size = ReadInt(src);
int count = size / Marshal.SizeOf(typeof(T));
if (size % Marshal.SizeOf(typeof(T)) != 0)
throw new Exception("Total size must be multiple of array element size");
T[] array = new T[count];
GCHandle gch = GCHandle.Alloc(array, GCHandleType.Pinned);
IntPtr ptrArray = gch.AddrOfPinnedObject();
Marshal.Copy(src.GetBuffer(), (int)src.Position, ptrArray, size);
gch.Free();
src.Position += size;
return array;
}
public static MemoryStream ReadStream(MemoryStream src)
{
int size = ReadInt(src);
MemoryStream dst = new MemoryStream(size);
dst.SetLength(size);
Array.Copy(src.GetBuffer(), src.Position, dst.GetBuffer(), 0, size);
src.Position += size;
return dst;
}
public static MemoryStream ExtractMetaData(ZipFile zipFile, string metadata)
{
ZipEntry zipEntry = zipFile[Settings.MetaDataDir + "/" + metadata];
MemoryStream stream = new MemoryStream((int)zipEntry.UncompressedSize);
zipEntry.Extract(stream);
stream.Position = 0;
return stream;
}
}
/// <summary>
/// Save memory by releasing duplicate strings from memory
/// </summary>
class StringCompressor
{
Dictionary<string, string> dict = new Dictionary<string, string>();
public string Compress(string str)
{
if (str == null) {
return null;
} else {
string oldOne;
if (dict.TryGetValue(str, out oldOne)) {
return oldOne;
} else {
dict.Add(str, str);
return str;
}
}
}
public void Compress(ref string str)
{
str = Compress(str);
}
}
class WorkerThread
{
// Pulse it when content changes
Queue<MethodInvoker> pendingFlushes = new Queue<MethodInvoker>();
Thread processingThread;
bool exit = false;
public void Enqueue(MethodInvoker method)
{
if (Settings.AsyncWrite) {
// Asynchronous compression
lock (pendingFlushes) {
while (pendingFlushes.Count >= Settings.MaxQueuedWrites) {
Monitor.Wait(pendingFlushes);
}
pendingFlushes.Enqueue(method);
Monitor.PulseAll(pendingFlushes);
}
if (processingThread == null) {
processingThread = new Thread(ProcessFlushes);
processingThread.Name = "WorkerThread";
processingThread.Start();
}
}
else {
method();
}
}
void ProcessFlushes()
{
while (!exit) {
MethodInvoker next;
lock (pendingFlushes) {
if (pendingFlushes.Count > 0) {
next = pendingFlushes.Dequeue();
Monitor.PulseAll(pendingFlushes);
}
else {
Monitor.Wait(pendingFlushes, TimeSpan.FromSeconds(1));
continue;
}
}
next();
}
}
public void WaitUntilDoneAndExit()
{
ManualResetEvent done = new ManualResetEvent(false);
Enqueue((MethodInvoker)delegate { done.Set(); Thread.Sleep(0); });
done.WaitOne();
exit = true;
}
}
static class StreamPool
{
static Queue<WeakReference> free = new Queue<WeakReference>();
static object syncObject = new object();
static public int Capacity = Settings.MaxZipEntrySize;
public static MemoryStream Allocate()
{
lock(syncObject) {
while(free.Count > 0) {
WeakReference weakRef = free.Dequeue();
MemoryStream stream = (MemoryStream)weakRef.Target;
if (stream != null) {
stream.Position = 0;
stream.SetLength(0);
return stream;
}
}
return new MemoryStream(Capacity);
}
}
public static void Release(ref MemoryStream stream)
{
lock(syncObject) {
free.Enqueue(new WeakReference(stream));
stream = null;
}
}
}
}