Skip to content

Commit 59f8f80

Browse files
Merge pull request #267 from TMCBonds/master
Add BitSet, Timestamp, Proxy and Throwable native serializers
2 parents 8a486fd + 0233290 commit 59f8f80

File tree

5 files changed

+154
-0
lines changed

5 files changed

+154
-0
lines changed

src/main/java/org/nustaq/serialization/FSTConfiguration.java

+6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.math.BigInteger;
4040
import java.net.URL;
4141
import java.nio.charset.StandardCharsets;
42+
import java.sql.Timestamp;
4243
import java.text.SimpleDateFormat;
4344
import java.util.*;
4445
import java.util.List;
@@ -500,6 +501,10 @@ protected static FSTConfiguration initDefaultFstConfigurationInternal(FSTConfigu
500501
reg.putSerializer(Hashtable.class, new FSTMapSerializer(), true);
501502
reg.putSerializer(ConcurrentHashMap.class, new FSTMapSerializer(), true);
502503
reg.putSerializer(FSTStruct.class, new FSTStructSerializer(), true);
504+
reg.putSerializer(Throwable.class, new FSTThrowableSerializer(),true);
505+
506+
reg.putSerializer(BitSet.class, new FSTBitSetSerializer(),true);
507+
reg.putSerializer(Timestamp.class, new FSTTimestampSerializer(),true);
503508

504509
// serializers for classes failing in fst JDK emulation (e.g. Android<=>JDK)
505510
reg.putSerializer(BigInteger.class, new FSTBigIntegerSerializer(), true);
@@ -830,6 +835,7 @@ void addDefaultClazzes() {
830835
classRegistry.registerClass(Vector.class,this);
831836
classRegistry.registerClass(Hashtable.class,this);
832837
classRegistry.registerClass(BitSet.class,this);
838+
classRegistry.registerClass(Timestamp.class, this);
833839
classRegistry.registerClass(Locale.class,this);
834840

835841
classRegistry.registerClass(StringBuffer.class,this);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.nustaq.serialization.serializers;
2+
3+
import org.nustaq.serialization.FSTBasicObjectSerializer;
4+
import org.nustaq.serialization.FSTClazzInfo;
5+
import org.nustaq.serialization.FSTObjectInput;
6+
import org.nustaq.serialization.FSTObjectOutput;
7+
8+
import java.io.IOException;
9+
import java.util.BitSet;
10+
11+
public class FSTBitSetSerializer extends FSTBasicObjectSerializer {
12+
@Override
13+
public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo,
14+
FSTClazzInfo.FSTFieldInfo referencedBy, int streamPosition) throws IOException {
15+
out.writeObject(((BitSet)toWrite).toLongArray());
16+
}
17+
18+
@Override
19+
public boolean alwaysCopy(){
20+
return true;
21+
}
22+
23+
@Override
24+
public Object instantiate(Class objectClass, FSTObjectInput in, FSTClazzInfo serializationInfo,
25+
FSTClazzInfo.FSTFieldInfo referencee, int streamPosition) throws Exception {
26+
long[] l = (long[])in.readObject();
27+
Object res = BitSet.valueOf(l);
28+
return res;
29+
}
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.nustaq.serialization.serializers;
2+
3+
4+
import org.nustaq.serialization.FSTBasicObjectSerializer;
5+
import org.nustaq.serialization.FSTClazzInfo;
6+
import org.nustaq.serialization.FSTObjectInput;
7+
import org.nustaq.serialization.FSTObjectOutput;
8+
9+
import java.io.IOException;
10+
import java.lang.reflect.InvocationHandler;
11+
import java.lang.reflect.Proxy;
12+
13+
14+
public class FSTProxySerializer extends FSTBasicObjectSerializer {
15+
16+
@Override
17+
public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo, FSTClazzInfo.FSTFieldInfo referencedBy, int streamPosition) throws IOException {
18+
Class<?>[] ifaces = clzInfo.getClazz().getInterfaces();
19+
ClassLoader cl = out.getConf().getClassLoader();
20+
out.writeInt(ifaces.length);
21+
for (Class i : ifaces)
22+
out.writeUTF(i.getName());
23+
out.writeObject(Proxy.getInvocationHandler(toWrite));
24+
}
25+
26+
@Override
27+
public Object instantiate(Class objectClass, FSTObjectInput in, FSTClazzInfo serializationInfo, FSTClazzInfo.FSTFieldInfo referencee, int streamPositioin) throws IOException, ClassNotFoundException {
28+
ClassLoader cl = in.getConf().getClassLoader();
29+
int numIfaces = in.readInt();
30+
String[] interfaces = new String[numIfaces];
31+
for (int i = 0; i < numIfaces; i++) {
32+
interfaces[i] = in.readUTF();
33+
}
34+
Class[] classObjs = new Class[interfaces.length];
35+
36+
for(int i = 0; i < interfaces.length; ++i) {
37+
try {
38+
classObjs[i] = Class.forName(interfaces[i], false, cl);
39+
} catch (ClassNotFoundException e) {
40+
classObjs[i] = Class.forName(interfaces[i], false, this.getClass().getClassLoader());
41+
}
42+
}
43+
InvocationHandler ih = (InvocationHandler)in.readObject();
44+
Object res = Proxy.newProxyInstance(in.getConf().getClassLoader(),classObjs,ih);
45+
in.registerObject(res,streamPositioin,serializationInfo,referencee);
46+
return res;
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.nustaq.serialization.serializers;
2+
3+
import org.nustaq.serialization.FSTBasicObjectSerializer;
4+
import org.nustaq.serialization.FSTClazzInfo;
5+
import org.nustaq.serialization.FSTObjectInput;
6+
import org.nustaq.serialization.FSTObjectOutput;
7+
8+
import java.io.IOException;
9+
import java.lang.reflect.Constructor;
10+
11+
public class FSTThrowableSerializer extends FSTBasicObjectSerializer {
12+
@Override
13+
public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo,
14+
FSTClazzInfo.FSTFieldInfo referencedBy, int streamPosition) throws IOException {
15+
Throwable t = (Throwable)toWrite;
16+
out.writeStringUTF(t.getMessage());
17+
StackTraceElement[] ste = t.getStackTrace();
18+
out.writeObject(ste);
19+
out.writeObject(t.getCause());
20+
out.writeObject(t.getSuppressed());
21+
}
22+
23+
24+
@Override
25+
public Object instantiate(Class objectClass, FSTObjectInput in, FSTClazzInfo serializationInfo,
26+
FSTClazzInfo.FSTFieldInfo referencee, int streamPosition) throws Exception {
27+
Constructor<? extends Throwable> constructor = objectClass.getConstructor(String.class);
28+
Throwable t = constructor.newInstance(in.readStringUTF()); // This causes stack trace to be filled in twice but not an easy way to solve
29+
StackTraceElement[] ste = (StackTraceElement[]) in.readObject();
30+
if (ste!=null)
31+
t.setStackTrace(ste);
32+
t.initCause((Throwable) in.readObject());
33+
Throwable[] suppressed = (Throwable[]) in.readObject();
34+
for (Throwable s : suppressed)
35+
t.addSuppressed(s);
36+
return t;
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.nustaq.serialization.serializers;
2+
3+
import java.io.IOException;
4+
5+
import org.nustaq.serialization.FSTBasicObjectSerializer;
6+
import org.nustaq.serialization.FSTClazzInfo;
7+
import org.nustaq.serialization.FSTClazzInfo.FSTFieldInfo;
8+
import org.nustaq.serialization.FSTObjectInput;
9+
import org.nustaq.serialization.FSTObjectOutput;
10+
11+
public class FSTTimestampSerializer extends FSTBasicObjectSerializer {
12+
13+
@Override
14+
public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo,
15+
FSTFieldInfo referencedBy, int streamPosition) throws IOException {
16+
out.writeLong(((java.sql.Timestamp)toWrite).getTime());
17+
}
18+
19+
@Override
20+
public boolean alwaysCopy(){
21+
return true;
22+
}
23+
24+
@Override
25+
public Object instantiate(Class objectClass, FSTObjectInput in, FSTClazzInfo serializationInfo,
26+
FSTClazzInfo.FSTFieldInfo referencee, int streamPosition) throws Exception {
27+
long l = in.readLong();
28+
Object res = new java.sql.Timestamp(l);
29+
return res;
30+
}
31+
}

0 commit comments

Comments
 (0)