public abstract class CustomUrlCodec extends Object
Utility for encoding and decoding strings so that they can safely be used as part of a URL.
This utility can be used to encode a string that shall be used as part of a URL. After encoding, the string does not contain any characters that carry a special meaning in a URL path or query string.
When being encoded, a string is first converted to its byte representation.
Then, only the letters A
–Z
and a
–z
, the digits 0
–9
, the
minus sign (-
), and the underscore (_
) are kept as
is.
All other bytes are encoded in the resulting string by a tilde character(
~
) followed by two hexadecimal digits.
The encoding used by this class is very similar to the general encoding used for URLs, but there are a few important differences:
%
), the tilde character (
~
) is used for escaping. This has the advantage that no
double-escaping will occur if the encoded string is used as part of a URL
that is then encoded again./
) and the semicolon (;
) are encoded. This way, encoded strings
can safely be used as part of a URL path.When decoding, this utility assumes that the encoded string only contains characters from the US-ASCII character set (like a correctly encoded string does). Other characters are mostly likely lost during the decoding process.
Constructor and Description |
---|
CustomUrlCodec() |
Modifier and Type | Method and Description |
---|---|
static String |
decode(String encodedString)
Decodes an encoded string using the UTF-8 character set.
|
static String |
decode(String encodedString,
Charset charset)
Decodes an encoded string using the specified character set.
|
static String |
decode(String encodedString,
String encoding)
Decodes an encoded string using the specified encoding.
|
static String |
encode(String unencodedString)
Encodes the specified string using the UTF-8 character set.
|
static String |
encode(String unencodedString,
Charset charset)
Encoded the specified string using the specified character set.
|
static String |
encode(String unencodedString,
String encoding)
Encodes the specified string using the specified encoding.
|
public static String decode(String encodedString)
encodedString
- encoded string. Typically, this string will have been
generated by calling the encode(String)
method.NullPointerException
- if encodedString
is null
.decode(String, Charset)
,
encode(String)
public static String decode(String encodedString, String encoding)
encodedString
- encoded string. Typically, this string will have been
generated by calling the encode(String, String)
method, using the same encoding
.encoding
- encoding that is used for converting the resulting byte array
back to a string (e.g. "UTF-8").IllegalArgumentException
- if encoding
is null
.IllegalCharsetNameException
- if the specified encoding
is not valid.NullPointerException
- if encodedString
is null
.UnsupportedCharsetException
- if the specified encoding
is not supported by
the JVM.decode(String, Charset)
,
encode(String, String)
public static String decode(String encodedString, Charset charset)
encodedString
- encoded string. Typically, this string will have been
generated by calling the encode(String, Charset)
method, using the same charset
.charset
- character set that is used for converting the resulting byte
array back to a string.NullPointerException
- if charset
or encodedString
is
null
.encode(String, Charset)
public static String encode(String unencodedString)
unencodedString
- string to be encoded.decode(String)
.NullPointerException
- if unencodedString
is null
.decode(String)
,
encode(String, Charset)
public static String encode(String unencodedString, String encoding)
unencodedString
- string to be encoded.encoding
- encoding that is used for converting the string to a byte
array (e.g. "UTF-8").decode(String, String)
.IllegalArgumentException
- if encoding
is null
.IllegalCharsetNameException
- if the specified encoding
is not valid.NullPointerException
- if unencodedString
is null
.UnsupportedCharsetException
- if the specified encoding
is not supported by
the JVM.decode(String, String)
,
encode(String, Charset)
public static String encode(String unencodedString, Charset charset)
unencodedString
- string to be encoded.charset
- character set that is used for converting the string to a byte
array.decode(String, Charset)
.NullPointerException
- if charset
or unencodedString
is
null
.decode(String, Charset)
Copyright © 2011–2016 aquenos GmbH. All rights reserved.