|
RSS Feed - WebSphere MQ Support
|
RSS Feed - Message Broker Support
|
 |
|
writeUTF and readFully |
« View previous topic :: View next topic » |
Author |
Message
|
csdenis |
Posted: Mon Dec 02, 2002 3:23 am Post subject: writeUTF and readFully |
|
|
Novice
Joined: 06 Oct 2002 Posts: 24
|
Dear all,
I've got a problem about writeUTF method.
I use writeUTF to write an MQ message in UTF-8 format. When I try to read it back, I use readUTF and the result is perfect. However, when I use readfully to read it into an byte array and contruct it into string using
String sth = new String(myByteArray, "UTF8")
the first 2 bytes of the string are strange characters. I know it is the length in UTF-8, but can I got the exact string using writeUTF and readFully?
Thanks to all. |
|
Back to top |
|
 |
nimconsult |
Posted: Tue Dec 03, 2002 12:00 am Post subject: |
|
|
 Master
Joined: 22 May 2002 Posts: 268 Location: NIMCONSULT - Belgium
|
The readFully() reads the message data, including string length and string contents. The String(byte[], String) constructor does not expect a string length at the beginning of the bytes array.
One possible option is to call m.skipBytes(2) before the m.readFully(...).
My test sample:
Code: |
byte[] b = new byte[m.getDataLength() - 2];
m.skipBytes(2);
m.readFully(b);
String s = new String(b, "UTF8");
System.out.println(s); |
Of course readUTF is easier to use than the previous code sample. But this sample answers your question.
Nicolas _________________ Nicolas Maréchal
Senior Architect - Partner
NIMCONSULT Software Architecture Services (Belgium)
http://www.nimconsult.be |
|
Back to top |
|
 |
|
|
 |
|
Page 1 of 1 |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|
|