public static byte[] serialize(List<? extends VersionedObject> versionedObjectList, int CRCBytes) throws NotSerializableException { FragmentedByteArray byteArray = new FragmentedByteArray(); for (VersionedObject versionedObject : versionedObjectList) { byteArray.add(serialize(versionedObject, CRCBytes)); } return byteArray.generateArray(); }
public static byte[] serialize(VersionedObject versionedObject, int CRCBytes) throws NotSerializableException { FragmentedByteArray data = new FragmentedByteArray( Serializer.serializeObject(versionedObject.getCurrentVersion().toArrayList())); Map<String, Serializable> attributes = versionedObject.serialize(); data.add(Serializer.serialize(attributes.size())); for (Map.Entry<String, Serializable> entry : attributes.entrySet()) { byte[] attributeName = Serializer.serialize(entry.getKey()); // find the type of the attributes byte[] type; byte[] attributeArray; Object attribute = entry.getValue(); if (attribute instanceof String) { type = STRING_TYPE; attributeArray = Serializer.serialize((String) attribute); } else if (attribute instanceof Boolean || attribute == null) { // null value are serialized as a null Boolean type = BOOLEAN_TYPE; attributeArray = Serializer.serialize((Boolean) attribute); } else if (attribute instanceof Byte) { type = BYTE_TYPE; attributeArray = Serializer.serialize((Byte) attribute); } else if (attribute instanceof Short) { type = SHORT_TYPE; attributeArray = Serializer.serialize((Short) attribute); } else if (attribute instanceof Integer) { type = INTEGER_TYPE; attributeArray = Serializer.serialize((Integer) attribute); } else if (attribute instanceof Long) { type = LONG_TYPE; attributeArray = Serializer.serialize((Long) attribute); } else if (attribute instanceof Float) { type = FLOAT_TYPE; attributeArray = Serializer.serialize((Float) attribute); } else if (attribute instanceof Double) { type = DOUBLE_TYPE; attributeArray = Serializer.serialize((Double) attribute); } else if (attribute instanceof Enum<?>) { type = ENUM_TYPE; attributeArray = Serializer.addArrays( Serializer.serialize(attribute.getClass().getName()), Serializer.serialize((Enum) attribute)); } else if (attribute instanceof byte[]) { type = BYTE_ARRAY_TYPE; attributeArray = Serializer.serialize((byte[]) attribute); } else { type = SERIALIZABLE_TYPE; attributeArray = Serializer.serializeObject((Serializable) attribute); } data.add(attributeName, type, attributeArray).generateArray(); } return CRC.addCRC(data.generateArray(), CRCBytes, true); }
public static void serialize( List<? extends VersionedObject> versionedObjectList, String path, String... backupPaths) throws IOException { FragmentedByteArray byteArray = new FragmentedByteArray(); for (VersionedObject versionedObject : versionedObjectList) { byteArray.add(serialize(versionedObject)); } byte[] data = byteArray.generateArray(); FileReaderWriter.writeBytes(path, data); for (String backupPath : backupPaths) { FileReaderWriter.writeBytes(backupPath, data); } }