ByteBuffer with string, ints and floats

                  // send client message
                  int size = Constants.INT_FLT_SIZE;
                  msg = alias + ": " + msg;
                  size += msg.length();
                  ByteBuffer b = ByteBuffer.allocate(size);
                  synchronized(b) {
                        b.putInt(Constants.MSG_CHAT);
                        b.putInt(4);
                        b.put(msg.getBytes());  
                        b.rewind();
                  }

How can I get the msg part (string) out of this buffer?


int msgType = b.getInt();
int channel = b.getInt();
String msg = ?????

thanks!

what about


int msgType = b.getInt();
int channel = b.getInt();
String msg = b.asCharBuffer().toString();

the CharBuffer starts at the same location as the current position in the ByteBuffer IIRC so the above ought to work. Havent time to check it tho …

D.

Read the remaining bytes into a byte array and use the String(byte[]) constructor.

I had thought of that, but what if you have text/strings that are then followed by more ints or floats, etc?

[quote]I had thought of that, but what if you have text/strings that are then followed by more ints or floats, etc?
[/quote]
In that case you’re going to have to store the length of the string byte array in the ByteBuffer before the string itself. Then simply retrieve the length of the string byte array , and use the following:


int retStringLength = b.getInt();
byte[] strByteArray = new byte[retStringLength];
b.get(strByteArray);

String gotcha = new String(strByteArray);

that ought to work. Although of course you probably don’t want to use an int for the length, a short or even a byte will probably do for ya depending on what you’re sending.

D.

thanks, will give that whirl

Have you looked at using DataInput/OutputStream wrapping ByteArrayInput/OutputStream? It don’t use buffers directly, produces more garbage and are slower. But it has read/writeUTF. Hmmm, maybe not that much better :slight_smile:

Using a SocketChannel with NIO. Should be able to test some stuff above today. Main goal is to send a chat message with a a chatchannel id and send player data with id(int), alias(string), x,y,z(floats), etc… as well. Thanks

In that case you’re going to have to store the length of the
string byte array in the ByteBuffer before the string itself.

Btw… read/writeUTF does exactly that :wink:

[quote]>In that case you’re going to have to store the length of the

string byte array in the ByteBuffer before the string itself.

Btw… read/writeUTF does exactly that :wink:
[/quote]
It does too ! My advice therefore is to completely ignore what i’ve said up until now and “do what onyx said”. actually in retrospect its a much better and probably more portable solution, and as an added bonus, will probably handle Unicode strings without a bother. Something to consider … :slight_smile:

D.

I may be missing something, but I didnt think a SocketChannel had a write/readUTF method. ?

Only DataInput/Output has the UTF function.

If you stick with using msg.getBytes() I suggest you specify what charset to use. It seems from the javadoc that different platforms can have different charsets.