Commit | Line | Data |
---|---|---|
db31c358 NR |
1 | package be.nikiroo.utils.serial; |
2 | ||
a359464f | 3 | import java.io.IOException; |
db31c358 | 4 | import java.io.NotSerializableException; |
143a64f4 | 5 | import java.io.OutputStream; |
db31c358 NR |
6 | import java.util.HashMap; |
7 | import java.util.Map; | |
8 | ||
9 | import be.nikiroo.utils.StringUtils; | |
10 | ||
11 | /** | |
12 | * A simple class to serialise objects to {@link String}. | |
13 | * <p> | |
14 | * This class does not support inner classes (it does support nested classes, | |
15 | * though). | |
16 | * | |
17 | * @author niki | |
18 | */ | |
19 | public class Exporter { | |
20 | private Map<Integer, Object> map; | |
143a64f4 | 21 | private OutputStream out; |
db31c358 | 22 | |
f157aed8 NR |
23 | /** |
24 | * Create a new {@link Exporter}. | |
25 | */ | |
143a64f4 NR |
26 | public Exporter(OutputStream out) { |
27 | if (out == null) { | |
28 | throw new NullPointerException( | |
29 | "Cannot create an be.nikiroo.utils.serials.Exporter that will export to NULL"); | |
30 | } | |
31 | ||
32 | this.out = out; | |
db31c358 | 33 | map = new HashMap<Integer, Object>(); |
db31c358 NR |
34 | } |
35 | ||
f157aed8 NR |
36 | /** |
37 | * Serialise the given object and add it to the list. | |
38 | * <p> | |
39 | * <b>Important: </b>If the operation fails (with a | |
40 | * {@link NotSerializableException}), the {@link Exporter} will be corrupted | |
41 | * (will contain bad, most probably not importable data). | |
42 | * | |
43 | * @param o | |
44 | * the object to serialise | |
45 | * @return this (for easier appending of multiple values) | |
46 | * | |
47 | * @throws NotSerializableException | |
48 | * if the object cannot be serialised (in this case, the | |
49 | * {@link Exporter} can contain bad, most probably not | |
50 | * importable data) | |
143a64f4 NR |
51 | * @throws IOException |
52 | * in case of I/O error | |
f157aed8 | 53 | */ |
143a64f4 NR |
54 | public Exporter append(Object o) throws NotSerializableException, |
55 | IOException { | |
56 | SerialUtils.append(out, o, map); | |
db31c358 NR |
57 | return this; |
58 | } | |
59 | ||
a359464f NR |
60 | /** |
61 | * Append the exported items in a serialised form into the given | |
143a64f4 | 62 | * {@link OutputStream}. |
a359464f | 63 | * |
143a64f4 NR |
64 | * @param out |
65 | * the {@link OutputStream} | |
a359464f NR |
66 | * @param b64 |
67 | * TRUE to have BASE64-coded content, FALSE to have raw content, | |
68 | * NULL to let the system decide | |
69 | * @param zip | |
70 | * TRUE to zip the BASE64 output if the output is indeed in | |
71 | * BASE64 format, FALSE not to | |
72 | */ | |
143a64f4 NR |
73 | public void appendTo(OutputStream out, Boolean b64, boolean zip) { |
74 | if (b64 == null && out.length() < 128) { | |
a359464f NR |
75 | b64 = false; |
76 | } | |
77 | ||
3f277541 | 78 | if (b64 == null || b64) { |
a359464f | 79 | try { |
143a64f4 NR |
80 | String zipped = StringUtils.base64(out.toString(), zip); |
81 | if (b64 != null || zipped.length() < out.length() - 4) { | |
82 | SerialUtils.write(out, zip ? "ZIP:" : "B64:"); | |
83 | SerialUtils.write(out, zipped); | |
a359464f NR |
84 | return; |
85 | } | |
86 | } catch (IOException e) { | |
87 | throw new RuntimeException( | |
88 | "Base64 conversion of data failed, maybe not enough memory?", | |
89 | e); | |
90 | } | |
91 | } | |
92 | ||
143a64f4 | 93 | out.append(out); |
db31c358 NR |
94 | } |
95 | } |