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