From bc156cfd2b6a3bf5abc8b8a03324a8b57752b02f Mon Sep 17 00:00:00 2001 From: "m.ross" Date: Sat, 17 Nov 2018 13:30:03 -0500 Subject: [PATCH 1/5] BitSet serializer --- .../serialization/FSTConfiguration.java | 2 ++ .../serializers/FSTBitSetSerializer.java | 31 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 src/main/java/org/nustaq/serialization/serializers/FSTBitSetSerializer.java diff --git a/src/main/java/org/nustaq/serialization/FSTConfiguration.java b/src/main/java/org/nustaq/serialization/FSTConfiguration.java index 9e8a9e35..82ffcc3c 100644 --- a/src/main/java/org/nustaq/serialization/FSTConfiguration.java +++ b/src/main/java/org/nustaq/serialization/FSTConfiguration.java @@ -501,6 +501,8 @@ protected static FSTConfiguration initDefaultFstConfigurationInternal(FSTConfigu reg.putSerializer(ConcurrentHashMap.class, new FSTMapSerializer(), true); reg.putSerializer(FSTStruct.class, new FSTStructSerializer(), true); + reg.putSerializer(BitSet.class, new FSTBitSetSerializer(),true); + // serializers for classes failing in fst JDK emulation (e.g. Android<=>JDK) reg.putSerializer(BigInteger.class, new FSTBigIntegerSerializer(), true); diff --git a/src/main/java/org/nustaq/serialization/serializers/FSTBitSetSerializer.java b/src/main/java/org/nustaq/serialization/serializers/FSTBitSetSerializer.java new file mode 100644 index 00000000..661e1029 --- /dev/null +++ b/src/main/java/org/nustaq/serialization/serializers/FSTBitSetSerializer.java @@ -0,0 +1,31 @@ +package org.nustaq.serialization.serializers; + +import org.nustaq.serialization.FSTBasicObjectSerializer; +import org.nustaq.serialization.FSTClazzInfo; +import org.nustaq.serialization.FSTObjectInput; +import org.nustaq.serialization.FSTObjectOutput; + +import java.io.IOException; +import java.util.BitSet; + +public class FSTBitSetSerializer extends FSTBasicObjectSerializer { + @Override + public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo, + FSTClazzInfo.FSTFieldInfo referencedBy, int streamPosition) throws IOException { + out.writeObject(((BitSet)toWrite).toLongArray()); + } + + @Override + public boolean alwaysCopy(){ + return true; + } + + @Override + public Object instantiate(Class objectClass, FSTObjectInput in, FSTClazzInfo serializationInfo, + FSTClazzInfo.FSTFieldInfo referencee, int streamPosition) throws Exception { + long[] l = (long[])in.readObject(); + Object res = BitSet.valueOf(l); + return res; + } + +} \ No newline at end of file From df1e5d21ef028f193255bfb5fd6b1047e4266928 Mon Sep 17 00:00:00 2001 From: "m.ross" Date: Sat, 17 Nov 2018 13:32:10 -0500 Subject: [PATCH 2/5] Timestamp serializer --- .../serialization/FSTConfiguration.java | 3 ++ .../serializers/FSTTimestampSerializer.java | 31 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 src/main/java/org/nustaq/serialization/serializers/FSTTimestampSerializer.java diff --git a/src/main/java/org/nustaq/serialization/FSTConfiguration.java b/src/main/java/org/nustaq/serialization/FSTConfiguration.java index 82ffcc3c..499f5864 100644 --- a/src/main/java/org/nustaq/serialization/FSTConfiguration.java +++ b/src/main/java/org/nustaq/serialization/FSTConfiguration.java @@ -39,6 +39,7 @@ import java.math.BigInteger; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.*; import java.util.List; @@ -502,6 +503,7 @@ protected static FSTConfiguration initDefaultFstConfigurationInternal(FSTConfigu reg.putSerializer(FSTStruct.class, new FSTStructSerializer(), true); reg.putSerializer(BitSet.class, new FSTBitSetSerializer(),true); + reg.putSerializer(Timestamp.class, new FSTTimestampSerializer(),true); // serializers for classes failing in fst JDK emulation (e.g. Android<=>JDK) reg.putSerializer(BigInteger.class, new FSTBigIntegerSerializer(), true); @@ -832,6 +834,7 @@ void addDefaultClazzes() { classRegistry.registerClass(Vector.class,this); classRegistry.registerClass(Hashtable.class,this); classRegistry.registerClass(BitSet.class,this); + classRegistry.registerClass(Timestamp.class, this); classRegistry.registerClass(Locale.class,this); classRegistry.registerClass(StringBuffer.class,this); diff --git a/src/main/java/org/nustaq/serialization/serializers/FSTTimestampSerializer.java b/src/main/java/org/nustaq/serialization/serializers/FSTTimestampSerializer.java new file mode 100644 index 00000000..f5cffd80 --- /dev/null +++ b/src/main/java/org/nustaq/serialization/serializers/FSTTimestampSerializer.java @@ -0,0 +1,31 @@ +package org.nustaq.serialization.serializers; + +import java.io.IOException; + +import org.nustaq.serialization.FSTBasicObjectSerializer; +import org.nustaq.serialization.FSTClazzInfo; +import org.nustaq.serialization.FSTClazzInfo.FSTFieldInfo; +import org.nustaq.serialization.FSTObjectInput; +import org.nustaq.serialization.FSTObjectOutput; + +public class FSTTimestampSerializer extends FSTBasicObjectSerializer { + + @Override + public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo, + FSTFieldInfo referencedBy, int streamPosition) throws IOException { + out.writeLong(((java.sql.Timestamp)toWrite).getTime()); + } + + @Override + public boolean alwaysCopy(){ + return true; + } + + @Override + public Object instantiate(Class objectClass, FSTObjectInput in, FSTClazzInfo serializationInfo, + FSTClazzInfo.FSTFieldInfo referencee, int streamPosition) throws Exception { + long l = in.readLong(); + Object res = new java.sql.Timestamp(l); + return res; + } +} From db7ba746910703a86abaaa579c64349184916b44 Mon Sep 17 00:00:00 2001 From: "m.ross" Date: Sat, 17 Nov 2018 13:40:49 -0500 Subject: [PATCH 3/5] Proxy serializer --- .../serializers/FSTProxySerializer.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/main/java/org/nustaq/serialization/serializers/FSTProxySerializer.java diff --git a/src/main/java/org/nustaq/serialization/serializers/FSTProxySerializer.java b/src/main/java/org/nustaq/serialization/serializers/FSTProxySerializer.java new file mode 100644 index 00000000..99318d32 --- /dev/null +++ b/src/main/java/org/nustaq/serialization/serializers/FSTProxySerializer.java @@ -0,0 +1,48 @@ +package org.nustaq.serialization.serializers; + + +import org.nustaq.serialization.FSTBasicObjectSerializer; +import org.nustaq.serialization.FSTClazzInfo; +import org.nustaq.serialization.FSTObjectInput; +import org.nustaq.serialization.FSTObjectOutput; + +import java.io.IOException; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Proxy; + + +public class FSTProxySerializer extends FSTBasicObjectSerializer { + + @Override + public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo, FSTClazzInfo.FSTFieldInfo referencedBy, int streamPosition) throws IOException { + Class[] ifaces = clzInfo.getClazz().getInterfaces(); + ClassLoader cl = out.getConf().getClassLoader(); + out.writeInt(ifaces.length); + for (Class i : ifaces) + out.writeUTF(i.getName()); + out.writeObject(Proxy.getInvocationHandler(toWrite)); + } + + @Override + public Object instantiate(Class objectClass, FSTObjectInput in, FSTClazzInfo serializationInfo, FSTClazzInfo.FSTFieldInfo referencee, int streamPositioin) throws IOException, ClassNotFoundException { + ClassLoader cl = in.getConf().getClassLoader(); + int numIfaces = in.readInt(); + String[] interfaces = new String[numIfaces]; + for (int i = 0; i < numIfaces; i++) { + interfaces[i] = in.readUTF(); + } + Class[] classObjs = new Class[interfaces.length]; + + for(int i = 0; i < interfaces.length; ++i) { + try { + classObjs[i] = Class.forName(interfaces[i], false, cl); + } catch (ClassNotFoundException e) { + classObjs[i] = Class.forName(interfaces[i], false, this.getClass().getClassLoader()); + } + } + InvocationHandler ih = (InvocationHandler)in.readObject(); + Object res = Proxy.newProxyInstance(in.getConf().getClassLoader(),classObjs,ih); + in.registerObject(res,streamPositioin,serializationInfo,referencee); + return res; + } +} \ No newline at end of file From 00747d533626a2b194035566935e33c3d32d5d10 Mon Sep 17 00:00:00 2001 From: "m.ross" Date: Sat, 17 Nov 2018 13:46:27 -0500 Subject: [PATCH 4/5] Throwable serializer --- .../serialization/FSTConfiguration.java | 1 + .../serializers/FSTThrowableSerializer.java | 38 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/main/java/org/nustaq/serialization/serializers/FSTThrowableSerializer.java diff --git a/src/main/java/org/nustaq/serialization/FSTConfiguration.java b/src/main/java/org/nustaq/serialization/FSTConfiguration.java index 499f5864..5129943f 100644 --- a/src/main/java/org/nustaq/serialization/FSTConfiguration.java +++ b/src/main/java/org/nustaq/serialization/FSTConfiguration.java @@ -501,6 +501,7 @@ protected static FSTConfiguration initDefaultFstConfigurationInternal(FSTConfigu reg.putSerializer(Hashtable.class, new FSTMapSerializer(), true); reg.putSerializer(ConcurrentHashMap.class, new FSTMapSerializer(), true); reg.putSerializer(FSTStruct.class, new FSTStructSerializer(), true); + reg.putSerializer(Throwable.class, new FSTThrowableSerializer(),true); reg.putSerializer(BitSet.class, new FSTBitSetSerializer(),true); reg.putSerializer(Timestamp.class, new FSTTimestampSerializer(),true); diff --git a/src/main/java/org/nustaq/serialization/serializers/FSTThrowableSerializer.java b/src/main/java/org/nustaq/serialization/serializers/FSTThrowableSerializer.java new file mode 100644 index 00000000..96d95de2 --- /dev/null +++ b/src/main/java/org/nustaq/serialization/serializers/FSTThrowableSerializer.java @@ -0,0 +1,38 @@ +package org.nustaq.serialization.serializers; + +import org.nustaq.serialization.FSTBasicObjectSerializer; +import org.nustaq.serialization.FSTClazzInfo; +import org.nustaq.serialization.FSTObjectInput; +import org.nustaq.serialization.FSTObjectOutput; + +import java.io.IOException; +import java.lang.reflect.Constructor; + +public class FSTThrowableSerializer extends FSTBasicObjectSerializer { + @Override + public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo, + FSTClazzInfo.FSTFieldInfo referencedBy, int streamPosition) throws IOException { + Throwable t = (Throwable)toWrite; + out.writeStringUTF(t.getMessage()); + StackTraceElement[] ste = t.getStackTrace(); + out.writeObject(ste); + out.writeObject(t.getCause()); + out.writeObject(t.getSuppressed()); + } + + + @Override + public Object instantiate(Class objectClass, FSTObjectInput in, FSTClazzInfo serializationInfo, + FSTClazzInfo.FSTFieldInfo referencee, int streamPosition) throws Exception { + Constructor constructor = objectClass.getConstructor(String.class); + Throwable t = constructor.newInstance(in.readStringUTF()); // This causes stack trace to be filled in twice but not an easy way to solve + StackTraceElement[] ste = (StackTraceElement[]) in.readObject(); + if (ste!=null) + t.setStackTrace(ste); + t.initCause((Throwable) in.readObject()); + Throwable[] suppressed = (Throwable[]) in.readObject(); + for (Throwable s : suppressed) + t.addSuppressed(s); + return t; + } +} From 02332907d3c89c7a68d352bcf75b7fa473a45265 Mon Sep 17 00:00:00 2001 From: "m.ross" Date: Sun, 18 Nov 2018 10:05:22 -0500 Subject: [PATCH 5/5] Expose getters for some corresponding setters --- .../org/nustaq/serialization/coders/FSTStreamDecoder.java | 4 ++++ .../org/nustaq/serialization/coders/FSTStreamEncoder.java | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/src/main/java/org/nustaq/serialization/coders/FSTStreamDecoder.java b/src/main/java/org/nustaq/serialization/coders/FSTStreamDecoder.java index bde0f9dc..2b204024 100644 --- a/src/main/java/org/nustaq/serialization/coders/FSTStreamDecoder.java +++ b/src/main/java/org/nustaq/serialization/coders/FSTStreamDecoder.java @@ -452,6 +452,10 @@ public void setInputStream(InputStream in) { clnames.clear(); } + public InputStream getInputStream() { + return input; + } + @Override public void resetToCopyOf(byte[] bytes, int off, int len) { input.reset(); diff --git a/src/main/java/org/nustaq/serialization/coders/FSTStreamEncoder.java b/src/main/java/org/nustaq/serialization/coders/FSTStreamEncoder.java index 521f89bd..3848c310 100644 --- a/src/main/java/org/nustaq/serialization/coders/FSTStreamEncoder.java +++ b/src/main/java/org/nustaq/serialization/coders/FSTStreamEncoder.java @@ -526,6 +526,10 @@ public void setOutstream(OutputStream outstream) { else buffout.setOutstream(outstream); } + + public OutputStream getOutstream() { + return buffout; + } /** * writes current buffer to underlying output and resets buffer.