| 1 | package net.spy.memcached.transcoders; |
| 2 | |
| 3 | import java.io.UnsupportedEncodingException; |
| 4 | import java.util.Date; |
| 5 | |
| 6 | import net.spy.memcached.CachedData; |
| 7 | |
| 8 | /** |
| 9 | * Handles old whalin (tested with v1.6) encoding: data type is in the first |
| 10 | * byte of the value. |
| 11 | * |
| 12 | * @author bpartensky |
| 13 | * @since Oct 16, 2008 |
| 14 | */ |
| 15 | public class WhalinV1Transcoder extends BaseSerializingTranscoder |
| 16 | implements Transcoder<Object> { |
| 17 | |
| 18 | public static final int SPECIAL_BYTE = 1; |
| 19 | public static final int SPECIAL_BOOLEAN = 2; |
| 20 | public static final int SPECIAL_INTEGER = 3; |
| 21 | public static final int SPECIAL_LONG = 4; |
| 22 | public static final int SPECIAL_CHARACTER = 5; |
| 23 | public static final int SPECIAL_STRING = 6; |
| 24 | public static final int SPECIAL_STRINGBUFFER = 7; |
| 25 | public static final int SPECIAL_FLOAT = 8; |
| 26 | public static final int SPECIAL_SHORT = 9; |
| 27 | public static final int SPECIAL_DOUBLE = 10; |
| 28 | public static final int SPECIAL_DATE = 11; |
| 29 | public static final int SPECIAL_STRINGBUILDER = 12; |
| 30 | public static final int COMPRESSED = 2; |
| 31 | public static final int SERIALIZED = 8; |
| 32 | |
| 33 | public WhalinV1Transcoder() { |
| 34 | super(CachedData.MAX_SIZE); |
| 35 | } |
| 36 | |
| 37 | public CachedData encode(Object o) { |
| 38 | byte[] b = null; |
| 39 | int flags = 0; |
| 40 | if (o instanceof String) { |
| 41 | b = encodeW1String((String) o); |
| 42 | } else if (o instanceof StringBuffer) { |
| 43 | b = encodeStringBuffer((StringBuffer) o); |
| 44 | } else if (o instanceof StringBuilder) { |
| 45 | b = encodeStringbuilder((StringBuilder) o); |
| 46 | } else if (o instanceof Long) { |
| 47 | b = encodeLong((Long) o); |
| 48 | } else if (o instanceof Integer) { |
| 49 | b = encodeInteger((Integer) o); |
| 50 | } else if (o instanceof Short) { |
| 51 | b = encodeShort((Short) o); |
| 52 | } else if (o instanceof Boolean) { |
| 53 | b = encodeBoolean((Boolean) o); |
| 54 | } else if (o instanceof Date) { |
| 55 | b = encodeLong(((Date) o).getTime(), SPECIAL_DATE); |
| 56 | } else if (o instanceof Byte) { |
| 57 | b = encodeByte((Byte) o); |
| 58 | } else if (o instanceof Float) { |
| 59 | b = encodeFloat((Float) o); |
| 60 | } else if (o instanceof Double) { |
| 61 | b = encodeDouble((Double) o); |
| 62 | } else if (o instanceof Character) { |
| 63 | b = encodeCharacter((Character) o); |
| 64 | } else { |
| 65 | b = serialize(o); |
| 66 | flags |= SERIALIZED; |
| 67 | } |
| 68 | assert b != null; |
| 69 | if (b.length > compressionThreshold) { |
| 70 | byte[] compressed = compress(b); |
| 71 | if (compressed.length < b.length) { |
| 72 | getLogger().info("Compressed %s from %d to %d", |
| 73 | o.getClass().getName(), b.length, compressed.length); |
| 74 | b = compressed; |
| 75 | flags |= COMPRESSED; |
| 76 | } else { |
| 77 | getLogger().info( |
| 78 | "Compression increased the size of %s from %d to %d", |
| 79 | o.getClass().getName(), b.length, compressed.length); |
| 80 | } |
| 81 | } |
| 82 | return new CachedData(flags, b, getMaxSize()); |
| 83 | } |
| 84 | |
| 85 | public Object decode(CachedData d) { |
| 86 | byte[] data = d.getData(); |
| 87 | Object rv = null; |
| 88 | if ((d.getFlags() & COMPRESSED) != 0) { |
| 89 | data = decompress(d.getData()); |
| 90 | } |
| 91 | if ((d.getFlags() & SERIALIZED) != 0) { |
| 92 | rv = deserialize(data); |
| 93 | } else { |
| 94 | int f = data[0]; |
| 95 | switch (f) { |
| 96 | case SPECIAL_BOOLEAN: |
| 97 | rv = decodeBoolean(data); |
| 98 | break; |
| 99 | case SPECIAL_INTEGER: |
| 100 | rv = decodeInteger(data); |
| 101 | break; |
| 102 | case SPECIAL_SHORT: |
| 103 | rv = decodeShort(data); |
| 104 | break; |
| 105 | case SPECIAL_LONG: |
| 106 | rv = decodeLong(data); |
| 107 | break; |
| 108 | case SPECIAL_DATE: |
| 109 | rv = new Date(decodeLong(data)); |
| 110 | break; |
| 111 | case SPECIAL_BYTE: |
| 112 | rv = decodeByte(data); |
| 113 | break; |
| 114 | case SPECIAL_FLOAT: |
| 115 | rv = decodeFloat(data); |
| 116 | break; |
| 117 | case SPECIAL_DOUBLE: |
| 118 | rv = decodeDouble(data); |
| 119 | break; |
| 120 | case SPECIAL_STRING: |
| 121 | rv = decodeW1String(data); |
| 122 | break; |
| 123 | case SPECIAL_STRINGBUFFER: |
| 124 | rv = new StringBuffer(decodeW1String(data)); |
| 125 | break; |
| 126 | case SPECIAL_STRINGBUILDER: |
| 127 | rv = new StringBuilder(decodeW1String(data)); |
| 128 | break; |
| 129 | case SPECIAL_CHARACTER: |
| 130 | rv = decodeCharacter(data); |
| 131 | break; |
| 132 | default: |
| 133 | getLogger().warn("Cannot handle data with flags %x", f); |
| 134 | } |
| 135 | } |
| 136 | return rv; |
| 137 | } |
| 138 | |
| 139 | private Short decodeShort(byte[] data) { |
| 140 | return Short.valueOf((short) decodeInteger(data).intValue()); |
| 141 | } |
| 142 | |
| 143 | private Byte decodeByte(byte[] in) { |
| 144 | assert in.length == 2 : "Wrong length for a byte"; |
| 145 | byte value = in[1]; |
| 146 | return Byte.valueOf(value); |
| 147 | |
| 148 | } |
| 149 | |
| 150 | private Integer decodeInteger(byte[] in) { |
| 151 | assert in.length == 5 : "Wrong length for an int"; |
| 152 | return Integer.valueOf((int) decodeLong(in).longValue()); |
| 153 | |
| 154 | } |
| 155 | |
| 156 | private Float decodeFloat(byte[] in) { |
| 157 | assert in.length == 5 : "Wrong length for a float"; |
| 158 | Integer l = decodeInteger(in); |
| 159 | return Float.valueOf(Float.intBitsToFloat(l.intValue())); |
| 160 | } |
| 161 | |
| 162 | private Double decodeDouble(byte[] in) { |
| 163 | assert in.length == 9 : "Wrong length for a double"; |
| 164 | Long l = decodeLong(in); |
| 165 | return Double.valueOf(Double.longBitsToDouble(l.longValue())); |
| 166 | } |
| 167 | |
| 168 | private Boolean decodeBoolean(byte[] in) { |
| 169 | assert in.length == 2 : "Wrong length for a boolean"; |
| 170 | return Boolean.valueOf(in[1] == 1); |
| 171 | } |
| 172 | |
| 173 | private Long decodeLong(byte[] in) { |
| 174 | long rv = 0L; |
| 175 | for (int idx = 1; idx < in.length; idx++) { |
| 176 | byte i = in[idx]; |
| 177 | rv = (rv << 8) | (i < 0 ? 256 + i : i); |
| 178 | } |
| 179 | return Long.valueOf(rv); |
| 180 | } |
| 181 | |
| 182 | private Character decodeCharacter(byte[] b) { |
| 183 | return Character.valueOf((char) decodeInteger(b).intValue()); |
| 184 | } |
| 185 | |
| 186 | private String decodeW1String(byte[] b) { |
| 187 | try { |
| 188 | return new String(b, 1, b.length - 1, charset); |
| 189 | } catch (UnsupportedEncodingException e) { |
| 190 | throw new RuntimeException(e); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | private byte[] encodeByte(Byte value) { |
| 195 | byte[] b = new byte[2]; |
| 196 | b[0] = SPECIAL_BYTE; |
| 197 | b[1] = value.byteValue(); |
| 198 | return b; |
| 199 | } |
| 200 | |
| 201 | private byte[] encodeBoolean(Boolean value) { |
| 202 | byte[] b = new byte[2]; |
| 203 | b[0] = SPECIAL_BOOLEAN; |
| 204 | b[1] = (byte) (value.booleanValue() ? 1 : 0); |
| 205 | return b; |
| 206 | } |
| 207 | |
| 208 | private byte[] encodeInteger(Integer value) { |
| 209 | byte[] b = encodeNum(value, 4); |
| 210 | b[0] = SPECIAL_INTEGER; |
| 211 | return b; |
| 212 | } |
| 213 | |
| 214 | private byte[] encodeLong(Long value, int type) { |
| 215 | byte[] b = encodeNum(value, 8); |
| 216 | b[0] = (byte)type; |
| 217 | return b; |
| 218 | } |
| 219 | |
| 220 | private byte[] encodeLong(Long value) { |
| 221 | return encodeLong(value, SPECIAL_LONG); |
| 222 | } |
| 223 | |
| 224 | private byte[] encodeShort(Short value) { |
| 225 | byte[] b = encodeInteger((int) value.shortValue()); |
| 226 | b[0] = SPECIAL_SHORT; |
| 227 | return b; |
| 228 | } |
| 229 | |
| 230 | private byte[] encodeFloat(Float value) { |
| 231 | byte[] b = encodeInteger(Float.floatToIntBits(value)); |
| 232 | b[0] = SPECIAL_FLOAT; |
| 233 | return b; |
| 234 | } |
| 235 | |
| 236 | private byte[] encodeDouble(Double value) { |
| 237 | byte[] b = encodeLong(Double.doubleToLongBits(value)); |
| 238 | b[0] = SPECIAL_DOUBLE; |
| 239 | return b; |
| 240 | } |
| 241 | |
| 242 | private byte[] encodeCharacter(Character value) { |
| 243 | byte[] result = encodeInteger((int) value.charValue()); |
| 244 | result[0] = SPECIAL_CHARACTER; |
| 245 | return result; |
| 246 | } |
| 247 | |
| 248 | private byte[] encodeStringBuffer(StringBuffer value) { |
| 249 | byte[] b = encodeW1String(value.toString()); |
| 250 | b[0] = SPECIAL_STRINGBUFFER; |
| 251 | return b; |
| 252 | } |
| 253 | |
| 254 | private byte[] encodeStringbuilder(StringBuilder value) { |
| 255 | byte[] b = encodeW1String(value.toString()); |
| 256 | b[0] = SPECIAL_STRINGBUILDER; |
| 257 | return b; |
| 258 | } |
| 259 | |
| 260 | private byte[] encodeW1String(String value) { |
| 261 | byte[] svalue = null; |
| 262 | try { |
| 263 | svalue = value.getBytes(charset); |
| 264 | } catch (UnsupportedEncodingException e) { |
| 265 | throw new RuntimeException(e); |
| 266 | } |
| 267 | byte[] result = new byte[svalue.length + 1]; |
| 268 | System.arraycopy(svalue, 0, result, 1, svalue.length); |
| 269 | result[0] = SPECIAL_STRING; |
| 270 | return result; |
| 271 | } |
| 272 | |
| 273 | private byte[] encodeNum(long l, int maxBytes) { |
| 274 | byte[] rv = new byte[maxBytes + 1]; |
| 275 | |
| 276 | for (int i = 0; i < rv.length - 1; i++) { |
| 277 | int pos = rv.length - i - 1; |
| 278 | rv[pos] = (byte) ((l >> (8 * i)) & 0xff); |
| 279 | } |
| 280 | |
| 281 | return rv; |
| 282 | } |
| 283 | |
| 284 | } |