org.apache.commons.codec.net

Class URLCodec

public class URLCodec extends Object implements BinaryEncoder, BinaryDecoder, StringEncoder, StringDecoder

Implements the 'www-form-urlencoded' encoding scheme, also misleadingly known as URL encoding.

For more detailed information please refer to Chapter 17.13.4 'Form content types' of the HTML 4.01 Specification

This codec is meant to be a replacement for standard Java classes java.net.URLEncoder and java.net.URLDecoder on older Java platforms, as these classes in Java versions below 1.4 rely on the platform's default charset encoding.

Since: 1.2

Version: $Id: URLCodec.java 130337 2004-03-29 07:59:00Z ggregory $

Author: Apache Software Foundation

Field Summary
protected Stringcharset
The default charset used for string decoding and encoding.
protected static byteESCAPE_CHAR
protected static BitSetWWW_FORM_URL
BitSet of www-form-url safe characters.
Constructor Summary
URLCodec()
Default constructor.
URLCodec(String charset)
Constructor which allows for the selection of a default charset
Method Summary
byte[]decode(byte[] bytes)
Decodes an array of URL safe 7-bit characters into an array of original bytes.
Stringdecode(String pString, String charset)
Decodes a URL safe string into its original form using the specified encoding.
Stringdecode(String pString)
Decodes a URL safe string into its original form using the default string charset.
Objectdecode(Object pObject)
Decodes a URL safe object into its original form.
static byte[]decodeUrl(byte[] bytes)
Decodes an array of URL safe 7-bit characters into an array of original bytes.
byte[]encode(byte[] bytes)
Encodes an array of bytes into an array of URL safe 7-bit characters.
Stringencode(String pString, String charset)
Encodes a string into its URL safe form using the specified string charset.
Stringencode(String pString)
Encodes a string into its URL safe form using the default string charset.
Objectencode(Object pObject)
Encodes an object into its URL safe form.
static byte[]encodeUrl(BitSet urlsafe, byte[] bytes)
Encodes an array of bytes into an array of URL safe 7-bit characters.
StringgetDefaultCharset()
The default charset used for string decoding and encoding.
StringgetEncoding()
The String encoding used for decoding and encoding.

Field Detail

charset

protected String charset
The default charset used for string decoding and encoding.

ESCAPE_CHAR

protected static byte ESCAPE_CHAR

WWW_FORM_URL

protected static final BitSet WWW_FORM_URL
BitSet of www-form-url safe characters.

Constructor Detail

URLCodec

public URLCodec()
Default constructor.

URLCodec

public URLCodec(String charset)
Constructor which allows for the selection of a default charset

Parameters: charset the default string charset to use.

Method Detail

decode

public byte[] decode(byte[] bytes)
Decodes an array of URL safe 7-bit characters into an array of original bytes. Escaped characters are converted back to their original representation.

Parameters: bytes array of URL safe characters

Returns: array of original bytes

Throws: DecoderException Thrown if URL decoding is unsuccessful

decode

public String decode(String pString, String charset)
Decodes a URL safe string into its original form using the specified encoding. Escaped characters are converted back to their original representation.

Parameters: pString URL safe string to convert into its original form charset the original string charset

Returns: original string

Throws: DecoderException Thrown if URL decoding is unsuccessful UnsupportedEncodingException Thrown if charset is not supported

decode

public String decode(String pString)
Decodes a URL safe string into its original form using the default string charset. Escaped characters are converted back to their original representation.

Parameters: pString URL safe string to convert into its original form

Returns: original string

Throws: DecoderException Thrown if URL decoding is unsuccessful

See Also: getDefaultCharset

decode

public Object decode(Object pObject)
Decodes a URL safe object into its original form. Escaped characters are converted back to their original representation.

Parameters: pObject URL safe object to convert into its original form

Returns: original object

Throws: DecoderException Thrown if URL decoding is not applicable to objects of this type if decoding is unsuccessful

decodeUrl

public static final byte[] decodeUrl(byte[] bytes)
Decodes an array of URL safe 7-bit characters into an array of original bytes. Escaped characters are converted back to their original representation.

Parameters: bytes array of URL safe characters

Returns: array of original bytes

Throws: DecoderException Thrown if URL decoding is unsuccessful

encode

public byte[] encode(byte[] bytes)
Encodes an array of bytes into an array of URL safe 7-bit characters. Unsafe characters are escaped.

Parameters: bytes array of bytes to convert to URL safe characters

Returns: array of bytes containing URL safe characters

encode

public String encode(String pString, String charset)
Encodes a string into its URL safe form using the specified string charset. Unsafe characters are escaped.

Parameters: pString string to convert to a URL safe form charset the charset for pString

Returns: URL safe string

Throws: UnsupportedEncodingException Thrown if charset is not supported

encode

public String encode(String pString)
Encodes a string into its URL safe form using the default string charset. Unsafe characters are escaped.

Parameters: pString string to convert to a URL safe form

Returns: URL safe string

Throws: EncoderException Thrown if URL encoding is unsuccessful

See Also: getDefaultCharset

encode

public Object encode(Object pObject)
Encodes an object into its URL safe form. Unsafe characters are escaped.

Parameters: pObject string to convert to a URL safe form

Returns: URL safe object

Throws: EncoderException Thrown if URL encoding is not applicable to objects of this type or if encoding is unsuccessful

encodeUrl

public static final byte[] encodeUrl(BitSet urlsafe, byte[] bytes)
Encodes an array of bytes into an array of URL safe 7-bit characters. Unsafe characters are escaped.

Parameters: urlsafe bitset of characters deemed URL safe bytes array of bytes to convert to URL safe characters

Returns: array of bytes containing URL safe characters

getDefaultCharset

public String getDefaultCharset()
The default charset used for string decoding and encoding.

Returns: the default string charset.

getEncoding

public String getEncoding()

Deprecated: use #getDefaultCharset()

The String encoding used for decoding and encoding.

Returns: Returns the encoding.

commons-codec version 1.3 - Copyright © 2002-2004 - Apache Software Foundation