Commit | Line | Data |
---|---|---|
db31c358 NR |
1 | package be.nikiroo.utils.serial; |
2 | ||
a359464f | 3 | import java.io.IOException; |
db31c358 | 4 | import java.io.NotSerializableException; |
08f80ac5 | 5 | import java.io.OutputStream; |
db31c358 NR |
6 | import java.util.HashMap; |
7 | import java.util.Map; | |
8 | ||
db31c358 NR |
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; | |
08f80ac5 | 19 | private OutputStream out; |
db31c358 | 20 | |
f157aed8 NR |
21 | /** |
22 | * Create a new {@link Exporter}. | |
08f80ac5 NR |
23 | * |
24 | * @param out | |
25 | * export the data to this stream | |
f157aed8 | 26 | */ |
08f80ac5 NR |
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; | |
db31c358 | 34 | map = new HashMap<Integer, Object>(); |
db31c358 NR |
35 | } |
36 | ||
f157aed8 NR |
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) | |
08f80ac5 NR |
52 | * @throws IOException |
53 | * in case of I/O error | |
f157aed8 | 54 | */ |
08f80ac5 NR |
55 | public Exporter append(Object o) throws NotSerializableException, |
56 | IOException { | |
57 | SerialUtils.append(out, o, map); | |
db31c358 NR |
58 | return this; |
59 | } | |
db31c358 | 60 | } |