Here you can find the source of toChars(byte[] bytes)
public static char[] toChars(byte[] bytes)
//package com.java2s; //License from project: Open Source License import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.StandardCharsets; import java.util.Arrays; public class Main { public static char[] toChars(byte[] bytes) { ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); CharBuffer charBuffer = StandardCharsets.UTF_8.decode(byteBuffer); char[] chars = Arrays.copyOfRange(charBuffer.array(), charBuffer.position(), charBuffer.limit()); Arrays.fill(byteBuffer.array(), (byte) 0); Arrays.fill(charBuffer.array(), '\u0000'); return chars; }// w ww . j ava 2s . c o m }