-
Notifications
You must be signed in to change notification settings - Fork 0
/
Serializer.cs
189 lines (179 loc) · 6.03 KB
/
Serializer.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
//Copyright (c) 2007-2008 Henrik Schröder, Oliver Kofoed Pedersen
//Permission is hereby granted, free of charge, to any person
//obtaining a copy of this software and associated documentation
//files (the "Software"), to deal in the Software without
//restriction, including without limitation the rights to use,
//copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the
//Software is furnished to do so, subject to the following
//conditions:
//The above copyright notice and this permission notice shall be
//included in all copies or substantial portions of the Software.
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//OTHER DEALINGS IN THE SOFTWARE.
using System;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
namespace BeIT.MemCached
{
internal enum SerializedType : ushort
{
ByteArray = 0,
Object = 1,
String = 2,
Datetime = 3,
Bool = 4,
//SByte = 5, //Makes no sense.
Byte = 6,
Short = 7,
UShort = 8,
Int = 9,
UInt = 10,
Long = 11,
ULong = 12,
Float = 13,
Double = 14,
CompressedByteArray = 255,
CompressedObject = 256,
CompressedString = 257,
}
internal class Serializer
{
public static byte[] Serialize(object value, out SerializedType type, uint compressionThreshold) {
byte[] bytes;
if (value is byte[]) {
bytes = (byte[])value;
type = SerializedType.ByteArray;
if (bytes.Length > compressionThreshold) {
bytes = compress(bytes);
type = SerializedType.CompressedByteArray;
}
} else if (value is string) {
bytes = Encoding.UTF8.GetBytes((string)value);
type = SerializedType.String;
if (bytes.Length > compressionThreshold) {
bytes = compress(bytes);
type = SerializedType.CompressedString;
}
} else if (value is DateTime) {
bytes = BitConverter.GetBytes(((DateTime)value).Ticks);
type = SerializedType.Datetime;
} else if (value is bool) {
bytes = new byte[]{(byte)((bool)value ? 1 : 0)};
type = SerializedType.Bool;
} else if (value is byte) {
bytes = new byte[]{(byte)value};
type = SerializedType.Byte;
} else if (value is short) {
bytes = BitConverter.GetBytes((short)value);
type = SerializedType.Short;
} else if (value is ushort) {
bytes = BitConverter.GetBytes((ushort)value);
type = SerializedType.UShort;
} else if (value is int) {
bytes = BitConverter.GetBytes((int)value);
type = SerializedType.Int;
} else if (value is uint) {
bytes = BitConverter.GetBytes((uint)value);
type = SerializedType.UInt;
} else if (value is long) {
bytes = BitConverter.GetBytes((long)value);
type = SerializedType.Long;
} else if (value is ulong) {
bytes = BitConverter.GetBytes((ulong)value);
type = SerializedType.ULong;
} else if (value is float) {
bytes = BitConverter.GetBytes((float)value);
type = SerializedType.Float;
} else if (value is double) {
bytes = BitConverter.GetBytes((double)value);
type = SerializedType.Double;
} else {
//Object
using(MemoryStream ms = new MemoryStream()) {
new BinaryFormatter().Serialize(ms, value);
bytes = ms.ToArray();
type = SerializedType.Object;
if (bytes.Length > compressionThreshold) {
bytes = compress(bytes);
type = SerializedType.CompressedObject;
}
}
}
return bytes;
}
private static byte[] compress(byte[] bytes) {
using (MemoryStream ms = new MemoryStream()) {
using (DeflateStream gzs = new DeflateStream(ms, CompressionMode.Compress, false)) {
gzs.Write(bytes, 0, bytes.Length);
}
ms.Close();
return ms.ToArray();
}
}
private static byte[] decompress (byte[] bytes) {
using (MemoryStream ms = new MemoryStream(bytes, false)) {
using(DeflateStream gzs = new DeflateStream(ms, CompressionMode.Decompress, false)) {
using(MemoryStream dest = new MemoryStream()) {
byte[] tmp = new byte[bytes.Length];
int read;
while ((read = gzs.Read(tmp, 0, tmp.Length)) != 0) {
dest.Write(tmp, 0, read);
}
dest.Close();
return dest.ToArray();
}
}
}
}
public static object DeSerialize(byte[] bytes, SerializedType type) {
switch (type) {
case SerializedType.String:
return Encoding.UTF8.GetString(bytes);
case SerializedType.Datetime:
return new DateTime(BitConverter.ToInt64(bytes, 0));
case SerializedType.Bool:
return bytes[0] == 1;
case SerializedType.Byte:
return bytes[0];
case SerializedType.Short:
return BitConverter.ToInt16(bytes, 0);
case SerializedType.UShort:
return BitConverter.ToUInt16(bytes, 0);
case SerializedType.Int:
return BitConverter.ToInt32(bytes, 0);
case SerializedType.UInt:
return BitConverter.ToUInt32(bytes, 0);
case SerializedType.Long:
return BitConverter.ToInt64(bytes, 0);
case SerializedType.ULong:
return BitConverter.ToUInt64(bytes, 0);
case SerializedType.Float:
return BitConverter.ToSingle(bytes, 0);
case SerializedType.Double:
return BitConverter.ToDouble(bytes, 0);
case SerializedType.Object:
using(MemoryStream ms = new MemoryStream(bytes)) {
return new BinaryFormatter().Deserialize(ms);
}
case SerializedType.CompressedByteArray:
return DeSerialize(decompress(bytes), SerializedType.ByteArray);
case SerializedType.CompressedString:
return DeSerialize(decompress(bytes), SerializedType.String);
case SerializedType.CompressedObject:
return DeSerialize(decompress(bytes), SerializedType.Object);
case SerializedType.ByteArray:
default:
return bytes;
}
}
}
}