en cours, 2
authorNiki Roo <niki@nikiroo.be>
Wed, 24 Apr 2019 06:35:08 +0000 (08:35 +0200)
committerNiki Roo <niki@nikiroo.be>
Wed, 24 Apr 2019 06:35:08 +0000 (08:35 +0200)
src/be/nikiroo/utils/serial/CustomSerializer.java
src/be/nikiroo/utils/serial/Exporter.java
src/be/nikiroo/utils/serial/SerialUtils.java

index c5f79ff0331ae856596b4f2390b667d80245dbe4..80bac595a8a13068b1b45b10ece585c1566d5bb9 100644 (file)
@@ -14,28 +14,29 @@ public abstract class CustomSerializer {
        protected abstract String getType();
 
        /**
-        * Encode the object into the given {@link OutputStream} if possible (if
-        * supported).
+        * Encode the object into the given {@link OutputStream} if supported.
         * 
         * @param out
         *            the builder to append to
         * @param value
         *            the object to encode
         * 
-        * @return TRUE if success, FALSE if not (the content of the builder won't
-        *         be changed in case of failure)
+        * @return FALSE if the value is not supported, TRUE if the operation was
+        *         successful (if the value is supported by the operation was not
+        *         successful, you will get an {@link IOException})
         * 
         * @throws IOException
         *             in case of I/O error
         */
        public boolean encode(OutputStream out, Object value) throws IOException {
-               InputStream customString = toStream(out, value);
+               if (!isSupported(value)) {
+                       return false;
+               }
+
                SerialUtils.write(out, "custom^");
                SerialUtils.write(out, getType());
                SerialUtils.write(out, "^");
-               if (!SerialUtils.encode(out, customString)) {
-                       return false;
-               }
+               toStream(out, value);
 
                return true;
        }
index dc96d973226fbcaaf09331ae8f0b6ac3d68ada98..709c5903f6b4e9e9f62515bc601432e6aad200b7 100644 (file)
@@ -2,6 +2,7 @@ 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;
 
@@ -17,14 +18,19 @@ import be.nikiroo.utils.StringUtils;
  */
 public class Exporter {
        private Map<Integer, Object> 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<Integer, Object>();
-               builder = new StringBuilder();
        }
 
        /**
@@ -42,26 +48,21 @@ 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();
-       }
-
        /**
         * Append the exported items in a serialised form into the given
-        * {@link StringBuilder}.
+        * {@link OutputStream}.
         * 
-        * @param toBuilder
-        *            the {@link StringBuilder}
+        * @param out
+        *            the {@link OutputStream}
         * @param b64
         *            TRUE to have BASE64-coded content, FALSE to have raw content,
         *            NULL to let the system decide
@@ -69,17 +70,17 @@ public class Exporter {
         *            TRUE to zip the BASE64 output if the output is indeed in
         *            BASE64 format, FALSE not to
         */
-       public void appendTo(StringBuilder toBuilder, Boolean b64, boolean zip) {
-               if (b64 == null && builder.length() < 128) {
+       public void appendTo(OutputStream out, Boolean b64, boolean zip) {
+               if (b64 == null && out.length() < 128) {
                        b64 = false;
                }
 
                if (b64 == null || b64) {
                        try {
-                               String zipped = StringUtils.base64(builder.toString(), zip);
-                               if (b64 != null || zipped.length() < builder.length() - 4) {
-                                       toBuilder.append(zip ? "ZIP:" : "B64:");
-                                       toBuilder.append(zipped);
+                               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) {
@@ -89,51 +90,6 @@ public class Exporter {
                        }
                }
 
-               toBuilder.append(builder);
-       }
-
-       /**
-        * The exported items in a serialised form.
-        * 
-        * @deprecated use {@link Exporter#toString(Boolean, boolean)} instead
-        * 
-        * @param zip
-        *            TRUE to have zipped (and BASE64-coded) content, FALSE to have
-        *            raw content, NULL to let the system decide
-        * 
-        * @return the items currently in this {@link Exporter}
-        */
-       @Deprecated
-       public String toString(Boolean zip) {
-               return toString(zip, zip == null || zip);
-       }
-
-       /**
-        * The exported items in a serialised form.
-        * 
-        * @param b64
-        *            TRUE to have BASE64-coded content, FALSE to have raw content,
-        *            NULL to let the system decide
-        * @param zip
-        *            TRUE to zip the BASE64 output if the output is indeed in
-        *            BASE64 format, FALSE not to
-        * 
-        * @return the items currently in this {@link Exporter}
-        */
-       public String toString(Boolean b64, boolean zip) {
-               StringBuilder toBuilder = new StringBuilder();
-               appendTo(toBuilder, b64, zip);
-               return toBuilder.toString();
-       }
-
-       /**
-        * The exported items in a serialised form (possibly BASE64-coded, possibly
-        * zipped).
-        * 
-        * @return the items currently in this {@link Exporter}
-        */
-       @Override
-       public String toString() {
-               return toString(null, true);
+               out.append(out);
        }
 }
\ No newline at end of file
index e2d8decea64113bfe76ac0962196e325a2fcf2d8..e88337b0a7f68ac03fd10ce2696909bb0cf8a187 100644 (file)
@@ -56,39 +56,42 @@ public class SerialUtils {
                // Array types:
                customTypes.put("[]", new CustomSerializer() {
                        @Override
-                       protected String toString(Object value) {
+                       protected void toStream(OutputStream out, Object value)
+                                       throws IOException {
                                String type = value.getClass().getCanonicalName();
                                type = type.substring(0, type.length() - 2); // remove the []
 
-                               StringBuilder builder = new StringBuilder();
-                               builder.append(type).append("\n");
+                               write(out,type);
+                               write(out,"\n");
                                try {
                                        for (int i = 0; true; i++) {
                                                Object item = Array.get(value, i);
                                                // encode it normally if direct value
-                                               if (!SerialUtils.encode(builder, item)) {
+                                               if (!SerialUtils.encode(out, item)) {
                                                        try {
-                                                               // use ZIP: if not
-                                                               new Exporter().append(item).appendTo(builder,
-                                                                               true, false);
+                                                               // TODO: use ZIP: if not?
+                                                               new Exporter(out).append(item);
                                                        } catch (NotSerializableException e) {
                                                                throw new UnknownFormatConversionException(e
                                                                                .getMessage());
                                                        }
                                                }
-                                               builder.append("\n");
+                                               write(out,"\n");
                                        }
                                } catch (ArrayIndexOutOfBoundsException e) {
                                        // Done.
                                }
-
-                               return builder.toString();
                        }
 
                        @Override
                        protected String getType() {
                                return "[]";
                        }
+                       
+                       @Override
+                       protected Object fromStream(InputStream in) throws IOException {
+                               return null;
+                       }
 
                        @Override
                        protected Object fromString(String content) throws IOException {