Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  ByteBuffer with string, ints and floats  (Read 1828 times)
0 Members and 1 Guest are viewing this topic.
Offline MickeyB

Senior Member




my game will work, my game will work!


« Posted 2005-02-09 18:47:37 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
                  // 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?
1  
2  
3  
int msgType = b.getInt();
int channel = b.getInt();
String msg = ?????


thanks!

MickeyB

Current Project: http://www22.brinkster.com/mbowles/
Offline Daire Quinlan

Junior Member





« Reply #1 - Posted 2005-02-09 19:13:11 »

Quote

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


thanks!


what about

1  
2  
3  
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.
Offline tom
« Reply #2 - Posted 2005-02-09 19:28:17 »

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

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline MickeyB

Senior Member




my game will work, my game will work!


« Reply #3 - Posted 2005-02-09 19:31:53 »

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

MickeyB

Current Project: http://www22.brinkster.com/mbowles/
Offline Daire Quinlan

Junior Member





« Reply #4 - Posted 2005-02-09 19:42:25 »

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


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:

1  
2  
3  
4  
5  
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.
Offline MickeyB

Senior Member




my game will work, my game will work!


« Reply #5 - Posted 2005-02-09 19:49:44 »

thanks, will give that whirl

MickeyB

Current Project: http://www22.brinkster.com/mbowles/
Offline tom
« Reply #6 - Posted 2005-02-09 22:26:46 »

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 Smiley

Offline MickeyB

Senior Member




my game will work, my game will work!


« Reply #7 - Posted 2005-02-10 11:53:36 »

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

MickeyB

Current Project: http://www22.brinkster.com/mbowles/
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #8 - Posted 2005-02-11 00:55:03 »

>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

弾幕 ☆ @mahonnaiseblog
Offline Daire Quinlan

Junior Member





« Reply #9 - Posted 2005-02-11 01:16:31 »

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


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 ... :-)

D.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline MickeyB

Senior Member




my game will work, my game will work!


« Reply #10 - Posted 2005-02-11 12:51:55 »

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

MickeyB

Current Project: http://www22.brinkster.com/mbowles/
Offline tom
« Reply #11 - Posted 2005-02-11 14:46:49 »

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.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (15 views)
2014-07-29 18:09:19

Riven (10 views)
2014-07-29 18:08:52

Dwinin (10 views)
2014-07-29 10:59:34

E.R. Fleming (28 views)
2014-07-29 03:07:13

E.R. Fleming (10 views)
2014-07-29 03:06:25

pw (40 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (27 views)
2014-07-23 21:07:15

Riven (29 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!