Java Byte Array to String by Charset convertToChars(final byte[] bytes, final Charset charset)

Here you can find the source of convertToChars(final byte[] bytes, final Charset charset)

Description

convert To Chars

License

Open Source License

Declaration

public static char[] convertToChars(final byte[] bytes, final 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 {
    public static char[] convertToChars(final byte[] bytes, final Charset charset) {
        ByteBuffer bb = ByteBuffer.allocate(bytes.length);
        bb.put(bytes);/*w  w  w.ja  va 2  s  .c  o m*/
        bb.flip();
        CharBuffer cb = charset.decode(bb);
        return cb.array();
    }

    public static char[] convertToChars(final byte[] bytes, final String charsetName) {
        return convertToChars(bytes, Charset.forName(charsetName));
    }

    public static char[] convertToChars(final byte[] bytes) {
        return convertToChars(bytes, Charset.defaultCharset());
    }

    public static char[] convertToChars(final String str) {
        return str.toCharArray();
    }
}

Related

  1. changeEncoding(byte[] byteArray, Charset charsetFrom, Charset charsetTo)
  2. charSequence2Bytes(CharSequence sb, Charset charset)
  3. charSetForBOM(byte[] buffer, int offset)
  4. convertCharset(byte[] content, Charset fromCharset, Charset toCharset)
  5. convertToBytes(final String str, final Charset charset)
  6. convertToString(byte[] bytes, Charset charset)
  7. convertToString(final byte[] bytes, final Charset charset)
  8. decode(CharsetDecoder decoder, byte[] in)
  9. decode(final byte[] binaryValue, final Charset charset)