6325484eda7ae32ba3a1ebd9055ab8849d8deacd
[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 public Exporter(OutputStream out) {
25 if (out == null) {
26 throw new NullPointerException(
27 "Cannot create an be.nikiroo.utils.serials.Exporter that will export to NULL");
28 }
29
30 this.out = out;
31 map = new HashMap<Integer, Object>();
32 }
33
34 /**
35 * Serialise the given object and add it to the list.
36 * <p>
37 * <b>Important: </b>If the operation fails (with a
38 * {@link NotSerializableException}), the {@link Exporter} will be corrupted
39 * (will contain bad, most probably not importable data).
40 *
41 * @param o
42 * the object to serialise
43 * @return this (for easier appending of multiple values)
44 *
45 * @throws NotSerializableException
46 * if the object cannot be serialised (in this case, the
47 * {@link Exporter} can contain bad, most probably not
48 * importable data)
49 * @throws IOException
50 * in case of I/O error
51 */
52 public Exporter append(Object o) throws NotSerializableException,
53 IOException {
54 SerialUtils.append(out, o, map);
55 return this;
56 }
57 }