protected byte[] toBytes(char[] chars) {
    if (chars == null) {
      return null;
    } else {
      CharBuffer charBuffer = CharBuffer.wrap(chars);
      ByteBuffer byteBuffer = Charset.forName("UTF-8").encode(charBuffer);

      byte[] bytes = Arrays.copyOfRange(byteBuffer.array(), byteBuffer.position(), byteBuffer.limit());

      // Clear sensitive data
      Arrays.fill(charBuffer.array(), '\u0000');
      Arrays.fill(byteBuffer.array(), (byte) 0);

      return bytes;
    }
  }
Converts an array of characters to an array of bytes by encoding each character into UTF-8 bytes.
<p/>
An attempt is made to clear out sensitive data by filling any buffers with 0's

@param chars the array of chars to convert
@return an array of bytes, or null if the original array was null
ambari-server/src/main/java/org/apache/ambari/server/security/encryption/AbstractCredentialStore.java