Results 1 to 3 of 3
  1. #1
    Dan0100 is offline Member
    Join Date
    Aug 2010
    Posts
    18
    Rep Power
    0

    Default 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:
    Java Code:
    int len = byteBuffer.getShort();
    String msg = decoder.decode(byteBuffer).toString();
    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.

    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):
    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);
                }
            }
    }
    Thanks for your help and reading,
    Dan

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,573
    Rep Power
    23

    Default

    Does this method: decoder.decode() have an overloaded version that takes a length?

  3. #3
    Dan0100 is offline Member
    Join Date
    Aug 2010
    Posts
    18
    Rep Power
    0

    Default

    My fix:

    Java Code:
    public String readString(ByteBuffer data) throws CharacterCodingException {
            int len = data.getShort();
            int originalLimit = data.limit();
    
            data.limit(data.position() + len);
            String msg = Main.decoder.decode(data).toString();
            data.limit(originalLimit);
    
            return msg;
    }

Similar Threads

  1. Scanner reading accented characters
    By Phenomena in forum New To Java
    Replies: 2
    Last Post: 04-29-2010, 04:06 PM
  2. Reading characters without showing up on the screen?
    By alexsb92 in forum New To Java
    Replies: 11
    Last Post: 03-28-2010, 08:08 PM
  3. ByteBuffer as***Buffer.hasArray();
    By bloose in forum New To Java
    Replies: 0
    Last Post: 11-20-2009, 10:55 PM
  4. [SOLVED] InputStream into ByteBuffer
    By OrangeDog in forum Advanced Java
    Replies: 7
    Last Post: 04-29-2009, 12:19 PM
  5. writing and reading unicode characters from a file
    By ranoosh in forum Advanced Java
    Replies: 4
    Last Post: 09-28-2008, 04:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •