Java ByteBuffer to String toString(ByteBuffer buffer, Charset charset)

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

Description

to String

License

Open Source License

Parameter

Parameter Description
buffer The buffer to convert to a string.
charset The charset to use when converting bytes to characters.

Return

A string representation of buffer's content.

Declaration

public static String toString(ByteBuffer buffer, Charset charset) 

Method Source Code


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

import java.nio.ByteBuffer;
import java.nio.CharBuffer;

import java.nio.charset.Charset;

public class Main {
    /**/*from  ww  w.  j  a va2  s  . c o m*/
     * @param buffer The buffer to convert to a string.
     * @param charset The charset to use when converting bytes to characters.
     * @return A string representation of buffer's content.
     */
    public static String toString(ByteBuffer buffer, Charset charset) {
        buffer = buffer.slice();
        byte[] buf = new byte[buffer.remaining()];
        buffer.get(buf);
        return new String(buf, charset);
    }

    public static String toString(CharBuffer buffer) {
        buffer = buffer.slice();
        char[] buf = new char[buffer.remaining()];
        buffer.get(buf);
        return new String(buf);
    }
}

Related

  1. toString(ByteBuffer buffer)
  2. toString(ByteBuffer buffer)
  3. toString(ByteBuffer buffer)
  4. toString(ByteBuffer buffer)
  5. toString(ByteBuffer buffer)
  6. toString(ByteBuffer buffer, int offset, int length)
  7. toString(ByteBuffer buffer, String encoding)
  8. toString(ByteBuffer bytes)
  9. toString(ByteBuffer bytes)