1 | package net.spy.memcached.transcoders; |
2 | |
3 | import java.io.ByteArrayInputStream; |
4 | import java.io.ByteArrayOutputStream; |
5 | import java.io.IOException; |
6 | import java.io.ObjectInputStream; |
7 | import java.io.ObjectOutputStream; |
8 | import java.io.UnsupportedEncodingException; |
9 | import java.util.zip.GZIPInputStream; |
10 | import java.util.zip.GZIPOutputStream; |
11 | |
12 | import net.spy.memcached.CachedData; |
13 | import net.spy.memcached.compat.CloseUtil; |
14 | import net.spy.memcached.compat.SpyObject; |
15 | |
16 | /** |
17 | * Base class for any transcoders that may want to work with serialized or |
18 | * compressed data. |
19 | */ |
20 | public abstract class BaseSerializingTranscoder extends SpyObject { |
21 | |
22 | /** |
23 | * Default compression threshold value. |
24 | */ |
25 | public static final int DEFAULT_COMPRESSION_THRESHOLD = 16384; |
26 | |
27 | private static final String DEFAULT_CHARSET = "UTF-8"; |
28 | |
29 | protected int compressionThreshold=DEFAULT_COMPRESSION_THRESHOLD; |
30 | protected String charset=DEFAULT_CHARSET; |
31 | |
32 | private final int maxSize; |
33 | |
34 | /** |
35 | * Initialize a serializing transcoder with the given maximum data size. |
36 | */ |
37 | public BaseSerializingTranscoder(int max) { |
38 | super(); |
39 | maxSize = max; |
40 | } |
41 | |
42 | public boolean asyncDecode(CachedData d) { |
43 | return false; |
44 | } |
45 | |
46 | /** |
47 | * Set the compression threshold to the given number of bytes. This |
48 | * transcoder will attempt to compress any data being stored that's larger |
49 | * than this. |
50 | * |
51 | * @param to the number of bytes |
52 | */ |
53 | public void setCompressionThreshold(int to) { |
54 | compressionThreshold=to; |
55 | } |
56 | |
57 | /** |
58 | * Set the character set for string value transcoding (defaults to UTF-8). |
59 | */ |
60 | public void setCharset(String to) { |
61 | // Validate the character set. |
62 | try { |
63 | new String(new byte[97], to); |
64 | } catch (UnsupportedEncodingException e) { |
65 | throw new RuntimeException(e); |
66 | } |
67 | charset=to; |
68 | } |
69 | |
70 | /** |
71 | * Get the bytes representing the given serialized object. |
72 | */ |
73 | protected byte[] serialize(Object o) { |
74 | if(o == null) { |
75 | throw new NullPointerException("Can't serialize null"); |
76 | } |
77 | byte[] rv=null; |
78 | try { |
79 | ByteArrayOutputStream bos=new ByteArrayOutputStream(); |
80 | ObjectOutputStream os=new ObjectOutputStream(bos); |
81 | os.writeObject(o); |
82 | os.close(); |
83 | bos.close(); |
84 | rv=bos.toByteArray(); |
85 | } catch(IOException e) { |
86 | throw new IllegalArgumentException("Non-serializable object", e); |
87 | } |
88 | return rv; |
89 | } |
90 | |
91 | /** |
92 | * Get the object represented by the given serialized bytes. |
93 | */ |
94 | protected Object deserialize(byte[] in) { |
95 | Object rv=null; |
96 | try { |
97 | if(in != null) { |
98 | ByteArrayInputStream bis=new ByteArrayInputStream(in); |
99 | ObjectInputStream is=new ObjectInputStream(bis); |
100 | rv=is.readObject(); |
101 | is.close(); |
102 | bis.close(); |
103 | } |
104 | } catch(IOException e) { |
105 | getLogger().warn("Caught IOException decoding %d bytes of data", |
106 | in.length, e); |
107 | } catch (ClassNotFoundException e) { |
108 | getLogger().warn("Caught CNFE decoding %d bytes of data", |
109 | in.length, e); |
110 | } |
111 | return rv; |
112 | } |
113 | |
114 | /** |
115 | * Compress the given array of bytes. |
116 | */ |
117 | protected byte[] compress(byte[] in) { |
118 | if(in == null) { |
119 | throw new NullPointerException("Can't compress null"); |
120 | } |
121 | ByteArrayOutputStream bos=new ByteArrayOutputStream(); |
122 | GZIPOutputStream gz=null; |
123 | try { |
124 | gz = new GZIPOutputStream(bos); |
125 | gz.write(in); |
126 | } catch (IOException e) { |
127 | throw new RuntimeException("IO exception compressing data", e); |
128 | } finally { |
129 | CloseUtil.close(gz); |
130 | CloseUtil.close(bos); |
131 | } |
132 | byte[] rv=bos.toByteArray(); |
133 | getLogger().debug("Compressed %d bytes to %d", in.length, rv.length); |
134 | return rv; |
135 | } |
136 | |
137 | /** |
138 | * Decompress the given array of bytes. |
139 | * |
140 | * @return null if the bytes cannot be decompressed |
141 | */ |
142 | protected byte[] decompress(byte[] in) { |
143 | ByteArrayOutputStream bos=null; |
144 | if(in != null) { |
145 | ByteArrayInputStream bis=new ByteArrayInputStream(in); |
146 | bos=new ByteArrayOutputStream(); |
147 | GZIPInputStream gis; |
148 | try { |
149 | gis = new GZIPInputStream(bis); |
150 | |
151 | byte[] buf=new byte[8192]; |
152 | int r=-1; |
153 | while((r=gis.read(buf)) > 0) { |
154 | bos.write(buf, 0, r); |
155 | } |
156 | } catch (IOException e) { |
157 | getLogger().warn("Failed to decompress data", e); |
158 | bos = null; |
159 | } |
160 | } |
161 | return bos == null ? null : bos.toByteArray(); |
162 | } |
163 | |
164 | /** |
165 | * Decode the string with the current character set. |
166 | */ |
167 | protected String decodeString(byte[] data) { |
168 | String rv=null; |
169 | try { |
170 | if(data != null) { |
171 | rv=new String(data, charset); |
172 | } |
173 | } catch (UnsupportedEncodingException e) { |
174 | throw new RuntimeException(e); |
175 | } |
176 | return rv; |
177 | } |
178 | |
179 | /** |
180 | * Encode a string into the current character set. |
181 | */ |
182 | protected byte[] encodeString(String in) { |
183 | byte[] rv=null; |
184 | try { |
185 | rv=in.getBytes(charset); |
186 | } catch (UnsupportedEncodingException e) { |
187 | throw new RuntimeException(e); |
188 | } |
189 | return rv; |
190 | } |
191 | |
192 | public int getMaxSize() { |
193 | return maxSize; |
194 | } |
195 | |
196 | } |