FileDocCategorySizeDatePackage
URLCodec.javaAPI DocAndroid 1.5 API11967Wed May 06 22:41:10 BST 2009org.apache.commons.codec.net

URLCodec

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

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 {@link java.net.URLEncoder} and {@link java.net.URLDecoder} on older Java platforms, as these classes in Java versions below 1.4 rely on the platform's default charset encoding.

author
Apache Software Foundation
since
1.2
version
$Id: URLCodec.java,v 1.19 2004/03/29 07:59:00 ggregory Exp $

Fields Summary
protected String
charset
The default charset used for string decoding and encoding.
protected static byte
ESCAPE_CHAR
protected static final BitSet
WWW_FORM_URL
BitSet of www-form-url safe characters.
Constructors Summary
public URLCodec()
Default constructor.

    
    // Static initializer for www_form_url
     
        // alpha characters
        for (int i = 'a"; i <= 'z"; i++) {
            WWW_FORM_URL.set(i);
        }
        for (int i = 'A"; i <= 'Z"; i++) {
            WWW_FORM_URL.set(i);
        }
        // numeric characters
        for (int i = '0"; i <= '9"; i++) {
            WWW_FORM_URL.set(i);
        }
        // special chars
        WWW_FORM_URL.set('-");
        WWW_FORM_URL.set('_");
        WWW_FORM_URL.set('.");
        WWW_FORM_URL.set('*");
        // blank to be replaced with +
        WWW_FORM_URL.set(' ");
    
        super();
    
public URLCodec(String charset)
Constructor which allows for the selection of a default charset

param
charset the default string charset to use.

        super();
        this.charset = charset;
    
Methods Summary
public java.lang.Stringdecode(java.lang.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.

param
pString URL safe string to convert into its original form
return
original string
throws
DecoderException Thrown if URL decoding is unsuccessful
see
#getDefaultCharset()

        if (pString == null) {
            return null;
        }
        try {
            return decode(pString, getDefaultCharset());
        } catch(UnsupportedEncodingException e) {
            throw new DecoderException(e.getMessage());
        }
    
public java.lang.Objectdecode(java.lang.Object pObject)
Decodes a URL safe object into its original form. Escaped characters are converted back to their original representation.

param
pObject URL safe object to convert into its original form
return
original object
throws
DecoderException Thrown if URL decoding is not applicable to objects of this type if decoding is unsuccessful

        if (pObject == null) {
            return null;
        } else if (pObject instanceof byte[]) {
            return decode((byte[])pObject);
        } else if (pObject instanceof String) {
            return decode((String)pObject);
        } else {
            throw new DecoderException("Objects of type " +
                pObject.getClass().getName() + " cannot be URL decoded"); 
              
        }
    
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.

param
bytes array of URL safe characters
return
array of original bytes
throws
DecoderException Thrown if URL decoding is unsuccessful

        return decodeUrl(bytes);
    
public java.lang.Stringdecode(java.lang.String pString, java.lang.String charset)
Decodes a URL safe string into its original form using the specified encoding. Escaped characters are converted back to their original representation.

param
pString URL safe string to convert into its original form
param
charset the original string charset
return
original string
throws
DecoderException Thrown if URL decoding is unsuccessful
throws
UnsupportedEncodingException Thrown if charset is not supported

        if (pString == null) {
            return null;
        }
        return new String(decode(pString.getBytes(StringEncodings.US_ASCII)), charset);
    
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.

param
bytes array of URL safe characters
return
array of original bytes
throws
DecoderException Thrown if URL decoding is unsuccessful

        if (bytes == null) {
            return null;
        }
        ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 
        for (int i = 0; i < bytes.length; i++) {
            int b = bytes[i];
            if (b == '+") {
                buffer.write(' ");
            } else if (b == '%") {
                try {
                    int u = Character.digit((char)bytes[++i], 16);
                    int l = Character.digit((char)bytes[++i], 16);
                    if (u == -1 || l == -1) {
                        throw new DecoderException("Invalid URL encoding");
                    }
                    buffer.write((char)((u << 4) + l));
                } catch(ArrayIndexOutOfBoundsException e) {
                    throw new DecoderException("Invalid URL encoding");
                }
            } else {
                buffer.write(b);
            }
        }
        return buffer.toByteArray(); 
    
public java.lang.Objectencode(java.lang.Object pObject)
Encodes an object into its URL safe form. Unsafe characters are escaped.

param
pObject string to convert to a URL safe form
return
URL safe object
throws
EncoderException Thrown if URL encoding is not applicable to objects of this type or if encoding is unsuccessful

        if (pObject == null) {
            return null;
        } else if (pObject instanceof byte[]) {
            return encode((byte[])pObject);
        } else if (pObject instanceof String) {
            return encode((String)pObject);
        } else {
            throw new EncoderException("Objects of type " +
                pObject.getClass().getName() + " cannot be URL encoded"); 
              
        }
    
public byte[]encode(byte[] bytes)
Encodes an array of bytes into an array of URL safe 7-bit characters. Unsafe characters are escaped.

param
bytes array of bytes to convert to URL safe characters
return
array of bytes containing URL safe characters

        return encodeUrl(WWW_FORM_URL, bytes);
    
public java.lang.Stringencode(java.lang.String pString, java.lang.String charset)
Encodes a string into its URL safe form using the specified string charset. Unsafe characters are escaped.

param
pString string to convert to a URL safe form
param
charset the charset for pString
return
URL safe string
throws
UnsupportedEncodingException Thrown if charset is not supported

        if (pString == null) {
            return null;
        }
        return new String(encode(pString.getBytes(charset)), StringEncodings.US_ASCII);
    
public java.lang.Stringencode(java.lang.String pString)
Encodes a string into its URL safe form using the default string charset. Unsafe characters are escaped.

param
pString string to convert to a URL safe form
return
URL safe string
throws
EncoderException Thrown if URL encoding is unsuccessful
see
#getDefaultCharset()

        if (pString == null) {
            return null;
        }
        try {
            return encode(pString, getDefaultCharset());
        } catch(UnsupportedEncodingException e) {
            throw new EncoderException(e.getMessage());
        }
    
public static final byte[]encodeUrl(java.util.BitSet urlsafe, byte[] bytes)
Encodes an array of bytes into an array of URL safe 7-bit characters. Unsafe characters are escaped.

param
urlsafe bitset of characters deemed URL safe
param
bytes array of bytes to convert to URL safe characters
return
array of bytes containing URL safe characters

        if (bytes == null) {
            return null;
        }
        if (urlsafe == null) {
            urlsafe = WWW_FORM_URL;
        }
        
        ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 
        for (int i = 0; i < bytes.length; i++) {
            int b = bytes[i];
            if (b < 0) {
                b = 256 + b;
            }
            if (urlsafe.get(b)) {
                if (b == ' ") {
                    b = '+";
                }
                buffer.write(b);
            } else {
                buffer.write('%");
                char hex1 = Character.toUpperCase(
                  Character.forDigit((b >> 4) & 0xF, 16));
                char hex2 = Character.toUpperCase(
                  Character.forDigit(b & 0xF, 16));
                buffer.write(hex1);
                buffer.write(hex2);
            }
        }
        return buffer.toByteArray(); 
    
public java.lang.StringgetDefaultCharset()
The default charset used for string decoding and encoding.

return
the default string charset.

        return this.charset;
    
public java.lang.StringgetEncoding()
The String encoding used for decoding and encoding.

return
Returns the encoding.
deprecated
use #getDefaultCharset()

        return this.charset;