Convert a string to a UTF-8 encoded byte array.
@see String#getBytes(String)
553531
public static byte[] getBytes(String string) {
    byte[] result = new byte[utf8Length(string)];
    try {                                         // avoid sync'd allocations
      writeChars(result, string, 0, string.length());
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    return result;
  }
145804
function (str) {
                str = stringify(str);
                var byteArray = [];

                for (var i = 0, l = str.length, ch; i < l; i++) {
                    ch = str.charCodeAt(i);
                    byteArray.push(ch >> 8);
                    byteArray.push(ch & 0xFF);
                }

                return byteArray;
            }
563240
public static byte[] decode(String string) {
        int length = string.length();
        if (length == 0) return new byte[0];
        int pad = (string.charAt(length - 2) == '=') ? 2 :
                (string.charAt(length - 1) == '=') ? 1 : 0;
        int size = length * 3 / 4 - pad;
        byte[] buffer = new byte[size];
        int block;
        int i = 0;
        int index = 0;
        while (i < length) {
            block = (ALPHABET.indexOf(string.charAt(i++)) & 0xff) << 18 |
                    (ALPHABET.indexOf(string.charAt(i++)) & 0xff) << 12 |
                    (ALPHABET.indexOf(string.charAt(i++)) & 0xff) << 6 |
                    (ALPHABET.indexOf(string.charAt(i++)) & 0xff);
            buffer[index++] = (byte) (block >>> 16);
            if (index < size) buffer[index++] = (byte) ((block >>> 8) & 0xff);
            if (index < size) buffer[index++] = (byte) (block & 0xff);
        }
        return buffer;
    }
1346238
@Deprecated
    public static byte[] encodeToBytes(byte[] bytes)
    {
        try {
            char[] chars = Base64.encode(bytes);
            String string = new String(chars);
            return string.getBytes(DEFAULT_ENCODING);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }
527
public static byte[] str2bytes(String str) throws IOException {
		byte[] b=new byte[str.length()];
		for(int i=0; i<str.length(); ++i) {
			char c=str.charAt(i);	
			if(c>255) throw new UnsupportedEncodingException("string contained a char > 255, cannot convert to bytes");
			b[i]=(byte)c;
		}
		return b;
	}
1000422
public static UTF8String fromBytes(byte[] bytes) {
    if (bytes != null) {
      return new UTF8String(bytes, BYTE_ARRAY_OFFSET, bytes.length);
    } else {
      return null;
    }
  }
128742
public static byte[] getBytes(String text) {
		byte[] bytes = new byte[] {};
		try {
			bytes = text.getBytes("utf-8");
		} catch (UnsupportedEncodingException e) {
		}
		return bytes;
	}
444114
public static byte[] hexStringToBytes(String data) {
        if (data == null)
            return EMPTY_BYTE_ARRAY;
        if (data.startsWith("0x"))
            data = data.substring(2);
        return Hex.decode(data);
    }
97313
public static ByteBuffer fromUtf8String(CharSequence utfString) {
        if (utfString == null || utfString.length() == 0) {
            return Buffers.EMPTY_BYTE_BUFFER;
        } else {
            return ByteBuffer.wrap(utfString.toString().getBytes(StandardCharsets.UTF_8));
        }
    }
185177
protected static byte[] encodeString(String string) {
        Element element = getStringCache().get(string);
        byte[] encoded = (element == null ? null : (byte[]) element.getObjectValue());
        if (encoded == null) {
            ByteBuffer buf = AMF.CHARSET.encode(string);
            encoded = new byte[buf.limit()];
            buf.get(encoded);
            getStringCache().put(new Element(string, encoded));
        }
        return encoded;
    }
383714
public static byte[] decodeBytes(String str)
    {
        byte[] ret = null;

        try
        {
            // De-obfuscate the string
            if(str != null)
                ret = Base64.decodeBase64(str.getBytes());
        }
        catch(NoClassDefFoundError e)
        {
            ret = str.getBytes();
            System.out.println("WARNING: unable to decode: "
                +e.getClass().getName()+": "+e.getMessage());
        }

        return ret;
    }
297892
@Override
	public ByteArray encodeAsByteArray() {
		if(message.isEmpty()) return ByteArray.EMPTY_BYTE_ARRAY;

		return new ByteArray(message.getBytes(CHARSET));
	}
5851
public static byte[] decodeToBytes(CharSequence in) throws IOException{
		if(in == null) return null;
		ByteArrayOutputStream baos = new ByteArrayOutputStream(in.length());
		decode(in, baos);
		return baos.toByteArray();
	}
105338
final private ByteBuffer stringToByteBuffer(CharSequence s) {
        bb.clear();
        final int len = s.length();
        for (int i = 0; i < len; i++) {
            if (bb.remaining() < 3) {
                ByteBuffer n = ByteBuffer.allocate(bb.capacity() << 1);
                bb.flip();
                n.put(bb);
                bb = n;
            }
            char c = s.charAt(i);
            if (c < 0x80) {
                bb.put((byte) c);
            } else if (c < 0x800) {
                bb.put((byte) (0xc0 | (c >> 6)));
                bb.put((byte) (0x80 | (c & 0x3f)));
            } else {
                bb.put((byte) (0xe0 | (c >> 12)));
                bb.put((byte) (0x80 | ((c >> 6) & 0x3f)));
                bb.put((byte) (0x80 | (c & 0x3f)));
            }
        }
        bb.flip();
        return bb;
    }
771326
public static String bytesToString(byte[] bytes, int offset, int length) {
        return new String(bytes, offset, length, UTF8_CHARSET);
    }
413173
public static byte[] decodeToBytes(String encoded) {
    try {
      byte[] bytes = encoded.getBytes("UTF-8");
      Base64Decoder in = new Base64Decoder(new ByteArrayInputStream(bytes));

      ByteArrayOutputStream out = new ByteArrayOutputStream((int) (bytes.length * 0.67));

      try {
        byte[] buf = new byte[4 * 1024]; // 4K buffer
        int bytesRead;
        while ((bytesRead = in.read(buf)) != -1) {
          out.write(buf, 0, bytesRead);
        }
        out.close();

        return out.toByteArray();
      } catch (IOException ignored) {
        return null;
      }
    } catch (UnsupportedEncodingException ignored) {
      return null;
    }
  }
1025125
public static String getString(byte[] contentBytes, Charset charset, boolean isGetCharsetFromContent) throws IOException {
		if (null == contentBytes) {
			return null;
		}

		if (null == charset) {
			charset = CharsetUtil.CHARSET_UTF_8;
		}
		String content = new String(contentBytes, charset);
		if (isGetCharsetFromContent) {
			final String charsetInContentStr = ReUtil.get(META_CHARSET_PATTERN, content, 1);
			if (StrUtil.isNotBlank(charsetInContentStr)) {
				Charset charsetInContent = null;
				try {
					charsetInContent = Charset.forName(charsetInContentStr);
				} catch (Exception e) {
					if (StrUtil.containsIgnoreCase(charsetInContentStr, "utf-8") || StrUtil.containsIgnoreCase(charsetInContentStr, "utf8")) {
						charsetInContent = CharsetUtil.CHARSET_UTF_8;
					} else if (StrUtil.containsIgnoreCase(charsetInContentStr, "gbk")) {
						charsetInContent = CharsetUtil.CHARSET_GBK;
					}
					// ignore
				}
				if (null != charsetInContent && false == charset.equals(charsetInContent)) {
					content = new String(contentBytes, charsetInContent);
				}
			}
		}
		return content;
	}