From 4d31956549a05df6dca42d58a1150a348a58dcd1 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Thu, 25 Apr 2019 21:16:34 +0200 Subject: [PATCH] fix arrays --- .../utils/serial/CustomSerializer.java | 1 - src/be/nikiroo/utils/serial/Importer.java | 1 + src/be/nikiroo/utils/serial/SerialUtils.java | 14 ++++++++------ .../nikiroo/utils/test_code/SerialTest.java | 19 ++++++++++++------- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/be/nikiroo/utils/serial/CustomSerializer.java b/src/be/nikiroo/utils/serial/CustomSerializer.java index 9c3ef8c..e29e8cf 100644 --- a/src/be/nikiroo/utils/serial/CustomSerializer.java +++ b/src/be/nikiroo/utils/serial/CustomSerializer.java @@ -44,7 +44,6 @@ public abstract class CustomSerializer { public Object decode(InputStream in) throws IOException { // TODO: manage ENTER - // TODO read and skip "custom^......^": next(), next(), nextAll() ? NextableInputStream stream = new NextableInputStream(in, new NextableInputStreamStep('^')); diff --git a/src/be/nikiroo/utils/serial/Importer.java b/src/be/nikiroo/utils/serial/Importer.java index 7a45ddf..6531cdb 100644 --- a/src/be/nikiroo/utils/serial/Importer.java +++ b/src/be/nikiroo/utils/serial/Importer.java @@ -84,6 +84,7 @@ public class Importer { boolean b64 = stream.startsWiths("B64:"); if (zip || b64) { + stream.skip("XXX:".length()); InputStream decoded = StringUtils.unbase64(stream.open(), zip); try { diff --git a/src/be/nikiroo/utils/serial/SerialUtils.java b/src/be/nikiroo/utils/serial/SerialUtils.java index 6a628f3..b083e34 100644 --- a/src/be/nikiroo/utils/serial/SerialUtils.java +++ b/src/be/nikiroo/utils/serial/SerialUtils.java @@ -69,21 +69,24 @@ public class SerialUtils { type = type.substring(0, type.length() - 2); // remove the [] write(out, type); - write(out, "\n"); + write(out, "\r"); try { for (int i = 0; true; i++) { Object item = Array.get(value, i); // encode it normally if direct value 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"); + write(out, "\r"); } } catch (ArrayIndexOutOfBoundsException e) { // Done. @@ -93,7 +96,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 +131,6 @@ public class SerialUtils { // URL: customTypes.put("java.net.URL", new CustomSerializer() { - @Override protected void toStream(OutputStream out, Object value) throws IOException { diff --git a/src/be/nikiroo/utils/test_code/SerialTest.java b/src/be/nikiroo/utils/test_code/SerialTest.java index 3a6211c..5ac5386 100644 --- a/src/be/nikiroo/utils/test_code/SerialTest.java +++ b/src/be/nikiroo/utils/test_code/SerialTest.java @@ -22,23 +22,28 @@ class SerialTest extends TestLauncher { } private void encodeRecodeTest(TestCase test, Object data) throws Exception { - byte[] encoded = toBytes(data); + byte[] encoded = toBytes(data,true); Object redata = fromBytes(encoded); - byte[] reencoded = toBytes(redata); + byte[] reencoded = toBytes(redata,true); test.assertEquals("Different data after encode/decode/encode", true, Arrays.equals(encoded, reencoded)); } // try to remove pointer addresses - private byte[] toBytes(Object data) throws NotSerializableException, + private byte[] toBytes(Object data, boolean clearRefs) throws NotSerializableException, IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); new Exporter(out).append(data); out.flush(); + + if(clearRefs){ String tmp = new String(out.toByteArray(), "UTF-8"); tmp = tmp.replaceAll("@[0-9]*", "@REF"); return tmp.getBytes("UTF-8"); + } + + return out.toByteArray(); } private Object fromBytes(byte[] data) throws NoSuchFieldException, @@ -86,16 +91,16 @@ class SerialTest extends TestLauncher { Data[] data = new Data[] { new Data() { @SuppressWarnings("unused") int value = 42; - } }; + }}; - byte[] encoded = toBytes(data); + byte[] encoded = toBytes(data,false); Object redata = fromBytes(encoded); // Comparing the 2 arrays won't be useful, because the @REFs // will be ZIP-encoded; so we parse and re-encode each object - byte[] encoded1 = toBytes(data[0]); - byte[] reencoded1 = toBytes(((Data[])redata)[0]); + byte[] encoded1 = toBytes(data[0],true); + byte[] reencoded1 = toBytes(((Data[])redata)[0],true); assertEquals("Different data after encode/decode/encode", true, Arrays.equals(encoded1, reencoded1)); -- 2.27.0