wt.util
Class EncodingConverter

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.ByteArrayOutputStream
          extended by wt.util.EncodingConverter
All Implemented Interfaces:
Closeable, Flushable
Direct Known Subclasses:
EncodingConverter

public class EncodingConverter
extends ByteArrayOutputStream

The class contains utility methods for converting a String into a MIME format called "x-www-form-urlencoded" and vice versa.

To convert a String, each character is examined in turn:


Deployment Notes:



Supported API: true
Extendable: false


Field Summary
 
Fields inherited from class java.io.ByteArrayOutputStream
buf, count
 
Constructor Summary
EncodingConverter()
          Consturctor 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
 String decode(String s)
          Translates a string from x-www-form-urlencoded format back into a string.
 String decode(String encoded, String encoding)
          Translates a string from x-www-form-urlencoded format back the original string.
static String decodeBytes(String str)
          Convience method to convert the bytes of a String in ISO-8859_1 encoding to UTF-8 encoding.
static String decodeBytes(String str, String enc)
          Convience method to convert the bytes of a String in ISO-8859_1 encoding to a designated encoding.
static String decodeBytes(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.
 String encode(String s)
          Translates a string into x-www-form-urlencoded format.
 void encode(StringBuffer buf, String s)
          Translates a string into x-www-form-urlencoded format.
 void encode(StringBuffer buf, String s, String encoding)
          Translates a string into x-www-form-urlencoded format.
 String encode(String s, String encoding)
          Translates a string into x-www-form-urlencoded format.
static String escape(String str)
          Escape special characters in accordance with URL path segment encoding requirements.
 
Methods inherited from class java.io.ByteArrayOutputStream
close, reset, size, toByteArray, toString, toString, toString, write, write, writeTo
 
Methods inherited from class java.io.OutputStream
flush, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

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

public EncodingConverter(String encoding)
Constructor to create a new EncodingConverter with a specified encoding.

Supported API: true

Parameters:
encoding - The encoding to use for encode/decoding.
Method Detail

encode

public String encode(String s)
Translates a string into x-www-form-urlencoded format. 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 - String to be translated.
Returns:
the translated String.

encode

public String encode(String s,
                     String encoding)
Translates a string into x-www-form-urlencoded format. 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 - String to be translated.
encoding - the character encoding name
Returns:
the translated String, or "" if the string s was null.

encode

public void encode(StringBuffer buf,
                   String s)
Translates a string into x-www-form-urlencoded format. 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 - StringBuffer to receive encoded characters.
s - String to be translated.

encode

public void encode(StringBuffer buf,
                   String s,
                   String encoding)
Translates a string into x-www-form-urlencoded format. 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 - StringBuffer to receive encoded characters.
s - String to be translated.
encoding - the character encoding name

escape

public static String escape(String str)
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 - String to escape.
Returns:
Escaped string value.

decode

public String decode(String s)
Translates a string from x-www-form-urlencoded format 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 of encode without loss data.

Thread Safe: true (not if shared between thread)

Supported API: true

Parameters:
s - String to be translated.
Returns:
the translated String.

decode

public String decode(String encoded,
                     String encoding)
Translates a string from x-www-form-urlencoded format 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 - String to be translated.
encoding - the character encoding name
Returns:
the translated String or "" if encoded is null.

decodeBytes

public static final String decodeBytes(String str)
                                throws UnsupportedEncodingException
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

public static final String decodeBytes(String str,
                                       String enc)
                                throws UnsupportedEncodingException
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