Page 1 of 1
Generic byte arrays
Posted: Thu Jul 14, 2022 3:26 am
by UrbanCyborg
This is really a question on how to use
GetStateInformation() and
SetStateInformation(). How
do you pass different information into and out of the byte arrays those routines use for data transfer? In
C or
C++ the method would involve casting to a pointer type, but that's not an option in
Java, and I can't find any sample code that shows how to do it. For instance, how would you save (or restore) say, a double, or a String to/from a byte array. I suppose for a string you could split out the characters one at a time and save them (although I'd bet character typing would get in the way), and it just don't seem right to break up a double by masking to get at byte-sized chunks. So I'm guessing there's some idiosyncratic syntax for doing this, and I'd really appreciate it if some kind soul would take pity on a poor ignorant code monkey and tell me what it is.

Thanks.
Reid
Re: Generic byte arrays
Posted: Thu Jul 14, 2022 6:15 am
by honki-bobo
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
Re: Generic byte arrays
Posted: Thu Jul 14, 2022 8:07 am
by UrbanCyborg
Idiosyncratic I said, and idiosyncratic it is. Don't think that solution would have occurred to me, as that's a part of the library I'm not familiar with. Thanks mucho, mi amigo!
Reid
Re: Generic byte arrays
Posted: Thu Jul 14, 2022 3:00 pm
by ColinP
Good advice from Martin.
ByteBuffer is a great class, it all works and makes sense and saves you having to worry about a lot of things but it still feels a little clumsy converting between byte[] and ByteBuffer on top of the other conversions.
I still consider myself a beginner in Java so might be missing an easier way but I ended up with ByteBuffer.allocate( Integer.BYTES ).putInt( i ).array() as the correct way to convert an integer to a byte array. Which does look a little idiosyncratic. I have a small number of helper functions that wrap such weird stuff up, so for instance the above becomes just intAsByteArray( i ) in my main code.
For simple modules I just write String asString() and void fromString( String s ) methods to convert module state to a string and a string to module state and then use ...
Code: Select all
@Override
public byte[] GetStateInformation()
{
return asString().getBytes( StandardCharsets.UTF_8 );
}
@Override
public void SetStateInformation(byte[] stateInfo)
{
fromString( new String( stateInfo, StandardCharsets.UTF_8 ) );
}
I use the same methods to perform undo and redo.
Using strings for state is also handy for debugging as you can just look at the string.
Re: Generic byte arrays
Posted: Thu Jul 14, 2022 5:52 pm
by UrbanCyborg
Thanks, to both of you! I'm busy reading up on that end of the library, now that I'm aware of it. Since I'm coming from
C++, idiosyncratic doesn't bother me.
