git://git.nikiroo.be
/
nikiroo-utils.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
6e56dcb
)
fix arrays
author
Niki Roo
<niki@nikiroo.be>
Thu, 25 Apr 2019 19:16:34 +0000
(21:16 +0200)
committer
Niki Roo
<niki@nikiroo.be>
Thu, 25 Apr 2019 19:16:34 +0000
(21:16 +0200)
src/be/nikiroo/utils/serial/CustomSerializer.java
patch
|
blob
|
blame
|
history
src/be/nikiroo/utils/serial/Importer.java
patch
|
blob
|
blame
|
history
src/be/nikiroo/utils/serial/SerialUtils.java
patch
|
blob
|
blame
|
history
src/be/nikiroo/utils/test_code/SerialTest.java
patch
|
blob
|
blame
|
history
diff --git
a/src/be/nikiroo/utils/serial/CustomSerializer.java
b/src/be/nikiroo/utils/serial/CustomSerializer.java
index 9c3ef8c425577bba30dd327fedf9e4d4348e80ac..e29e8cf93969e950a096ece9d48d14a9a81576be 100644
(file)
--- 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
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('^'));
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 7a45ddfa0d51b54c43ce597d9fb8085c27bfb73b..6531cdba99f420c8778b3c2d990854b667b7d387 100644
(file)
--- 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) {
boolean b64 = stream.startsWiths("B64:");
if (zip || b64) {
+ stream.skip("XXX:".length());
InputStream decoded = StringUtils.unbase64(stream.open(),
zip);
try {
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 6a628f3d706563f4140a33e949c39127522c7d2b..b083e3461e2b63d3c6d2a6e702d8cf60ef5d32b0 100644
(file)
--- 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);
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 {
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());
}
}
} catch (NotSerializableException e) {
throw new UnknownFormatConversionException(e
.getMessage());
}
}
- write(out, "\
n
");
+ write(out, "\
r
");
}
} catch (ArrayIndexOutOfBoundsException e) {
// Done.
}
} catch (ArrayIndexOutOfBoundsException e) {
// Done.
@@
-93,7
+96,7
@@
public class SerialUtils {
@Override
protected Object fromStream(InputStream in) throws IOException {
NextableInputStream stream = new NextableInputStream(in,
@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>();
try {
List<Object> list = new ArrayList<Object>();
@@
-128,7
+131,6
@@
public class SerialUtils {
// URL:
customTypes.put("java.net.URL", new CustomSerializer() {
// URL:
customTypes.put("java.net.URL", new CustomSerializer() {
-
@Override
protected void toStream(OutputStream out, Object value)
throws IOException {
@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 3a6211c1a145ae764caab2eb00123f45cb31294e..5ac53860eeb60818de4ddef3f37686181e4fe150 100644
(file)
--- 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 {
}
private void encodeRecodeTest(TestCase test, Object data) throws Exception {
- byte[] encoded = toBytes(data);
+ byte[] encoded = toBytes(data
,true
);
Object redata = fromBytes(encoded);
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
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();
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");
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,
}
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;
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
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));
assertEquals("Different data after encode/decode/encode", true,
Arrays.equals(encoded1, reencoded1));