如何从ByteBuffer中读取数据

san*_*_bl 4 java bytebuffer

如何读取ByteBuffer中存储的数据?

  • setValue()- 获取值“12 10”并转换为十六进制值并存储在String[]数据中。
  • write()- 将数据转换为字节并存储在ByteBuffer dest.
  • readBuffer- 我如何从中读取数据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?
}
Run Code Online (Sandbox Code Playgroud)

Sar*_*ari 6

destValue.rewind() 
while (destValue.hasRemaining())
     System.out.println((char)destValue.get());
}
Run Code Online (Sandbox Code Playgroud)