1 package be
.nikiroo
.utils
.serial
;
3 import java
.io
.IOException
;
4 import java
.io
.NotSerializableException
;
5 import java
.util
.HashMap
;
8 import be
.nikiroo
.utils
.StringUtils
;
11 * A simple class to serialise objects to {@link String}.
13 * This class does not support inner classes (it does support nested classes,
18 public class Exporter
{
19 private Map
<Integer
, Object
> map
;
20 private StringBuilder builder
;
23 * Create a new {@link Exporter}.
26 map
= new HashMap
<Integer
, Object
>();
27 builder
= new StringBuilder();
31 * Serialise the given object and add it to the list.
33 * <b>Important: </b>If the operation fails (with a
34 * {@link NotSerializableException}), the {@link Exporter} will be corrupted
35 * (will contain bad, most probably not importable data).
38 * the object to serialise
39 * @return this (for easier appending of multiple values)
41 * @throws NotSerializableException
42 * if the object cannot be serialised (in this case, the
43 * {@link Exporter} can contain bad, most probably not
46 public Exporter
append(Object o
) throws NotSerializableException
{
47 SerialUtils
.append(builder
, o
, map
);
52 * Clear the current content.
60 * Append the exported items in a serialised form into the given
61 * {@link StringBuilder}.
64 * the {@link StringBuilder}
66 * TRUE to have BASE64-coded content, FALSE to have raw content,
67 * NULL to let the system decide
69 * TRUE to zip the BASE64 output if the output is indeed in
70 * BASE64 format, FALSE not to
72 public void appendTo(StringBuilder toBuilder
, Boolean b64
, boolean zip
) {
73 if (b64
== null && builder
.length() < 128) {
77 if (b64
== null || b64
) {
79 String zipped
= StringUtils
.base64(builder
.toString(), zip
);
80 if (b64
!= null || zipped
.length() < builder
.length() - 4) {
81 toBuilder
.append(zip ?
"ZIP:" : "B64:");
82 toBuilder
.append(zipped
);
85 } catch (IOException e
) {
86 throw new RuntimeException(
87 "Base64 conversion of data failed, maybe not enough memory?",
92 toBuilder
.append(builder
);
96 * The exported items in a serialised form.
98 * @deprecated use {@link Exporter#toString(Boolean, boolean)} instead
101 * TRUE to have zipped (and BASE64-coded) content, FALSE to have
102 * raw content, NULL to let the system decide
104 * @return the items currently in this {@link Exporter}
107 public String
toString(Boolean zip
) {
108 return toString(zip
, zip
== null || zip
);
112 * The exported items in a serialised form.
115 * TRUE to have BASE64-coded content, FALSE to have raw content,
116 * NULL to let the system decide
118 * TRUE to zip the BASE64 output if the output is indeed in
119 * BASE64 format, FALSE not to
121 * @return the items currently in this {@link Exporter}
123 public String
toString(Boolean b64
, boolean zip
) {
124 StringBuilder toBuilder
= new StringBuilder();
125 appendTo(toBuilder
, b64
, zip
);
126 return toBuilder
.toString();
130 * The exported items in a serialised form (possibly BASE64-coded, possibly
133 * @return the items currently in this {@link Exporter}
136 public String
toString() {
137 return toString(null, true);