X-Git-Url: https://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fserial%2FExporter.java;h=709c5903f6b4e9e9f62515bc601432e6aad200b7;hb=ecd81c088d8df94c47c85399afdab87bbd39afaf;hp=90939880f6f29e0e2ba1488f274db1daed932f7b;hpb=f157aed840bdd5b8ef04902d2326d916f71139da;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/serial/Exporter.java b/src/be/nikiroo/utils/serial/Exporter.java index 9093988..709c590 100644 --- a/src/be/nikiroo/utils/serial/Exporter.java +++ b/src/be/nikiroo/utils/serial/Exporter.java @@ -1,6 +1,8 @@ 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; @@ -16,14 +18,19 @@ import be.nikiroo.utils.StringUtils; */ public class Exporter { private Map map; - private StringBuilder builder; + private OutputStream out; /** * Create a new {@link Exporter}. */ - public Exporter() { + public Exporter(OutputStream out) { + if (out == null) { + throw new NullPointerException( + "Cannot create an be.nikiroo.utils.serials.Exporter that will export to NULL"); + } + + this.out = out; map = new HashMap(); - builder = new StringBuilder(); } /** @@ -41,48 +48,48 @@ public class Exporter { * 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 */ - public Exporter append(Object o) throws NotSerializableException { - SerialUtils.append(builder, o, map); + public Exporter append(Object o) throws NotSerializableException, + IOException { + SerialUtils.append(out, o, map); return this; } /** - * Clear the current content. - */ - public void clear() { - builder.setLength(0); - map.clear(); - } - - /** - * The exported items in a serialised form. + * Append the exported items in a serialised form into the given + * {@link OutputStream}. * + * @param out + * the {@link OutputStream} + * @param b64 + * TRUE to have BASE64-coded content, FALSE to have raw content, + * NULL to let the system decide * @param zip - * TRUE to have zipped content, FALSE to have raw content, NULL - * to let the system decide - * - * @return the items currently in this {@link Exporter} + * TRUE to zip the BASE64 output if the output is indeed in + * BASE64 format, FALSE not to */ - public String toString(Boolean zip) { - if (zip == null) { - zip = builder.length() > 128; + public void appendTo(OutputStream out, Boolean b64, boolean zip) { + if (b64 == null && out.length() < 128) { + b64 = false; } - if (zip) { - return "ZIP:" + StringUtils.zip64(builder.toString()); + if (b64 == null || b64) { + try { + String zipped = StringUtils.base64(out.toString(), zip); + if (b64 != null || zipped.length() < out.length() - 4) { + SerialUtils.write(out, zip ? "ZIP:" : "B64:"); + SerialUtils.write(out, zipped); + return; + } + } catch (IOException e) { + throw new RuntimeException( + "Base64 conversion of data failed, maybe not enough memory?", + e); + } } - return builder.toString(); - } - - /** - * The exported items in a serialised form (possibly zipped). - * - * @return the items currently in this {@link Exporter} - */ - @Override - public String toString() { - return toString(null); + out.append(out); } } \ No newline at end of file