Merge commit '929409950e82914aa3cee323cfa7c5007585d2ea'
[nikiroo-utils.git] / src / be / nikiroo / utils / serial / Exporter.java
1 package be.nikiroo.utils.serial;
2
3 import java.io.IOException;
4 import java.io.NotSerializableException;
5 import java.io.OutputStream;
6 import java.util.HashMap;
7 import java.util.Map;
8
9 /**
10 * A simple class to serialise objects to {@link String}.
11 * <p>
12 * This class does not support inner classes (it does support nested classes,
13 * though).
14 *
15 * @author niki
16 */
17 public class Exporter {
18 private Map<Integer, Object> map;
19 private OutputStream out;
20
21 /**
22 * Create a new {@link Exporter}.
23 *
24 * @param out
25 * export the data to this stream
26 */
27 public Exporter(OutputStream out) {
28 if (out == null) {
29 throw new NullPointerException(
30 "Cannot create an be.nikiroo.utils.serials.Exporter that will export to NULL");
31 }
32
33 this.out = out;
34 map = new HashMap<Integer, Object>();
35 }
36
37 /**
38 * Serialise the given object and add it to the list.
39 * <p>
40 * <b>Important: </b>If the operation fails (with a
41 * {@link NotSerializableException}), the {@link Exporter} will be corrupted
42 * (will contain bad, most probably not importable data).
43 *
44 * @param o
45 * the object to serialise
46 * @return this (for easier appending of multiple values)
47 *
48 * @throws NotSerializableException
49 * if the object cannot be serialised (in this case, the
50 * {@link Exporter} can contain bad, most probably not
51 * importable data)
52 * @throws IOException
53 * in case of I/O error
54 */
55 public Exporter append(Object o) throws NotSerializableException,
56 IOException {
57 SerialUtils.append(out, o, map);
58 return this;
59 }
60 }