Java ByteBuffer to String toString(ByteBuffer buffer, String encoding)

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

Description

to String

License

Open Source License

Declaration

public static String toString(ByteBuffer buffer, String encoding) 

Method Source Code

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

import java.io.*;

import java.nio.ByteBuffer;

public class Main {
    public static String toString(ByteBuffer buffer, String encoding) {
        byte[] bytes = new byte[buffer.remaining()];
        buffer.get(bytes);/* ww w  . j a  v  a  2 s .  c  om*/
        return fromBytes(bytes, encoding);
    }

    public static String fromBytes(byte[] b) {
        return fromBytes(b, "UTF-8");
    }

    public static String fromBytes(byte[] b, String encoding) {
        try {
            return new String(b, encoding);
        } catch (UnsupportedEncodingException e) {
            return new String(b);
        }
    }
}

Related

  1. toString(ByteBuffer buffer)
  2. toString(ByteBuffer buffer)
  3. toString(ByteBuffer buffer)
  4. toString(ByteBuffer buffer, Charset charset)
  5. toString(ByteBuffer buffer, int offset, int length)
  6. toString(ByteBuffer bytes)
  7. toString(ByteBuffer bytes)
  8. toString(ByteBuffer sequence)
  9. toString(ByteBuffer value, String charsetName)