private byte[] toByteArray(char[] chars, Charset charset) {
        assert chars != null;

        CharBuffer charBuffer = CharBuffer.wrap(chars);
        ByteBuffer byteBuffer = charset.encode(charBuffer);
        byte[] bytes = Arrays.copyOfRange(byteBuffer.array(),
                byteBuffer.position(), byteBuffer.limit());
        Arrays.fill(byteBuffer.array(), (byte) 0); // clear sensitive data
        return bytes;
    }
private byte [ ] toByteArray ( char [ ] chars , Charset charset ) { assert chars != null ; CharBuffer charBuffer = CharBuffer . wrap ( chars ) ; ByteBuffer byteBuffer = charset . encode ( charBuffer ) ; byte [ ] bytes = Arrays . copyOfRange ( byteBuffer . array ( ) , byteBuffer . position ( ) , byteBuffer . limit ( ) ) ; Arrays . fill ( byteBuffer . array ( ) , ( byte ) 0 ) ; return bytes ; }
Converts the given char array to a UTF-8 encoded byte array.

@param chars   the char array to convert
@param charset Charset of the password
@return UTF-8 encoded byte array