-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSyncEngineHelpers.cs
More file actions
72 lines (66 loc) · 2.38 KB
/
SyncEngineHelpers.cs
File metadata and controls
72 lines (66 loc) · 2.38 KB
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic.Core;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NETCoreSync.Exceptions;
using System.Reflection;
using System.IO;
using System.IO.Compression;
namespace NETCoreSync
{
public abstract partial class SyncEngine
{
private static SyncConfiguration.SchemaInfo GetSchemaInfo(SyncConfiguration syncConfiguration, Type type)
{
if (syncConfiguration == null) throw new NullReferenceException(nameof(syncConfiguration));
if (!syncConfiguration.SyncSchemaInfos.ContainsKey(type)) throw new SyncEngineMissingTypeInSyncConfigurationException(type);
return syncConfiguration.SyncSchemaInfos[type];
}
internal static byte[] Compress(string text)
{
var bytes = Encoding.Unicode.GetBytes(text);
using (var mso = new MemoryStream())
{
using (var gs = new GZipStream(mso, CompressionMode.Compress))
{
gs.Write(bytes, 0, bytes.Length);
}
return mso.ToArray();
}
}
internal static string Decompress(byte[] data)
{
// Read the last 4 bytes to get the length
byte[] lengthBuffer = new byte[4];
Array.Copy(data, data.Length - 4, lengthBuffer, 0, 4);
int uncompressedSize = BitConverter.ToInt32(lengthBuffer, 0);
var buffer = new byte[uncompressedSize];
using (var ms = new MemoryStream(data))
{
using (var gzip = new GZipStream(ms, CompressionMode.Decompress))
{
int totalRead = 0;
while (totalRead < buffer.Length)
{
int bytesRead = gzip.Read(buffer, totalRead, buffer.Length - totalRead);
if (bytesRead == 0) break;
totalRead += bytesRead;
}
}
}
return Encoding.Unicode.GetString(buffer);
}
internal protected static long GetNowTicks()
{
return DateTime.Now.Ticks;
}
internal protected static long GetMinValueTicks()
{
return DateTime.MinValue.Ticks;
}
}
}