709c5903f6b4e9e9f62515bc601432e6aad200b7
1 package be
.nikiroo
.utils
.serial
;
3 import java
.io
.IOException
;
4 import java
.io
.NotSerializableException
;
5 import java
.io
.OutputStream
;
6 import java
.util
.HashMap
;
9 import be
.nikiroo
.utils
.StringUtils
;
12 * A simple class to serialise objects to {@link String}.
14 * This class does not support inner classes (it does support nested classes,
19 public class Exporter
{
20 private Map
<Integer
, Object
> map
;
21 private OutputStream out
;
24 * Create a new {@link Exporter}.
26 public Exporter(OutputStream out
) {
28 throw new NullPointerException(
29 "Cannot create an be.nikiroo.utils.serials.Exporter that will export to NULL");
33 map
= new HashMap
<Integer
, Object
>();
37 * Serialise the given object and add it to the list.
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).
44 * the object to serialise
45 * @return this (for easier appending of multiple values)
47 * @throws NotSerializableException
48 * if the object cannot be serialised (in this case, the
49 * {@link Exporter} can contain bad, most probably not
52 * in case of I/O error
54 public Exporter
append(Object o
) throws NotSerializableException
,
56 SerialUtils
.append(out
, o
, map
);
61 * Append the exported items in a serialised form into the given
62 * {@link OutputStream}.
65 * the {@link OutputStream}
67 * TRUE to have BASE64-coded content, FALSE to have raw content,
68 * NULL to let the system decide
70 * TRUE to zip the BASE64 output if the output is indeed in
71 * BASE64 format, FALSE not to
73 public void appendTo(OutputStream out
, Boolean b64
, boolean zip
) {
74 if (b64
== null && out
.length() < 128) {
78 if (b64
== null || b64
) {
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
);
86 } catch (IOException e
) {
87 throw new RuntimeException(
88 "Base64 conversion of data failed, maybe not enough memory?",