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