-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #267 from TMCBonds/master
Add BitSet, Timestamp, Proxy and Throwable native serializers
- Loading branch information
Showing
5 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/org/nustaq/serialization/serializers/FSTBitSetSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/org/nustaq/serialization/serializers/FSTProxySerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/org/nustaq/serialization/serializers/FSTThrowableSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/org/nustaq/serialization/serializers/FSTTimestampSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |