fix arrays
authorNiki Roo <niki@nikiroo.be>
Thu, 25 Apr 2019 19:16:34 +0000 (21:16 +0200)
committerNiki Roo <niki@nikiroo.be>
Thu, 25 Apr 2019 19:16:34 +0000 (21:16 +0200)
src/be/nikiroo/utils/serial/CustomSerializer.java
src/be/nikiroo/utils/serial/Importer.java
src/be/nikiroo/utils/serial/SerialUtils.java
src/be/nikiroo/utils/test_code/SerialTest.java

index 9c3ef8c425577bba30dd327fedf9e4d4348e80ac..e29e8cf93969e950a096ece9d48d14a9a81576be 100644 (file)
@@ -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('^'));
 
index 7a45ddfa0d51b54c43ce597d9fb8085c27bfb73b..6531cdba99f420c8778b3c2d990854b667b7d387 100644 (file)
@@ -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 {
index 6a628f3d706563f4140a33e949c39127522c7d2b..b083e3461e2b63d3c6d2a6e702d8cf60ef5d32b0 100644 (file)
@@ -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<Object> list = new ArrayList<Object>();
@@ -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 {
index 3a6211c1a145ae764caab2eb00123f45cb31294e..5ac53860eeb60818de4ddef3f37686181e4fe150 100644 (file)
@@ -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));