Java ByteBuffer to String toString(final ByteBuffer buffer)

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

Description

Convert ByteBuffer to string, assuming UTF-8 encoding in the buffer.

License

Open Source License

Parameter

Parameter Description
buffer buffer to convert

Return

String representation of ByteBuffer (UTF-8 encoding)

Declaration

public static String toString(final ByteBuffer buffer) 

Method Source Code


//package com.java2s;
/*// ww  w . j  a va2s. c  o m
 * jndn-management
 * Copyright (c) 2015-2016, Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU Lesser General Public License,
 * version 3, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
 * more details.
 */

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

public class Main {
    /**
     * Convert ByteBuffer to string, assuming UTF-8 encoding in the buffer.
     *
     * @param buffer buffer to convert
     * @return String representation of ByteBuffer (UTF-8 encoding)
     */
    public static String toString(final ByteBuffer buffer) {
        byte[] array = new byte[buffer.remaining()];
        buffer.get(array);
        return new String(array, Charset.forName("UTF-8"));
    }
}

Related

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