-
Notifications
You must be signed in to change notification settings - Fork 415
/
SerializingTranscoder.java
318 lines (290 loc) · 7.93 KB
/
SerializingTranscoder.java
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright (c) 2006 Dustin Sallings <dustin@spy.net>
package net.spy.memcached;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import net.spy.SpyObject;
import net.spy.util.CloseUtil;
/**
* Transcoder that serializes and compresses objects.
*/
public final class SerializingTranscoder extends SpyObject
implements Transcoder {
/**
* Default compression threshold value.
*/
public static final int DEFAULT_COMPRESSION_THRESHOLD = 16384;
// General flags
static final int SERIALIZED=1;
static final int COMPRESSED=2;
// Special flags for specially handled types.
private static final int SPECIAL_MASK=0xff00;
private static final int SPECIAL_BOOLEAN=(1<<8);
private static final int SPECIAL_INT=(2<<8);
private static final int SPECIAL_LONG=(3<<8);
private static final int SPECIAL_DATE=(4<<8);
private static final int SPECIAL_BYTE=(5<<8);
private static final int SPECIAL_FLOAT=(6<<8);
private static final int SPECIAL_DOUBLE=(7<<8);
private static final int SPECIAL_BYTEARRAY=(8<<8);
private static final String DEFAULT_CHARSET = "UTF-8";
private int compressionThreshold=DEFAULT_COMPRESSION_THRESHOLD;
private String charset=DEFAULT_CHARSET;
/**
* Set the compression threshold to the given number of bytes. This
* transcoder will attempt to compress any data being stored that's larger
* than this.
*
* @param to the number of bytes
*/
public void setCompressionThreshold(int to) {
compressionThreshold=to;
}
/**
* Set the character set for string value transcoding (defaults to UTF-8).
*/
public void setCharset(String to) {
// Validate the character set.
try {
new String(new byte[97], to);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
charset=to;
}
/* (non-Javadoc)
* @see net.spy.memcached.Transcoder#decode(net.spy.memcached.CachedData)
*/
public Object decode(CachedData d) {
byte[] data=d.getData();
Object rv=null;
if((d.getFlags() & COMPRESSED) != 0) {
data=decompress(d.getData());
}
if((d.getFlags() & SERIALIZED) != 0) {
rv=deserialize(data);
} else if((d.getFlags() & SPECIAL_MASK) != 0) {
switch(d.getFlags() & SPECIAL_MASK) {
case SPECIAL_BOOLEAN:
rv=Boolean.valueOf(decodeBoolean(data));
break;
case SPECIAL_INT:
rv=new Integer(decodeInt(data));
break;
case SPECIAL_LONG:
rv=new Long(decodeLong(data));
break;
case SPECIAL_DATE:
rv=new Date(decodeLong(data));
break;
case SPECIAL_BYTE:
rv=new Byte(decodeByte(data));
break;
case SPECIAL_FLOAT:
rv=new Float(Float.intBitsToFloat(decodeInt(data)));
break;
case SPECIAL_DOUBLE:
rv=new Double(Double.longBitsToDouble(decodeLong(data)));
break;
case SPECIAL_BYTEARRAY:
rv=data;
break;
default: assert false;
}
} else {
try {
rv=new String(data, charset);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
return rv;
}
/* (non-Javadoc)
* @see net.spy.memcached.Transcoder#encode(java.lang.Object)
*/
public CachedData encode(Object o) {
CachedData rv=null;
byte[] b=null;
int flags=0;
if(o instanceof String) {
try {
b=((String)o).getBytes(charset);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
} else if(o instanceof Long) {
b=encodeLong((Long)o);
flags |= SPECIAL_LONG;
} else if(o instanceof Integer) {
b=encodeInt((Integer)o);
flags |= SPECIAL_INT;
} else if(o instanceof Boolean) {
b=encodeBoolean((Boolean)o);
flags |= SPECIAL_BOOLEAN;
} else if(o instanceof Date) {
b=encodeLong(((Date)o).getTime());
flags |= SPECIAL_DATE;
} else if(o instanceof Byte) {
b=encodeByte((Byte)o);
flags |= SPECIAL_BYTE;
} else if(o instanceof Float) {
b=encodeInt(Float.floatToRawIntBits((Float)o));
flags |= SPECIAL_FLOAT;
} else if(o instanceof Double) {
b=encodeLong(Double.doubleToRawLongBits((Double)o));
flags |= SPECIAL_DOUBLE;
} else if(o instanceof byte[]) {
b=(byte[])o;
flags |= SPECIAL_BYTEARRAY;
} else {
b=serialize(o);
flags |= SERIALIZED;
}
if(b != null) {
if(b.length > compressionThreshold) {
byte[] compressed=compress(b);
if(compressed.length < b.length) {
getLogger().info("Compressed %s from %d to %d",
o.getClass().getName(), b.length, compressed.length);
b=compressed;
flags |= COMPRESSED;
} else {
getLogger().info(
"Compression increased the size of %s from %d to %d",
o.getClass().getName(), b.length, compressed.length);
}
}
rv=new CachedData(flags, b);
}
return rv;
}
private byte[] serialize(Object o) {
assert o != null;
byte[] rv=null;
try {
ByteArrayOutputStream bos=new ByteArrayOutputStream();
ObjectOutputStream os=new ObjectOutputStream(bos);
os.writeObject(o);
os.close();
bos.close();
rv=bos.toByteArray();
} catch(IOException e) {
throw new IllegalArgumentException("Non-serializable object", e);
}
return rv;
}
private Object deserialize(byte[] in) {
Object rv=null;
assert in != null;
try {
ByteArrayInputStream bis=new ByteArrayInputStream(in);
ObjectInputStream is=new ObjectInputStream(bis);
rv=is.readObject();
is.close();
bis.close();
} catch(IOException e) {
getLogger().warn("Caught IOException decoding %d bytes of data",
in.length, e);
} catch (ClassNotFoundException e) {
getLogger().warn("Caught CNFE decoding %d bytes of data",
in.length, e);
}
return rv;
}
private byte[] compress(byte[] in) {
assert in != null;
ByteArrayOutputStream bos=new ByteArrayOutputStream();
GZIPOutputStream gz=null;
try {
gz = new GZIPOutputStream(bos);
gz.write(in);
} catch (IOException e) {
throw new RuntimeException("IO exception compressing data", e);
} finally {
CloseUtil.close(gz);
CloseUtil.close(bos);
}
byte[] rv=bos.toByteArray();
getLogger().debug("Compressed %d bytes to %d", in.length, rv.length);
return rv;
}
private byte[] decompress(byte[] in) {
assert in != null;
ByteArrayInputStream bis=new ByteArrayInputStream(in);
ByteArrayOutputStream bos=new ByteArrayOutputStream();
GZIPInputStream gis;
try {
gis = new GZIPInputStream(bis);
byte[] buf=new byte[8192];
int r=-1;
while((r=gis.read(buf)) > 0) {
bos.write(buf, 0, r);
}
} catch (IOException e) {
throw new RuntimeException("Error decompressing data", e);
}
return bos.toByteArray();
}
private byte[] encodeNum(long l, int maxBytes) {
byte[] rv=new byte[maxBytes];
for(int i=0; i<rv.length; i++) {
int pos=rv.length-i-1;
rv[pos]=(byte) ((l >> (8 * i)) & 0xff);
}
int firstNonZero=0;
for(;firstNonZero<rv.length && rv[firstNonZero]==0; firstNonZero++) {
// Just looking for what we can reduce
}
if(firstNonZero > 0) {
byte[] tmp=new byte[rv.length - firstNonZero];
System.arraycopy(rv, firstNonZero, tmp, 0, rv.length-firstNonZero);
rv=tmp;
}
return rv;
}
byte[] encodeLong(long l) {
return encodeNum(l, 8);
}
long decodeLong(byte[] b) {
long rv=0;
for(byte i : b) {
rv = (rv << 8) | (i<0?256+i:i);
}
return rv;
}
byte[] encodeInt(int in) {
return encodeNum(in, 4);
}
int decodeInt(byte[] in) {
assert in.length <= 4
: "Too long to be an int (" + in.length + ") bytes";
return (int)decodeLong(in);
}
byte[] encodeByte(byte in) {
return new byte[]{in};
}
byte decodeByte(byte[] in) {
assert in.length <= 1 : "Too long for a byte";
byte rv=0;
if(in.length == 1) {
rv=in[0];
}
return rv;
}
byte[] encodeBoolean(boolean b) {
byte[] rv=new byte[1];
rv[0]=(byte)(b?'1':'0');
return rv;
}
boolean decodeBoolean(byte[] in) {
assert in.length == 1 : "Wrong length for a boolean";
return in[0] == '1';
}
}