X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fserial%2FExporter.java;h=2470bde4dace9f6dd0ca73f61373991a4cbd4283;hb=5584adbbbf5444c0039fed2b35dc7d5bb57b71b1;hp=8b522e9556054dea43572f6a5e4226f738e116b8;hpb=cd0c27d2e457ea19fcd9def879e1534a528292c2;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/serial/Exporter.java b/src/be/nikiroo/utils/serial/Exporter.java index 8b522e9..2470bde 100644 --- a/src/be/nikiroo/utils/serial/Exporter.java +++ b/src/be/nikiroo/utils/serial/Exporter.java @@ -1,11 +1,11 @@ package be.nikiroo.utils.serial; +import java.io.IOException; import java.io.NotSerializableException; +import java.io.OutputStream; import java.util.HashMap; import java.util.Map; -import be.nikiroo.utils.StringUtils; - /** * A simple class to serialise objects to {@link String}. *

@@ -16,43 +16,45 @@ import be.nikiroo.utils.StringUtils; */ public class Exporter { private Map map; - private StringBuilder builder; - - public Exporter() { - map = new HashMap(); - builder = new StringBuilder(); - } - - public Exporter append(Object o) throws NotSerializableException { - SerialUtils.append(builder, o, map); - return this; - } - - public void clear() { - builder.setLength(0); - map.clear(); - } + private OutputStream out; - // null = auto - public String toString(Boolean zip) { - if (zip == null) { - zip = builder.length() > 128; - } - - if (zip) { - return "ZIP:" + StringUtils.zip64(builder.toString()); + /** + * Create a new {@link Exporter}. + * + * @param out + * export the data to this stream + */ + public Exporter(OutputStream out) { + if (out == null) { + throw new NullPointerException( + "Cannot create an be.nikiroo.utils.serials.Exporter that will export to NULL"); } - return builder.toString(); + this.out = out; + map = new HashMap(); } /** - * The exported items in a serialised form. + * Serialise the given object and add it to the list. + *

+ * Important: If the operation fails (with a + * {@link NotSerializableException}), the {@link Exporter} will be corrupted + * (will contain bad, most probably not importable data). * - * @return the items currently in this {@link Exporter}. + * @param o + * the object to serialise + * @return this (for easier appending of multiple values) + * + * @throws NotSerializableException + * if the object cannot be serialised (in this case, the + * {@link Exporter} can contain bad, most probably not + * importable data) + * @throws IOException + * in case of I/O error */ - @Override - public String toString() { - return toString(null); + public Exporter append(Object o) throws NotSerializableException, + IOException { + SerialUtils.append(out, o, map); + return this; } } \ No newline at end of file