Java ByteBuffer to String toString(final ByteBuffer buffer)

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

Description

Converts a byte buffer into a string in the UTF-8 character set.

License

Open Source License

Parameter

Parameter Description
buffer Byte buffer to convert.

Return

UTF-8 string representation of bytes.

Declaration

public static String toString(final ByteBuffer buffer) 

Method Source Code

//package com.java2s;
/* See LICENSE for licensing and NOTICE for copyright. */

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

public class Main {
    /** Default character set for bytes is UTF-8. */
    public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");

    /**//from  w  ww  .  j ava2s. c o  m
     * Converts a byte array into a string in the UTF-8 character set.
     *
     * @param  bytes  Byte array to convert.
     *
     * @return  UTF-8 string representation of bytes.
     */
    public static String toString(final byte[] bytes) {
        return new String(bytes, DEFAULT_CHARSET);
    }

    /**
     * Converts a byte buffer into a string in the UTF-8 character set.
     *
     * @param  buffer  Byte buffer to convert.
     *
     * @return  UTF-8 string representation of bytes.
     */
    public static String toString(final ByteBuffer buffer) {
        return toCharBuffer(buffer).toString();
    }

    /**
     * Converts a byte buffer into a character buffer.
     *
     * @param  buffer  Byte buffer to convert.
     *
     * @return  Character buffer containing UTF-8 string representation of bytes.
     */
    public static CharBuffer toCharBuffer(final ByteBuffer buffer) {
        return DEFAULT_CHARSET.decode(buffer);
    }
}

Related

  1. toString(ByteBuffer bytes)
  2. toString(ByteBuffer sequence)
  3. toString(ByteBuffer value, String charsetName)
  4. toString(final ByteBuffer buffer)
  5. toString(final ByteBuffer buffer)
  6. toStringBinary(ByteBuffer buf)
  7. toStringBinary(ByteBuffer buf)
  8. toText(ByteBuffer data, StringBuilder result, int cnt)
  9. toTextAll(ByteBuffer bytes)