Created
October 17, 2011 08:16
-
-
Save samstokes/1292190 to your computer and use it in GitHub Desktop.
One ISerialization to rule them all?
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
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