Class StringUtils
- java.lang.Object
-
- org.apache.tomcat.util.codec.binary.StringUtils
-
public class StringUtils extends java.lang.Object
Converts String to and from bytes using the encodings required by the Java specification. These encodings are specified in Standard charsets.This class is immutable and thread-safe.
- Since:
- 1.4
- See Also:
- Standard charsets
-
-
Constructor Summary
Constructors Constructor Description StringUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static byte[]
getBytesUtf8(java.lang.String string)
Encodes the given string into a sequence of bytes using the UTF-8 charset, storing the result into a new byte array.static java.lang.String
newStringUsAscii(byte[] bytes)
Constructs a newString
by decoding the specified array of bytes using the US-ASCII charset.static java.lang.String
newStringUtf8(byte[] bytes)
Constructs a newString
by decoding the specified array of bytes using the UTF-8 charset.
-
-
-
Method Detail
-
getBytesUtf8
public static byte[] getBytesUtf8(java.lang.String string)
Encodes the given string into a sequence of bytes using the UTF-8 charset, storing the result into a new byte array.- Parameters:
string
- the String to encode, may benull
- Returns:
- encoded bytes, or
null
if the input string wasnull
- Throws:
java.lang.NullPointerException
- Thrown ifStandardCharsets.UTF_8
is not initialized, which should never happen since it is required by the Java platform specification.- Since:
- As of 1.7, throws
NullPointerException
instead of UnsupportedEncodingException - See Also:
- Standard charsets
-
newStringUsAscii
public static java.lang.String newStringUsAscii(byte[] bytes)
Constructs a newString
by decoding the specified array of bytes using the US-ASCII charset.- Parameters:
bytes
- The bytes to be decoded into characters- Returns:
- A new
String
decoded from the specified array of bytes using the US-ASCII charset, ornull
if the input byte array wasnull
. - Throws:
java.lang.NullPointerException
- Thrown ifStandardCharsets.US_ASCII
is not initialized, which should never happen since it is required by the Java platform specification.- Since:
- As of 1.7, throws
NullPointerException
instead of UnsupportedEncodingException
-
newStringUtf8
public static java.lang.String newStringUtf8(byte[] bytes)
Constructs a newString
by decoding the specified array of bytes using the UTF-8 charset.- Parameters:
bytes
- The bytes to be decoded into characters- Returns:
- A new
String
decoded from the specified array of bytes using the UTF-8 charset, ornull
if the input byte array wasnull
. - Throws:
java.lang.NullPointerException
- Thrown ifStandardCharsets.UTF_8
is not initialized, which should never happen since it is required by the Java platform specification.- Since:
- As of 1.7, throws
NullPointerException
instead of UnsupportedEncodingException
-
-