Java ByteBuffer to String byteBufferToString(ByteBuffer buffer)

Here you can find the source of byteBufferToString(ByteBuffer buffer)

Description

Takes a ByteBuffer in read mode and converts it to a comma separated string of Hex encoded bytes.

License

Apache License

Parameter

Parameter Description
buffer - Byte Data Buffer

Return

A string representation of the data

Declaration

public static String byteBufferToString(ByteBuffer buffer) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.nio.ByteBuffer;

public class Main {
    /**/*from ww  w. j a  va2 s  .  com*/
     * Takes a ByteBuffer in read mode and converts it to a
     * comma separated string of Hex encoded bytes. This is a
     * read only operation. When this operation is complete the
     * ByteBuffer will be rewound.
     * @param buffer - Byte Data Buffer
     * @return A string representation of the data
     */
    public static String byteBufferToString(ByteBuffer buffer) {
        StringBuilder builder = new StringBuilder(buffer.remaining() * 2);

        while (buffer.hasRemaining()) {
            String formatted = String.format("%02X",
                    Byte.valueOf(buffer.get()));
            builder.append(formatted).append(',');
        }

        buffer.rewind();

        return builder.toString();
    }
}

Related

  1. byteBufferToString(ByteBuffer buf)
  2. byteBufferToString(ByteBuffer buffer)
  3. byteBufferToString(ByteBuffer buffer)
  4. byteBufferToString(ByteBuffer buffer)
  5. byteBufferToString(ByteBuffer buffer)
  6. ByteBufferToString(ByteBuffer buffer, Charset charset)
  7. byteBufferToString(ByteBuffer buffer, String encoding)
  8. byteBufferToString(ByteBuffer stringBuf)
  9. bytesToString(ByteBuffer buf, int off, int len)