1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.util.io; |
16 |
| |
17 |
| import java.io.BufferedInputStream; |
18 |
| import java.io.BufferedOutputStream; |
19 |
| import java.io.ByteArrayInputStream; |
20 |
| import java.io.ByteArrayOutputStream; |
21 |
| import java.io.InputStream; |
22 |
| import java.io.ObjectInputStream; |
23 |
| import java.io.ObjectOutputStream; |
24 |
| import java.io.Serializable; |
25 |
| import java.util.zip.GZIPInputStream; |
26 |
| import java.util.zip.GZIPOutputStream; |
27 |
| |
28 |
| import org.apache.commons.codec.binary.Base64; |
29 |
| import org.apache.hivemind.ApplicationRuntimeException; |
30 |
| import org.apache.hivemind.ClassResolver; |
31 |
| import org.apache.tapestry.services.DataSqueezer; |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| public class SerializableAdaptor implements SqueezeAdaptor |
42 |
| { |
43 |
| private ClassResolver _resolver; |
44 |
| |
45 |
| private static final char BYTESTREAM_PREFIX = 'O'; |
46 |
| |
47 |
| private static final char GZIP_BYTESTREAM_PREFIX = 'Z'; |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| private static final String PREFIX = "OZ"; |
53 |
| |
54 |
78
| public String getPrefix()
|
55 |
| { |
56 |
78
| return PREFIX;
|
57 |
| } |
58 |
| |
59 |
78
| public Class getDataClass()
|
60 |
| { |
61 |
78
| return Serializable.class;
|
62 |
| } |
63 |
| |
64 |
36
| public String squeeze(DataSqueezer squeezer, Object data)
|
65 |
| { |
66 |
36
| try
|
67 |
| { |
68 |
36
| ByteArrayOutputStream bosPlain = new ByteArrayOutputStream();
|
69 |
36
| ByteArrayOutputStream bosCompressed = new ByteArrayOutputStream();
|
70 |
| |
71 |
36
| GZIPOutputStream gos = new GZIPOutputStream(bosCompressed);
|
72 |
| |
73 |
36
| TeeOutputStream tos = new TeeOutputStream(bosPlain, gos);
|
74 |
| |
75 |
36
| ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(tos));
|
76 |
| |
77 |
36
| oos.writeObject(data);
|
78 |
| |
79 |
36
| oos.close();
|
80 |
| |
81 |
36
| boolean useCompressed = bosCompressed.size() < bosPlain.size();
|
82 |
| |
83 |
36
| byte[] byteArray = useCompressed ? bosCompressed.toByteArray() : bosPlain.toByteArray();
|
84 |
| |
85 |
36
| byte[] encoded = Base64.encodeBase64(byteArray);
|
86 |
| |
87 |
36
| String prefix = Character.toString(useCompressed ? GZIP_BYTESTREAM_PREFIX
|
88 |
| : BYTESTREAM_PREFIX); |
89 |
| |
90 |
36
| return prefix + new String(encoded);
|
91 |
| } |
92 |
| catch (Exception ex) |
93 |
| { |
94 |
0
| throw new ApplicationRuntimeException(IoMessages.encodeFailure(data, ex), ex);
|
95 |
| } |
96 |
| } |
97 |
| |
98 |
12
| public Object unsqueeze(DataSqueezer squeezer, String encoded)
|
99 |
| { |
100 |
12
| char prefix = encoded.charAt(0);
|
101 |
| |
102 |
12
| try
|
103 |
| { |
104 |
| |
105 |
| |
106 |
12
| byte[] mimeData = encoded.substring(1).getBytes();
|
107 |
| |
108 |
12
| byte[] decoded = Base64.decodeBase64(mimeData);
|
109 |
| |
110 |
12
| InputStream is = new ByteArrayInputStream(decoded);
|
111 |
| |
112 |
12
| if (prefix == GZIP_BYTESTREAM_PREFIX)
|
113 |
2
| is = new GZIPInputStream(is);
|
114 |
| |
115 |
12
| is = new BufferedInputStream(is);
|
116 |
| |
117 |
12
| ObjectInputStream ois = new ResolvingObjectInputStream(_resolver, is);
|
118 |
| |
119 |
12
| Object result = ois.readObject();
|
120 |
| |
121 |
12
| ois.close();
|
122 |
| |
123 |
12
| return result;
|
124 |
| } |
125 |
| catch (Exception ex) |
126 |
| { |
127 |
0
| throw new ApplicationRuntimeException(IoMessages.decodeFailure(ex), ex);
|
128 |
| } |
129 |
| } |
130 |
| |
131 |
78
| public void setResolver(ClassResolver resolver)
|
132 |
| { |
133 |
78
| _resolver = resolver;
|
134 |
| } |
135 |
| |
136 |
| } |