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
Generic byte arrays
-
- Posts: 588
- Joined: Mon Nov 15, 2021 9:23 pm
Generic byte arrays
Cyberwerks Heavy Industries -- viewforum.php?f=76
- honki-bobo
- Posts: 305
- Joined: Sat Nov 09, 2019 1:18 pm
Re: Generic byte arrays
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
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
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
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
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);
Code: Select all
byte[] stringBytes = myString.getBytes();
The final line in GetStateInformation() is then
Code: Select all
return buf.array();
Code: Select all
ByteBuffer buf = ByteBuffer.wrap(stateInfo);
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
-
- Posts: 588
- Joined: Mon Nov 15, 2021 9:23 pm
Re: Generic byte arrays
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
Reid
Cyberwerks Heavy Industries -- viewforum.php?f=76
Re: Generic byte arrays
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 ...
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.
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 ) );
}
Using strings for state is also handy for debugging as you can just look at the string.
-
- Posts: 588
- Joined: Mon Nov 15, 2021 9:23 pm
Re: Generic byte arrays
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.
Cyberwerks Heavy Industries -- viewforum.php?f=76