1 package be
.nikiroo
.utils
.serial
;
3 import java
.io
.NotSerializableException
;
4 import java
.util
.HashMap
;
7 import be
.nikiroo
.utils
.StringUtils
;
10 * A simple class to serialise objects to {@link String}.
12 * This class does not support inner classes (it does support nested classes,
17 public class Exporter
{
18 private Map
<Integer
, Object
> map
;
19 private StringBuilder builder
;
22 * Create a new {@link Exporter}.
25 map
= new HashMap
<Integer
, Object
>();
26 builder
= new StringBuilder();
30 * Serialise the given object and add it to the list.
32 * <b>Important: </b>If the operation fails (with a
33 * {@link NotSerializableException}), the {@link Exporter} will be corrupted
34 * (will contain bad, most probably not importable data).
37 * the object to serialise
38 * @return this (for easier appending of multiple values)
40 * @throws NotSerializableException
41 * if the object cannot be serialised (in this case, the
42 * {@link Exporter} can contain bad, most probably not
45 public Exporter
append(Object o
) throws NotSerializableException
{
46 SerialUtils
.append(builder
, o
, map
);
51 * Clear the current content.
59 * The exported items in a serialised form.
62 * TRUE to have zipped content, FALSE to have raw content, NULL
63 * to let the system decide
65 * @return the items currently in this {@link Exporter}
67 public String
toString(Boolean zip
) {
69 zip
= builder
.length() > 128;
73 return "ZIP:" + StringUtils
.zip64(builder
.toString());
76 return builder
.toString();
80 * The exported items in a serialised form (possibly zipped).
82 * @return the items currently in this {@link Exporter}
85 public String
toString() {
86 return toString(null);