Created
February 14, 2013 15:52
-
-
Save dennisdegryse/4953694 to your computer and use it in GitHub Desktop.
Proof of concept for object serialization. Note: custom data objects must override the equals method and implement
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.ObjectInputStream; | |
import java.io.ObjectOutputStream; | |
import java.io.PipedInputStream; | |
import java.io.PipedOutputStream; | |
import java.io.Serializable; | |
public class TestApp { | |
public static class DataPackage implements Serializable { | |
private static final long serialVersionUID = -3910272123635233286L; | |
private final int x; | |
private final String a; | |
public DataPackage(int x, String a) { | |
this.x = x; | |
this.a = a; | |
} | |
public int getX() { | |
return x; | |
} | |
public String getA() { | |
return a; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (o instanceof DataPackage) | |
return ((DataPackage) o).x == x && ((DataPackage) o).a.equals(a); | |
return false; | |
} | |
}; | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
try { | |
final Object testData = new DataPackage(-9, "asagg"); | |
final PipedOutputStream pos = new PipedOutputStream(); | |
final PipedInputStream pis = new PipedInputStream(); | |
pos.connect(pis); | |
final ObjectOutputStream oos = new ObjectOutputStream(pos); | |
final ObjectInputStream ois = new ObjectInputStream(pis); | |
oos.writeObject(testData); | |
final Object result = ois.readObject(); | |
oos.close(); | |
final boolean success = result.equals(testData); | |
final DataPackage resultPackage = (DataPackage)result; | |
System.out.println(success); | |
System.out.print(resultPackage.getX()); | |
System.out.print(resultPackage.getA()); | |
System.in.read(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment