Skip to content

Commit

Permalink
Merge pull request #267 from TMCBonds/master
Browse files Browse the repository at this point in the history
Add BitSet, Timestamp, Proxy and Throwable native serializers
  • Loading branch information
RuedigerMoeller authored Nov 18, 2018
2 parents 8a486fd + 0233290 commit 59f8f80
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/java/org/nustaq/serialization/FSTConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -500,6 +501,10 @@ 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);

// serializers for classes failing in fst JDK emulation (e.g. Android<=>JDK)
reg.putSerializer(BigInteger.class, new FSTBigIntegerSerializer(), true);
Expand Down Expand Up @@ -830,6 +835,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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<? extends Throwable> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 59f8f80

Please sign in to comment.