VERSION + fix custom image
[nikiroo-utils.git] / src / be / nikiroo / utils / serial / SerialUtils.java
index 55d1f842e2597381d52984c48dbd614fc3518a90..a6a02a8e06205cb5996b0a94c1124056ce2cbf60 100644 (file)
@@ -70,11 +70,12 @@ public class SerialUtils {
                                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?
@@ -87,7 +88,6 @@ public class SerialUtils {
                                                                                .getMessage());
                                                        }
                                                }
-                                               write(out, "\r");
                                        }
                                } catch (ArrayIndexOutOfBoundsException e) {
                                        // Done.
@@ -174,7 +174,8 @@ public class SerialUtils {
                                                in.close();
                                        }
                                } finally {
-                                       encoded.close();
+                                       encoded.flush();
+                                       // Cannot close!
                                }
                        }
 
@@ -186,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());
                                }
@@ -373,8 +376,9 @@ public class SerialUtils {
                                e.printStackTrace(); // should not happen (see
                                                                                // setAccessible)
                        }
+
+                       write(out, "\n}");
                }
-               write(out, "\n}");
        }
 
        /**
@@ -513,7 +517,7 @@ public class SerialUtils {
                        if (e instanceof IOException) {
                                throw (IOException) e;
                        }
-                       throw new IOException(e.getMessage());
+                       throw new IOException(e.getMessage(), e);
                }
        }