Bytebuffer and UTF

J

jespersahner

In Java IO the DataInputStream-class supports readInt()-,
readDouble()- og especially readUTF(). In Java NIO the ByteBuffer-class
has the same functionality through getInt(), getDouble() - but getUTF()
doesn't exist.

How is UTF read with Java NIO?

You can probably make a workaround by copying the source from
readUFT() in the DataInputStream-class, by I wonder why there isn't a
getUTF()-method in the ByteBuffer-class.

Am I missing something?
 
T

Thomas Hawtin

jespersahner said:
How is UTF read with Java NIO?

You can probably make a workaround by copying the source from
readUFT() in the DataInputStream-class, by I wonder why there isn't a
getUTF()-method in the ByteBuffer-class.

Am I missing something?

java.nio.charsets?

Tom Hawtin
 
J

jespersahner

Could you be more specific? The "trick" about writeUTF() and readUTF()
is that the length of the string is "included" in the format.

Regards,
Jesper
 
D

Dale King

jespersahner said:
Could you be more specific? The "trick" about writeUTF() and readUTF()
is that the length of the string is "included" in the format.

That format is specific to writeUTF and readUTF. Basically you need to
do the length yourself and then you can convert between char and byte
using the encode and decode methods of Charset.

If you need to actually emulate the length property of readUTF/writeUTF
then you have a little more work to do.
 
R

Roedy Green

That format is specific to writeUTF and readUTF. Basically you need to
do the length yourself and then you can convert between char and byte
using the encode and decode methods of Charset.

writeUTF prepends a length byte and readUTF expect one. These are for
embedding strings in DataOutput binary.


If you are reading UTF text files, then you just select the
appropriate encoding e.g. UTF-8 and the Reader/Writer deals with the
8-16 bit transformations.

see http://mindprod.com/applets/fileio.html
for sample code to do things both ways.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top