Add serialization versioning
This commit is contained in:
parent
c02c00ee64
commit
07bec548b6
2 changed files with 24 additions and 0 deletions
Binary file not shown.
|
@ -5,12 +5,23 @@ import java.io.DataOutputStream;
|
|||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class BinSerializer {
|
||||
|
||||
// ascii (klooni) and binary (1010b)
|
||||
private static byte[] HEADER = { 0x6B, 0x6C, 0x6F, 0x6F, 0x6E, 0x69, 0xa };
|
||||
|
||||
// MODIFY THIS VALUE EVERY TIME A BinSerializable IMPLEMENTATION CHANGES
|
||||
// Or unwanted results will happen and corrupt the game in an unknown way.
|
||||
private static int VERSION = 1;
|
||||
|
||||
public static void serialize(final BinSerializable serializable, final OutputStream output)
|
||||
throws IOException {
|
||||
DataOutputStream out = new DataOutputStream(output);
|
||||
try {
|
||||
out.write(HEADER);
|
||||
out.writeInt(VERSION);
|
||||
serializable.write(out);
|
||||
} finally {
|
||||
try {
|
||||
|
@ -23,6 +34,19 @@ public class BinSerializer {
|
|||
throws IOException {
|
||||
DataInputStream in = new DataInputStream(input);
|
||||
try {
|
||||
// Read the HEADER and the VERSION (checks)
|
||||
byte[] savedBuffer = new byte[HEADER.length];
|
||||
in.readFully(savedBuffer);
|
||||
if (!Arrays.equals(savedBuffer, HEADER))
|
||||
throw new IOException("Invalid saved header found.");
|
||||
|
||||
int savedVersion = in.readInt();
|
||||
if (savedVersion != VERSION) {
|
||||
throw new IOException(
|
||||
"Invalid saved version found. Should be " + VERSION + ", not " + savedVersion);
|
||||
}
|
||||
|
||||
// Read the saved data if the checks passed
|
||||
serializable.read(in);
|
||||
} finally {
|
||||
try {
|
||||
|
|
Loading…
Reference in a new issue