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

COVERAGE SUMMARY FOR SOURCE FILE [BaseSerializingTranscoder.java]

nameclass, %method, %block, %line, %
BaseSerializingTranscoder.java100% (1/1)100% (11/11)95%  (272/286)97%  (79.5/82)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class BaseSerializingTranscoder100% (1/1)100% (11/11)95%  (272/286)97%  (79.5/82)
compress (byte []): byte [] 100% (1/1)78%  (49/63)82%  (11.5/14)
BaseSerializingTranscoder (int): void 100% (1/1)100% (12/12)100% (5/5)
asyncDecode (CachedData): boolean 100% (1/1)100% (2/2)100% (1/1)
decodeString (byte []): String 100% (1/1)100% (20/20)100% (7/7)
decompress (byte []): byte [] 100% (1/1)100% (51/51)100% (14/14)
deserialize (byte []): Object 100% (1/1)100% (59/59)100% (13/13)
encodeString (String): byte [] 100% (1/1)100% (16/16)100% (6/6)
getMaxSize (): int 100% (1/1)100% (3/3)100% (1/1)
serialize (Object): byte [] 100% (1/1)100% (38/38)100% (13/13)
setCharset (String): void 100% (1/1)100% (18/18)100% (6/6)
setCompressionThreshold (int): void 100% (1/1)100% (4/4)100% (2/2)

1package net.spy.memcached.transcoders;
2 
3import java.io.ByteArrayInputStream;
4import java.io.ByteArrayOutputStream;
5import java.io.IOException;
6import java.io.ObjectInputStream;
7import java.io.ObjectOutputStream;
8import java.io.UnsupportedEncodingException;
9import java.util.zip.GZIPInputStream;
10import java.util.zip.GZIPOutputStream;
11 
12import net.spy.memcached.CachedData;
13import net.spy.memcached.compat.CloseUtil;
14import 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 */
20public 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}

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