code: public static byte[] string2Bytes(String str) {
      try {
        final int len = str.length();
        // if we can, we will use it to return the bytes
        byte[] rawBytes = new byte[len];
        
        // get all chars of the given string
        char[] charArray = UTF8.getCharArray(len);
        str.getChars(0, len, charArray, 0);
        
        for (int i = 0; i < len; i++) {
          if (charArray[i] > UTF8.MAX_ASCII_CODE) {
            // non-ASCII chars present
            // do expensive conversion
            return str.getBytes(utf8charsetName);
          }
          // copy to output array
          rawBytes[i] = (byte) charArray[i];
        }
        // only ASCII present - return raw bytes
        return rawBytes;
      } catch (UnsupportedEncodingException e) {
        assert false : "UTF8 encoding is not supported ";
      }
      return null;
    }
docs: Converts a string to a byte array using UTF8 encoding.