Merge commit '77d3a60869e7a780c6ae069e51530e1eacece5e2'
[fanfix.git] / src / be / nikiroo / utils / serial / Exporter.java
index 90939880f6f29e0e2ba1488f274db1daed932f7b..2470bde4dace9f6dd0ca73f61373991a4cbd4283 100644 (file)
@@ -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}.
  * <p>
@@ -16,14 +16,22 @@ import be.nikiroo.utils.StringUtils;
  */
 public class Exporter {
        private Map<Integer, Object> map;
-       private StringBuilder builder;
+       private OutputStream out;
 
        /**
         * Create a new {@link Exporter}.
+        * 
+        * @param out
+        *            export the data to this stream
         */
-       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<Integer, Object>();
-               builder = new StringBuilder();
        }
 
        /**
@@ -41,48 +49,12 @@ 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.
-        * 
-        * @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}
-        */
-       public String toString(Boolean zip) {
-               if (zip == null) {
-                       zip = builder.length() > 128;
-               }
-
-               if (zip) {
-                       return "ZIP:" + StringUtils.zip64(builder.toString());
-               }
-
-               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);
-       }
 }
\ No newline at end of file