import java.io.*; import java.util.Vector; class Data { ByteArrayOutputStream bos = new ByteArrayOutputStream(20); int active; boolean mondayFirst; Vector profiles = new Vector(); private int VERSION_2 = 2; private int LAST_VERSION = VERSION_2; Data(byte[] raw) throws IOException { this(new DataInputStream(new ByteArrayInputStream(raw))); } Data(DataInput in) throws IOException { if (in == null) { profiles.addElement(new Profile("#1")); active = 0; mondayFirst = true; } else { int version = in.readByte(); if (version >= VERSION_2) { active = in.readShort(); mondayFirst = in.readBoolean(); int size = in.readShort(); for (int i = 0; i < size; ++i) { profiles.addElement(new Profile(in, version)); } } else { byte b = in.readByte(); active = b & 0x0f; b = in.readByte(); mondayFirst = (b & 0x01) == 0; //read all profiles one per record //for backwards compat } } } byte[] serialize() { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); writeTo(new DataOutputStream(bos)); return bos.toByteArray(); } catch (IOException e) { Log.log("" + e); throw new Error(""+e); } } void writeTo(DataOutput os) throws IOException { os.writeByte((byte)LAST_VERSION); os.writeShort(active); os.writeBoolean(mondayFirst); int size = profiles.size(); os.writeShort(size); for (int i = 0; i < size; ++i) { ((Profile) profiles.elementAt(i)).writeTo(os); } } int size() { return profiles.size(); } Profile activeProfile() { return (Profile) profiles.elementAt(active); } void merge(Data other) { Vector otherProfiles = other.profiles; int otherSize = otherProfiles.size(); for (int i = 0; i < otherSize; ++i) { profiles.addElement(otherProfiles.elementAt(i)); } } void setActive(int newActive) { int size = profiles.size(); if (newActive > size) { throw new Error("set profile " + newActive + " among " + size); } if (newActive == size) { profiles.addElement(new Profile("#"+(newActive+1))); } active = newActive; } }