Package wt.util
Class EncodingConverter
java.lang.Object
java.io.OutputStream
java.io.ByteArrayOutputStream
wt.util.EncodingConverter
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
- Direct Known Subclasses:
EncodingConverter
The class contains utility methods for converting a
Supported API: true
Extendable: false
String into a MIME format called
"x-www-form-urlencoded" and vice versa.
To convert a String, each character is examined in turn:
- The ASCII characters '
a' through 'z', 'A' through 'Z', and '0' through '9' remain the same. - The space character '
' is converted into a plus sign '+'. - All other characters are converted into an 8-bit byte sequence using
a character encoding and the resulting bytes are each represented as a
3-character string "
%xy", where xy is the two-digit hexadecimal representation of the byte.
Deployment Notes:
-
The EncodingConverter is a replacement for WTURLEncoder and is instance
based. The EncodingConverter should not be shared amongst threads or
used as a static instance unless appropriate synchronization blocks are
used to protect the internal buffers from overrighting. This can be done
one of two ways. Firstly by declaring the method which calls the
encode/decode methods as synchronized, or by placing a syncrhonized block
around the actually calls to encode/decode as shown below.
static EncodingConverter staticEncoder = new EncodingConverter();
public void sampleMethod(String s)
{
synchronized( staticEncoder )
}
{
staticEncoder.encode(s);
}
Supported API: true
Extendable: false
-
Field Summary
Fields inherited from class java.io.ByteArrayOutputStream
buf, count -
Constructor Summary
ConstructorsConstructorDescriptionConsturctor to create a new EncodingConverter with the encoding set to UTF-8.EncodingConverter(int ByteSize) Constructor to create a new EncodingConverter with an encoding set to UTF-8 and a specified buffer capacity.EncodingConverter(String encoding) Constructor to create a new EncodingConverter with a specified encoding. -
Method Summary
Modifier and TypeMethodDescriptionTranslates a string fromx-www-form-urlencodedformat back into a string.Translates a string fromx-www-form-urlencodedformat back the original string.static final StringdecodeBytes(String str) Convience method to convert the bytes of a String in ISO-8859_1 encoding to UTF-8 encoding.static final StringdecodeBytes(String str, String enc) Convience method to convert the bytes of a String in ISO-8859_1 encoding to a designated encoding.static final StringdecodeBytes(String str, String from_enc, String to_enc) Convience method to convert the bytes of a String in an input encoding encoding to a designated encoding.Translates a string intox-www-form-urlencodedformat.voidencode(StringBuffer buf, String s) Translates a string intox-www-form-urlencodedformat.voidencode(StringBuffer buf, String s, String encoding) Translates a string intox-www-form-urlencodedformat.Translates a string intox-www-form-urlencodedformat.static StringEscape special characters in accordance with URL path segment encoding requirements.Methods inherited from class java.io.ByteArrayOutputStream
close, reset, size, toByteArray, toString, toString, toString, toString, write, write, writeBytes, writeToMethods inherited from class java.io.OutputStream
flush, nullOutputStream, write
-
Constructor Details
-
EncodingConverter
public EncodingConverter()Consturctor to create a new EncodingConverter with the encoding set to UTF-8.
Supported API: true -
EncodingConverter
public EncodingConverter(int ByteSize) Constructor to create a new EncodingConverter with an encoding set to UTF-8 and a specified buffer capacity.
Supported API: true- Parameters:
ByteSize- The buffer capacity.
-
EncodingConverter
Constructor to create a new EncodingConverter with a specified encoding.
Supported API: true- Parameters:
encoding- The encoding to use for encode/decoding.
-
-
Method Details
-
encode
Translates a string intox-www-form-urlencodedformat. Hex escaped characters are first encoded using UTF8 character encoding before Hex escaping the resulting bytes. This produces an encoded string that can be decoded without loss of data.
Thread Safe: true (not if shared between thread)
Supported API: true- Parameters:
s-Stringto be translated.- Returns:
- the translated
String.
-
encode
Translates a string intox-www-form-urlencodedformat. Hex escaped characters are first encoded using the given character encoding before Hex escaping the resulting bytes.
Thread Safe: true (not if shared between thread)
Supported API: true- Parameters:
s-Stringto be translated.encoding- the character encoding name- Returns:
- the translated
String, or "" if the string s was null.
-
encode
Translates a string intox-www-form-urlencodedformat. Hex escaped characters are first encoded using UTF8 character encoding before Hex escaping the resulting bytes. This produces an encoded string that can be decoded without loss of data.
Thread Safe: true (not if shared between thread)
Supported API: true- Parameters:
buf-StringBufferto receive encoded characters.s-Stringto be translated.
-
encode
Translates a string intox-www-form-urlencodedformat. Hex escaped characters are first encoded using the given character encoding before Hex escaping the resulting bytes.
Thread Safe: true (not if shared between thread)
Supported API: true- Parameters:
buf-StringBufferto receive encoded characters.s-Stringto be translated.encoding- the character encoding name
-
escape
Escape special characters in accordance with URL path segment encoding requirements. Replaces spaces with %20 and double quotes with %22. In theory, all special characters should be URLEncoded, but the 4.x browsers don't interpret the file name correctly using their native character encoding when that is done.
Supported API: true- Parameters:
str-Stringto escape.- Returns:
- Escaped string value.
-
decode
Translates a string fromx-www-form-urlencodedformat back into a string. Hex escaped bytes are converted and the resulting sequence of 8-bit values is converted to characters using UTF8 encoding. This produces a decoded string from the results ofencodewithout loss data.
Thread Safe: true (not if shared between thread)
Supported API: true- Parameters:
s-Stringto be translated.- Returns:
- the translated
String.
-
decode
Translates a string fromx-www-form-urlencodedformat back the original string. Hex escaped bytes are converted and the resulting sequence of 8-bit values is converted to characters using the given character encoding.
Thread Safe: true (not if shared between thread)
Supported API: true- Parameters:
encoded-Stringto be translated.encoding- the character encoding name- Returns:
- the translated
Stringor "" if encoded is null.
-
decodeBytes
Convience method to convert the bytes of a String in ISO-8859_1 encoding to UTF-8 encoding. This should typically be called after a JSP request.getParameter( ) call.
Supported API: true- Parameters:
str- The string to re-encode for the correct bytes.- Returns:
- String the decoded and translated String.
- Throws:
UnsupportedEncodingException
-
decodeBytes
Convience method to convert the bytes of a String in ISO-8859_1 encoding to a designated encoding. This should typically be called after a JSP request.getParameter( ) call.
Supported API: true- Parameters:
str- The string to re-encode for the correct bytes.enc- The encoding to translate to.- Returns:
- String the decoded and translated String.
- Throws:
UnsupportedEncodingException
-
decodeBytes
public static final String decodeBytes(String str, String from_enc, String to_enc) throws UnsupportedEncodingException Convience method to convert the bytes of a String in an input encoding encoding to a designated encoding. This should typically be called after a JSP request.getParameter( ) call.
Supported API: true- Parameters:
str- The string to re-encode for the correct bytes.from_enc- The encoding coming from.to_enc- The encoding to translate to.- Returns:
- String the decoded and translated String.
- Throws:
UnsupportedEncodingException
-