Hi Reid,
I find using
java.nio.ByteBuffer is very convenient for me when I'm implementing GetStateInformation() and SetStateInformation().
In GetStateInformation() I would do something like
Code: Select all
int bufSize = 2048; // calculate the buffer size according to your needs; int := 4 bytes, double := 8 bytes
ByteBuffer buf = ByteBuffer.allocate(bufSize);
Then I use the
putInt(), putDouble(), put() and similar methods of the ByteBuffer to fill it with the data I want to store. Strings can be stored like
Code: Select all
byte[] stringBytes = myString.getBytes();
and add its length + 4 byte (for an int to store the length of stringBytes) to
bufSize.
The final line in GetStateInformation() is then
In SetStateInformation(byte[] stateInfo) I basically do it the other way around. I use
Code: Select all
ByteBuffer buf = ByteBuffer.wrap(stateInfo);
to turn the byte[] into a ByteBuffer. The I use the
getInt(), getDouble(), get() methods of the ByteBuffer to retrieve the information stored in the byte[].
In general you need to think of a "format" or "order" in which you want/need to store your data so you can easily get it back from the byte[], e.g. store the length of strings or data with variable size in general.
Hope this helps. Best regards,
Martin