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