VERSION + fix custom image
[nikiroo-utils.git] / src / be / nikiroo / utils / serial / SerialUtils.java
index 204d1d95a0ab22418a74fd540368fa183c38c460..a6a02a8e06205cb5996b0a94c1124056ce2cbf60 100644 (file)
@@ -19,9 +19,9 @@ import java.util.UnknownFormatConversionException;
 
 import be.nikiroo.utils.IOUtils;
 import be.nikiroo.utils.Image;
+import be.nikiroo.utils.StringUtils;
 import be.nikiroo.utils.streams.NextableInputStream;
 import be.nikiroo.utils.streams.NextableInputStreamStep;
-import be.nikiroo.utils.StringUtils;
 
 /**
  * Small class to help with serialisation.
@@ -63,17 +63,19 @@ public class SerialUtils {
                        @Override
                        protected void toStream(OutputStream out, Object value)
                                        throws IOException {
-                               // TODO: we use \n to separate, and b64 to un-\n -- but we could
-                               // use \\n ?
+
+                               // TODO: we use \n to separate, and b64 to un-\n
+                               // -- but we could use \\n ?
                                String type = value.getClass().getCanonicalName();
                                type = type.substring(0, type.length() - 2); // remove the []
 
                                write(out, type);
-                               write(out, "\r");
                                try {
                                        for (int i = 0; true; i++) {
                                                Object item = Array.get(value, i);
+
                                                // encode it normally if direct value
+                                               write(out, "\r");
                                                if (!SerialUtils.encode(out, item)) {
                                                        try {
                                                                // TODO: bad escaping?
@@ -86,7 +88,6 @@ public class SerialUtils {
                                                                                .getMessage());
                                                        }
                                                }
-                                               write(out, "\r");
                                        }
                                } catch (ArrayIndexOutOfBoundsException e) {
                                        // Done.
@@ -173,7 +174,8 @@ public class SerialUtils {
                                                in.close();
                                        }
                                } finally {
-                                       encoded.close();
+                                       encoded.flush();
+                                       // Cannot close!
                                }
                        }
 
@@ -185,7 +187,9 @@ public class SerialUtils {
                        @Override
                        protected Object fromStream(InputStream in) throws IOException {
                                try {
-                                       return new Image(in);
+                                       // Cannot close it!
+                                       InputStream decoded = StringUtils.unbase64(in, false);
+                                       return new Image(decoded);
                                } catch (IOException e) {
                                        throw new UnknownFormatConversionException(e.getMessage());
                                }
@@ -372,8 +376,9 @@ public class SerialUtils {
                                e.printStackTrace(); // should not happen (see
                                                                                // setAccessible)
                        }
+
+                       write(out, "\n}");
                }
-               write(out, "\n}");
        }
 
        /**
@@ -403,9 +408,9 @@ public class SerialUtils {
                } else if (value.getClass().getSimpleName().endsWith("[]")) {
                        // Simple name does support [] suffix and do not return NULL for
                        // inner anonymous classes
-                       return customTypes.get("[]").encode(out, value);
+                       customTypes.get("[]").encode(out, value);
                } else if (customTypes.containsKey(value.getClass().getCanonicalName())) {
-                       return customTypes.get(value.getClass().getCanonicalName())//
+                       customTypes.get(value.getClass().getCanonicalName())//
                                        .encode(out, value);
                } else if (value instanceof String) {
                        encodeString(out, (String) value);
@@ -512,7 +517,7 @@ public class SerialUtils {
                        if (e instanceof IOException) {
                                throw (IOException) e;
                        }
-                       throw new IOException(e.getMessage());
+                       throw new IOException(e.getMessage(), e);
                }
        }
 
@@ -604,6 +609,7 @@ public class SerialUtils {
 
        // aa bb -> "aa\tbb"
        static void encodeString(OutputStream out, String raw) throws IOException {
+               // TODO: not. efficient.
                out.write('\"');
                // TODO !! utf-8 required
                for (char car : raw.toCharArray()) {