Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
One ISerialization to rule them all?
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import backtype.storm.serialization.ISerialization;
public class SerializableSerialization<T extends Serializable> implements ISerialization<T> {
@Override
public boolean accept(@SuppressWarnings("rawtypes") Class c) {
return Serializable.class.isAssignableFrom(c);
}
@Override
public void serialize(T object, DataOutputStream stream) throws IOException {
new ObjectOutputStream(stream).writeObject(object);
stream.flush();
}
@SuppressWarnings("unchecked")
@Override
public T deserialize(DataInputStream stream) throws IOException {
try {
return (T) new ObjectInputStream(stream).readObject();
} catch (ClassNotFoundException e) {
throw new IOException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.