EMMA Coverage Report (generated Tue Oct 27 11:32:50 PDT 2009)
[all classes][net.spy.memcached.transcoders]

COVERAGE SUMMARY FOR SOURCE FILE [WhalinV1Transcoder.java]

nameclass, %method, %block, %line, %
WhalinV1Transcoder.java100% (1/1)100% (26/26)89%  (620/698)94%  (135/144)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class WhalinV1Transcoder100% (1/1)100% (26/26)89%  (620/698)94%  (135/144)
decodeW1String (byte []): String 100% (1/1)67%  (12/18)33%  (1/3)
decodeByte (byte []): Byte 100% (1/1)72%  (13/18)85%  (2.5/3)
decodeInteger (byte []): Integer 100% (1/1)72%  (13/18)77%  (1.5/2)
<static initializer> 100% (1/1)75%  (6/8)75%  (0.8/1)
decodeDouble (byte []): Double 100% (1/1)75%  (15/20)85%  (2.5/3)
decodeFloat (byte []): Float 100% (1/1)75%  (15/20)85%  (2.5/3)
decodeBoolean (byte []): Boolean 100% (1/1)76%  (16/21)77%  (1.5/2)
encodeW1String (String): byte [] 100% (1/1)82%  (27/33)78%  (7/9)
encode (Object): CachedData 100% (1/1)87%  (180/208)96%  (35.5/37)
decode (CachedData): Object 100% (1/1)90%  (103/114)97%  (33/34)
WhalinV1Transcoder (): void 100% (1/1)100% (4/4)100% (2/2)
decodeCharacter (byte []): Character 100% (1/1)100% (7/7)100% (1/1)
decodeLong (byte []): Long 100% (1/1)100% (30/30)100% (5/5)
decodeShort (byte []): Short 100% (1/1)100% (7/7)100% (1/1)
encodeBoolean (Boolean): byte [] 100% (1/1)100% (19/19)100% (4/4)
encodeByte (Byte): byte [] 100% (1/1)100% (14/14)100% (4/4)
encodeCharacter (Character): byte [] 100% (1/1)100% (12/12)100% (3/3)
encodeDouble (Double): byte [] 100% (1/1)100% (13/13)100% (3/3)
encodeFloat (Float): byte [] 100% (1/1)100% (13/13)100% (3/3)
encodeInteger (Integer): byte [] 100% (1/1)100% (13/13)100% (3/3)
encodeLong (Long): byte [] 100% (1/1)100% (5/5)100% (1/1)
encodeLong (Long, int): byte [] 100% (1/1)100% (13/13)100% (3/3)
encodeNum (long, int): byte [] 100% (1/1)100% (36/36)100% (5/5)
encodeShort (Short): byte [] 100% (1/1)100% (12/12)100% (3/3)
encodeStringBuffer (StringBuffer): byte [] 100% (1/1)100% (11/11)100% (3/3)
encodeStringbuilder (StringBuilder): byte [] 100% (1/1)100% (11/11)100% (3/3)

1package net.spy.memcached.transcoders;
2 
3import java.io.UnsupportedEncodingException;
4import java.util.Date;
5 
6import 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 */
15public 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}

[all classes][net.spy.memcached.transcoders]
EMMA 2.0.5312 (C) Vladimir Roubtsov