X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fserial%2FSerialUtils.java;h=a6a02a8e06205cb5996b0a94c1124056ce2cbf60;hb=01e45aa43d68445a0fb22a63f4ba1ebf1cdf8435;hp=6a628f3d706563f4140a33e949c39127522c7d2b;hpb=564bbbdbc349805ff7fc1142facbedae87e5dc0e;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/serial/SerialUtils.java b/src/be/nikiroo/utils/serial/SerialUtils.java index 6a628f3..a6a02a8 100644 --- a/src/be/nikiroo/utils/serial/SerialUtils.java +++ b/src/be/nikiroo/utils/serial/SerialUtils.java @@ -19,9 +19,9 @@ import java.util.UnknownFormatConversionException; import be.nikiroo.utils.IOUtils; import be.nikiroo.utils.Image; -import be.nikiroo.utils.NextableInputStream; -import be.nikiroo.utils.NextableInputStreamStep; import be.nikiroo.utils.StringUtils; +import be.nikiroo.utils.streams.NextableInputStream; +import be.nikiroo.utils.streams.NextableInputStreamStep; /** * Small class to help with serialisation. @@ -63,27 +63,31 @@ 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, "\n"); 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: use ZIP: if not? - new Exporter(out).append(item); + // TODO: bad escaping? + write(out, "B64:"); + OutputStream bout = StringUtils.base64(out, + false, false); + new Exporter(bout).append(item); } catch (NotSerializableException e) { throw new UnknownFormatConversionException(e .getMessage()); } } - write(out, "\n"); } } catch (ArrayIndexOutOfBoundsException e) { // Done. @@ -93,7 +97,7 @@ public class SerialUtils { @Override protected Object fromStream(InputStream in) throws IOException { NextableInputStream stream = new NextableInputStream(in, - new NextableInputStreamStep('\n')); + new NextableInputStreamStep('\r')); try { List list = new ArrayList(); @@ -128,7 +132,6 @@ public class SerialUtils { // URL: customTypes.put("java.net.URL", new CustomSerializer() { - @Override protected void toStream(OutputStream out, Object value) throws IOException { @@ -171,7 +174,8 @@ public class SerialUtils { in.close(); } } finally { - encoded.close(); + encoded.flush(); + // Cannot close! } } @@ -183,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()); } @@ -370,8 +376,9 @@ public class SerialUtils { e.printStackTrace(); // should not happen (see // setAccessible) } + + write(out, "\n}"); } - write(out, "\n}"); } /** @@ -401,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); @@ -510,7 +517,7 @@ public class SerialUtils { if (e instanceof IOException) { throw (IOException) e; } - throw new IOException(e.getMessage()); + throw new IOException(e.getMessage(), e); } } @@ -602,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()) {