Here is the Java / Android function:
public static byte[] serialize(MyClass myObject) {
int byteArraySizeNeeded = getByteArraySizeNeededForSerialize(myObject);
byte[] result = new byte[byteArraySizeNeeded];
ByteBuffer bb = ByteBuffer.wrap(result);
bb.putShort((short) (byteArraySizeNeeded-2));
bb.putLong(myObject.getProperty1ValueWhichIsALong());
bb.putDouble(myObject.getProperty2ValueWhichIsADouble());
bb.putFloat(myObject.getProperty3ValueWhichIsAFloat);
bb.put(CODE_MYCODE1);
bb.putFloat(myObject.getProperty4ValueWhichIsAFloat);
int curPosition = bb.position();
int offset = 2;
int byteCount = curPosition - offset;
myObject.crc.update(result, offset, byteCount);
long crcValue = myObject.crc.getValue();
bb.put(CODE_CRC);
bb.putLong(crcValue);
return result;
}
I would also like to do in iOS. Either I am missing or cannot find the low level data structure / class what to use.
NSData and NSMutableData claim to be an object that wraps an array of bytes, but they are only useful for dataContentsOfFile and writeToFile .
NSArchiver is like an ObjectOutputStream.
Programming and Serialization Guide
Cannot find any method that is not required to enter a key of type encodeBytes: length: forKey:
I do not want to reinvent the wheel and not use a lot of extra data when saving.
- , putLong() putFloat() putInt() char ?
user529543