How to read data from ByteBuffer

15,897

Solution 1

destValue.rewind() 
while (destValue.hasRemaining())
     System.out.println((char)destValue.get());
}

Solution 2

You can get the backing array of a ByteBuffer with .array(). If you want to convert it to a String, you'll also need to get the current position, or else you will have a lot of zero-bytes at the end. So you'll end up with code like:

new String(buf.array(), 0, buf.position())

Edit: Oh, it appears you want the byte values. Those you could either get by calling Arrays.toString(Arrays.copyOf(buf.array(), 0, buf.position()) or loop x from 0 to buf.position calling Integer.toString((int)buf.get(x) & 0xFF, 16) (to get a 2-digit hex code) and collecting the results in a StringBuilder.

Share:
15,897
sanam_bl
Author by

sanam_bl

Updated on June 24, 2022

Comments

  • sanam_bl
    sanam_bl about 2 years

    How to read the data stored in ByteBuffer?

    • setValue() - gets value "12 10" and is converted into Hexadecimal value and is stored in String[] data.
    • write() - converts data into bytes and stores in ByteBuffer dest.
    • readBuffer - How can I read data from ByteBuffer?
    static String[] data = {};
    //value = "12 10";
    String setValue(String value) {
        String[] samples = value.split("[ ,\n]+");
        data = new String[samples.length];
    
        //Generates Hex values
        for (int i = 0; i < samples.length; i++) {
            samples[i] = "0x"+String.format("%02x", Byte.parseByte(samples[i]));
        //data[i] will have values 0x0c, 0x0a
            data[i] = samples[i];
        }
        System.out.println("data :: " +Arrays.toString(samples));
        return value;
    }
    
    
    void write(int sequenceNumber, ByteBuffer dest) {
            for (int i = 0; i < data.length; i++) {
                System.out.println("data[i] in bytes :: "+data[i].getBytes());
    
                dest.put(data[i].getBytes());           
    
            }   
    
        }   
    
    void readBuffer(ByteBuffer destValue)
    {
            //How to read the data stored in ByteBuffer?
    }
    
  • sanam_bl
    sanam_bl about 9 years
    It prints everything in one line as 0x0c0x0a, is it possible to get output as 0x0c, 0x0a. Thanks
  • sanam_bl
    sanam_bl about 9 years
    This is just printing out lot of "0"'s
  • sanam_bl
    sanam_bl about 9 years
    System.out.println( Arrays.toString(Arrays.copyOf(dest.array(), dest.position()))); is returning [48, 120, 48, 99, 48, 120, 48, 97], looks like it is ASCI values. But I want output to be 0x0c, 0x0a. Is there a possibility to get this?
  • llogiq
    llogiq about 9 years
    Yes, which is to loop over the values and call Integer.toString((int)buf.get(x) & 0xFF, 16) on each, then adding (possibly with a separator and prefix of ' 0x') into a StringBuilder. Which java version are you targeting?
  • sanam_bl
    sanam_bl about 9 years
    Java 1.8. Now looks like I am doing completely wrong stuff. My main requirement is "Takes a sequence of byte values in hex format and writes these to the byte stream. "01 02 1a" => writes bytes 0x01 0x02 0x1a to the byte stream. " and I clearly dont understand what needs to be done
  • RAM237
    RAM237 over 2 years
    extra curly bracket at the end (or missing at the end of the while line)