1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.teeda.core.render; |
17 | |
|
18 | |
import java.io.ByteArrayInputStream; |
19 | |
import java.io.ByteArrayOutputStream; |
20 | |
import java.io.InputStream; |
21 | |
import java.io.ObjectInputStream; |
22 | |
import java.io.ObjectOutputStream; |
23 | |
import java.util.zip.GZIPOutputStream; |
24 | |
|
25 | |
import javax.faces.internal.FacesConfigOptions; |
26 | |
|
27 | |
import org.seasar.framework.util.Base64Util; |
28 | |
import org.seasar.framework.util.InputStreamUtil; |
29 | |
import org.seasar.framework.util.OutputStreamUtil; |
30 | |
import org.seasar.teeda.core.util.GZIPInputStreamUtil; |
31 | |
import org.seasar.teeda.core.util.GZIPOutputStreamUtil; |
32 | |
import org.seasar.teeda.core.util.ObjectInputStreamUtil; |
33 | |
import org.seasar.teeda.core.util.ObjectOutputStreamUtil; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | 6 | public class Base64EncodeConverter implements EncodeConverter { |
39 | |
|
40 | |
public String getAsEncodeString(Object target) { |
41 | 5 | final ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
42 | 5 | GZIPOutputStream gzipOut = null; |
43 | 5 | ObjectOutputStream oos = null; |
44 | |
try { |
45 | 5 | if (isCompressRequested()) { |
46 | 1 | gzipOut = GZIPOutputStreamUtil.getOutputStream(bos); |
47 | 1 | oos = ObjectOutputStreamUtil.getOutputStream(gzipOut); |
48 | 1 | ObjectOutputStreamUtil.writeObject(oos, target); |
49 | 1 | GZIPOutputStreamUtil.finish(gzipOut); |
50 | |
} else { |
51 | 4 | oos = ObjectOutputStreamUtil.getOutputStream(bos); |
52 | 4 | ObjectOutputStreamUtil.writeObject(oos, target); |
53 | |
} |
54 | 5 | return Base64Util.encode(bos.toByteArray()); |
55 | |
} finally { |
56 | 5 | OutputStreamUtil.close(oos); |
57 | 5 | OutputStreamUtil.close(gzipOut); |
58 | 5 | OutputStreamUtil.close(bos); |
59 | |
} |
60 | |
} |
61 | |
|
62 | |
public Object getAsDecodeObject(String state) { |
63 | 5 | final byte[] bytes = Base64Util.decode(state); |
64 | 5 | final InputStream bis = new ByteArrayInputStream(bytes); |
65 | 5 | InputStream is = null; |
66 | 5 | ObjectInputStream ois = null; |
67 | |
try { |
68 | 5 | if (isCompressRequested()) { |
69 | 1 | is = GZIPInputStreamUtil.getInputStream(bis); |
70 | 1 | ois = ObjectInputStreamUtil.getInputStream(is); |
71 | |
} else { |
72 | 4 | ois = ObjectInputStreamUtil.getInputStream(bis); |
73 | |
} |
74 | 5 | return ObjectInputStreamUtil.readObject(ois); |
75 | |
} finally { |
76 | 5 | InputStreamUtil.close(ois); |
77 | 5 | InputStreamUtil.close(is); |
78 | 5 | InputStreamUtil.close(bis); |
79 | |
} |
80 | |
} |
81 | |
|
82 | |
protected boolean isCompressRequested() { |
83 | 8 | return FacesConfigOptions.getCompressState(); |
84 | |
} |
85 | |
|
86 | |
} |