Top Description Constructors Methods
org.apache.tomcat.util.codec.binary

public Class StringUtils

extends Object
Class Inheritance
Imports
java.nio.charset.Charset, .StandardCharsets

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

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private static byte[]

Returns:

the encoded bytes
getBytes
(final String
The string to encode (if null, return null).
string
,
final Charset
The Charset to encode the String
charset
)

Calls String#getBytes(Charset)

public static byte[]

Returns:

encoded bytes, or null if the input string was null
getBytesUtf8
(final String
the String to encode, may be null
string
)

Encodes the given string into a sequence of bytes using the UTF-8 charset, storing the result into a new byte array.

private static String

Returns:

A new String decoded from the specified array of bytes using the given charset, or null if the input byte array was null.
newString
(final byte[]
The bytes to be decoded into characters
bytes
,
final Charset
The Charset to encode the String; not null
charset
)

Constructs a new String by decoding the specified array of bytes using the given charset.

public static String

Returns:

A new String decoded from the specified array of bytes using the US-ASCII charset, or null if the input byte array was null.
newStringUsAscii
(final byte[]
The bytes to be decoded into characters
bytes
)

Constructs a new String by decoding the specified array of bytes using the US-ASCII charset.

public static String

Returns:

A new String decoded from the specified array of bytes using the UTF-8 charset, or null if the input byte array was null.
newStringUtf8
(final byte[]
The bytes to be decoded into characters
bytes
)

Constructs a new String by decoding the specified array of bytes using the UTF-8 charset.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

StringUtilsback to summary
public StringUtils()

Method Detail

getBytesback to summary
private static byte[] getBytes(final String string, final Charset charset)

Calls String#getBytes(Charset)

Parameters
string:String

The string to encode (if null, return null).

charset:Charset

The Charset to encode the String

Returns:byte[]

the encoded bytes

getBytesUtf8back to summary
public static byte[] getBytesUtf8(final 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:String

the String to encode, may be null

Returns:byte[]

encoded bytes, or null if the input string was null

Exceptions
NullPointerException:
Thrown if StandardCharsets#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
newStringback to summary
private static String newString(final byte[] bytes, final Charset charset)

Constructs a new String by decoding the specified array of bytes using the given charset.

Parameters
bytes:byte[]

The bytes to be decoded into characters

charset:Charset

The Charset to encode the String; not null

Returns:String

A new String decoded from the specified array of bytes using the given charset, or null if the input byte array was null.

Exceptions
NullPointerException:
Thrown if charset is null
newStringUsAsciiback to summary
public static String newStringUsAscii(final byte[] bytes)

Constructs a new String by decoding the specified array of bytes using the US-ASCII charset.

Parameters
bytes:byte[]

The bytes to be decoded into characters

Returns:String

A new String decoded from the specified array of bytes using the US-ASCII charset, or null if the input byte array was null.

Exceptions
NullPointerException:
Thrown if StandardCharsets#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
newStringUtf8back to summary
public static String newStringUtf8(final byte[] bytes)

Constructs a new String by decoding the specified array of bytes using the UTF-8 charset.

Parameters
bytes:byte[]

The bytes to be decoded into characters

Returns:String

A new String decoded from the specified array of bytes using the UTF-8 charset, or null if the input byte array was null.

Exceptions
NullPointerException:
Thrown if StandardCharsets#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