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 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| public class SerializableAdaptor implements SqueezeAdaptor |
48 |
| { |
49 |
| private ClassResolver _resolver; |
50 |
| |
51 |
| private static final char BYTESTREAM_PREFIX = 'O'; |
52 |
| |
53 |
| private static final char GZIP_BYTESTREAM_PREFIX = 'Z'; |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| private static final String PREFIX = "OZ"; |
59 |
| |
60 |
37
| public String getPrefix()
|
61 |
| { |
62 |
37
| return PREFIX;
|
63 |
| } |
64 |
| |
65 |
37
| public Class getDataClass()
|
66 |
| { |
67 |
37
| return Serializable.class;
|
68 |
| } |
69 |
| |
70 |
17
| public String squeeze(DataSqueezer squeezer, Object data)
|
71 |
| { |
72 |
17
| try
|
73 |
| { |
74 |
17
| ByteArrayOutputStream bosPlain = new ByteArrayOutputStream();
|
75 |
17
| ByteArrayOutputStream bosCompressed = new ByteArrayOutputStream();
|
76 |
| |
77 |
17
| GZIPOutputStream gos = new GZIPOutputStream(bosCompressed);
|
78 |
| |
79 |
17
| TeeOutputStream tos = new TeeOutputStream(bosPlain, gos);
|
80 |
| |
81 |
17
| ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(tos));
|
82 |
| |
83 |
17
| oos.writeObject(data);
|
84 |
| |
85 |
17
| oos.close();
|
86 |
| |
87 |
17
| boolean useCompressed = bosCompressed.size() < bosPlain.size();
|
88 |
| |
89 |
17
| byte[] byteArray = useCompressed ? bosCompressed.toByteArray() : bosPlain.toByteArray();
|
90 |
| |
91 |
17
| byte[] encoded = Base64.encodeBase64(byteArray);
|
92 |
| |
93 |
17
| String prefix = Character.toString(useCompressed ? GZIP_BYTESTREAM_PREFIX
|
94 |
| : BYTESTREAM_PREFIX); |
95 |
| |
96 |
17
| return prefix + new String(encoded);
|
97 |
| } |
98 |
| catch (Exception ex) |
99 |
| { |
100 |
0
| throw new ApplicationRuntimeException(IoMessages.encodeFailure(data, ex), ex);
|
101 |
| } |
102 |
| } |
103 |
| |
104 |
5
| public Object unsqueeze(DataSqueezer squeezer, String encoded)
|
105 |
| { |
106 |
5
| char prefix = encoded.charAt(0);
|
107 |
| |
108 |
5
| try
|
109 |
| { |
110 |
| |
111 |
| |
112 |
5
| byte[] mimeData = encoded.substring(1).getBytes();
|
113 |
| |
114 |
5
| byte[] decoded = Base64.decodeBase64(mimeData);
|
115 |
| |
116 |
5
| InputStream is = new ByteArrayInputStream(decoded);
|
117 |
| |
118 |
5
| if (prefix == GZIP_BYTESTREAM_PREFIX)
|
119 |
1
| is = new GZIPInputStream(is);
|
120 |
| |
121 |
5
| is = new BufferedInputStream(is);
|
122 |
| |
123 |
5
| ObjectInputStream ois = new ResolvingObjectInputStream(_resolver, is);
|
124 |
| |
125 |
5
| Object result = ois.readObject();
|
126 |
| |
127 |
5
| ois.close();
|
128 |
| |
129 |
5
| return result;
|
130 |
| } |
131 |
| catch (Exception ex) |
132 |
| { |
133 |
0
| throw new ApplicationRuntimeException(IoMessages.decodeFailure(ex), ex);
|
134 |
| } |
135 |
| } |
136 |
| |
137 |
37
| public void setResolver(ClassResolver resolver)
|
138 |
| { |
139 |
37
| _resolver = resolver;
|
140 |
| } |
141 |
| |
142 |
| } |