Created
May 24, 2011 03:56
-
-
Save dblevins/988120 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.io.ObjectInputStream; | |
import java.io.ObjectOutputStream; | |
import java.io.Serializable; | |
import junit.framework.TestCase; | |
public class SerializationTest extends TestCase | |
{ | |
public void test() throws Exception | |
{ | |
deserialize(serialize(new Foo("serializable data"))); | |
} | |
public byte[] serialize(Object instance) throws IOException { | |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
ObjectOutputStream os = new ObjectOutputStream(baos); | |
os.writeObject(instance); | |
os.flush(); | |
return baos.toByteArray(); | |
} | |
public Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException { | |
ByteArrayInputStream bais = new ByteArrayInputStream(bytes); | |
ObjectInputStream is = new ObjectInputStream(bais); | |
return is.readObject(); | |
} | |
public static class Foo implements Serializable | |
{ | |
private final Object object; | |
public Foo(Object object) | |
{ | |
this.object = object; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment