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 | 0 | 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 | 0 | return PREFIX; |
50 | |
} |
51 | |
|
52 | |
public Class getDataClass() |
53 | |
{ |
54 | 0 | return Serializable.class; |
55 | |
} |
56 | |
|
57 | |
public String squeeze(DataSqueezer squeezer, Object data) |
58 | |
{ |
59 | |
try |
60 | |
{ |
61 | 0 | ByteArrayOutputStream bosPlain = new ByteArrayOutputStream(); |
62 | 0 | ByteArrayOutputStream bosCompressed = new ByteArrayOutputStream(); |
63 | |
|
64 | 0 | GZIPOutputStream gos = new GZIPOutputStream(bosCompressed); |
65 | |
|
66 | 0 | TeeOutputStream tos = new TeeOutputStream(bosPlain, gos); |
67 | |
|
68 | 0 | ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(tos)); |
69 | |
|
70 | 0 | oos.writeObject(data); |
71 | |
|
72 | 0 | oos.close(); |
73 | |
|
74 | 0 | boolean useCompressed = bosCompressed.size() < bosPlain.size(); |
75 | |
|
76 | 0 | byte[] byteArray = useCompressed ? bosCompressed.toByteArray() : bosPlain.toByteArray(); |
77 | |
|
78 | 0 | byte[] encoded = Base64.encodeBase64(byteArray); |
79 | |
|
80 | 0 | String prefix = Character.toString(useCompressed ? GZIP_BYTESTREAM_PREFIX : BYTESTREAM_PREFIX); |
81 | |
|
82 | 0 | 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 | 0 | char prefix = encoded.charAt(0); |
93 | |
|
94 | |
try |
95 | |
{ |
96 | |
|
97 | |
|
98 | 0 | byte[] mimeData = encoded.substring(1).getBytes(); |
99 | |
|
100 | 0 | byte[] decoded = Base64.decodeBase64(mimeData); |
101 | |
|
102 | 0 | InputStream is = new ByteArrayInputStream(decoded); |
103 | |
|
104 | 0 | if (prefix == GZIP_BYTESTREAM_PREFIX) |
105 | 0 | is = new GZIPInputStream(is); |
106 | |
|
107 | 0 | is = new BufferedInputStream(is); |
108 | |
|
109 | 0 | ObjectInputStream ois = new ResolvingObjectInputStream(_resolver, is); |
110 | |
|
111 | 0 | Object result = ois.readObject(); |
112 | |
|
113 | 0 | ois.close(); |
114 | |
|
115 | 0 | 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 | 0 | _resolver = resolver; |
126 | 0 | } |
127 | |
|
128 | |
} |