Skip to content

Instantly share code, notes, and snippets.

@samstokes
Created October 17, 2011 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samstokes/1292190 to your computer and use it in GitHub Desktop.
Save samstokes/1292190 to your computer and use it in GitHub Desktop.
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