Results 1 to 3 of 3
- 09-20-2010, 04:05 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 18
- Rep Power
- 0
Reading Characters from a ByteBuffer
Hi everyone,
I have a ByteBuffer with some information in it. At some point it needs to read a set of UTF characters. I have the length of these characters stored in a Short just before the bytes of the characters.
Currently my code looks like:
This works fine but it reads the whole of the remainder of the byteBuffer. I need it to read only the the number of bytes specified by len.Java Code:int len = byteBuffer.getShort(); String msg = decoder.decode(byteBuffer).toString();
On a side note is this the most efficient way to check I have enough data before I read it? (it works by storing the length as a short, reading that number of bytes and checking if there are any more bytes left over):
Thanks for your help and reading,Java Code:protected void readData(ByteBuffer readBuffer) throws IOException { if (readBuffer.limit() > 2) { int pos = readBuffer.position(); int len = readBuffer.getShort(); if (readBuffer.remaining() >= len) { readCommand(readBuffer); if (readBuffer.remaining() != 0) { readData(readBuffer); } else { readBuffer.clear(); } } else { readBuffer.position(pos); } } }
Dan
- 09-20-2010, 04:29 PM #2
Does this method: decoder.decode() have an overloaded version that takes a length?
- 09-20-2010, 10:05 PM #3
Member
- Join Date
- Aug 2010
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
Scanner reading accented characters
By Phenomena in forum New To JavaReplies: 2Last Post: 04-29-2010, 04:06 PM -
Reading characters without showing up on the screen?
By alexsb92 in forum New To JavaReplies: 11Last Post: 03-28-2010, 08:08 PM -
ByteBuffer as***Buffer.hasArray();
By bloose in forum New To JavaReplies: 0Last Post: 11-20-2009, 10:55 PM -
[SOLVED] InputStream into ByteBuffer
By OrangeDog in forum Advanced JavaReplies: 7Last Post: 04-29-2009, 12:19 PM -
writing and reading unicode characters from a file
By ranoosh in forum Advanced JavaReplies: 4Last Post: 09-28-2008, 04:34 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks