X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fserial%2FSerialUtils.java;h=c65463281ea51eca82729db53f40370942ab55f6;hb=23cf894daa02843da6e6f1b199eaaea29a739f81;hp=0dc4a35e30dc3ee08fe11bfb8cec46d2b2d5cee8;hpb=1d20e650389b5aaaaa5de4213cefefade582da5d;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/serial/SerialUtils.java b/src/be/nikiroo/utils/serial/SerialUtils.java index 0dc4a35..c654632 100644 --- a/src/be/nikiroo/utils/serial/SerialUtils.java +++ b/src/be/nikiroo/utils/serial/SerialUtils.java @@ -1,5 +1,6 @@ package be.nikiroo.utils.serial; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.NotSerializableException; @@ -18,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. @@ -62,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. @@ -92,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(); @@ -127,7 +132,6 @@ public class SerialUtils { // URL: customTypes.put("java.net.URL", new CustomSerializer() { - @Override protected void toStream(OutputStream out, Object value) throws IOException { @@ -246,7 +250,7 @@ public class SerialUtils { ctor = clazz.getDeclaredConstructor(classes .toArray(new Class[] {})); } catch (NoSuchMethodException nsme) { - // TODO: it seems e do not always need a parameter for each + // TODO: it seems we do not always need a parameter for each // level, so we currently try "ALL" levels or "FIRST" level // only -> we should check the actual rule and use it ctor = clazz.getDeclaredConstructor(classes.get(0)); @@ -370,7 +374,6 @@ public class SerialUtils { // setAccessible) } } - write(out, "\n}"); } /** @@ -400,9 +403,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); @@ -469,7 +472,14 @@ public class SerialUtils { // custom:TYPE_NAME:"content is String-encoded" String type = CustomSerializer.typeOf(encodedValue); if (customTypes.containsKey(type)) { - return customTypes.get(type).decode(encodedValue); + // TODO: we should start with a stream + InputStream streamEncodedValue = new ByteArrayInputStream( + encodedValue.getBytes("UTF-8")); + try { + return customTypes.get(type).decode(streamEncodedValue); + } finally { + streamEncodedValue.close(); + } } throw new IOException("Unknown custom type: " + type); } else if (encodedValue.equals("NULL") @@ -594,6 +604,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()) { @@ -606,7 +617,7 @@ public class SerialUtils { static void encodeString(OutputStream out, InputStream raw) throws IOException { out.write('\"'); - byte buffer[] = new byte[4069]; + byte buffer[] = new byte[4096]; for (int len = 0; (len = raw.read(buffer)) > 0;) { for (int i = 0; i < len; i++) { // TODO: not 100% correct, look up howto for UTF-8