Java ByteBuffer to String toString(ByteBuffer buffer)

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

Description

Converts the contents of the specified byte buffer to a string, which is formatted similarly to the output of the Arrays#toString() method.

License

Open Source License

Parameter

Parameter Description
buffer The buffer.

Return

The string.

Declaration

public static String toString(ByteBuffer buffer) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.nio.ByteBuffer;

public class Main {
    /**//from   www .  jav a 2s  .c  o m
     * Converts the contents of the specified byte buffer to a string, which is
     * formatted similarly to the output of the {@link Arrays#toString()}
     * method.
     * @param buffer The buffer.
     * @return The string.
     */
    public static String toString(ByteBuffer buffer) {
        StringBuilder builder = new StringBuilder("[");
        for (int i = 0; i < buffer.limit(); i++) {
            String hex = Integer.toHexString(buffer.get(i) & 0xFF).toUpperCase();
            if (hex.length() == 1)
                hex = "0" + hex;

            builder.append("0x").append(hex);
            if (i != buffer.limit() - 1) {
                builder.append(", ");
            }
        }
        builder.append("]");
        return builder.toString();
    }
}

Related

  1. toString(ByteBuffer buf, int len)
  2. toString(ByteBuffer buffer)
  3. toString(ByteBuffer buffer)
  4. toString(ByteBuffer buffer)
  5. toString(ByteBuffer buffer)
  6. toString(ByteBuffer buffer)
  7. toString(ByteBuffer buffer)
  8. toString(ByteBuffer buffer, Charset charset)
  9. toString(ByteBuffer buffer, int offset, int length)