Convert whole ByteBuffer to UTF-8 String. - Java java.nio

Java examples for java.nio:ByteBuffer Convert

Description

Convert whole ByteBuffer to UTF-8 String.

Demo Code

/*/*from  w  w w  .  java 2s.c  o  m*/
 * WireSpider
 *
 * Copyright (c) 2015 kazyx
 *
 * This software is released under the MIT License.
 * http://opensource.org/licenses/mit-license.php
 */
//package com.java2s;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;

public class Main {
    private static final Charset UTF8 = Charset.forName("UTF-8");

    /**
     * Convert whole byte buffer to UTF-8 String.
     *
     * @param bytes Source byte buffer.
     * @return String expression of the bytes.
     */
    public static String toTextAll(ByteBuffer bytes) {
        return new String(bytes.array(), UTF8);
    }
}

Related Tutorials