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