com.atlassian.jira.util
Class URLCodec

java.lang.Object
  extended by com.atlassian.jira.util.URLCodec

public class URLCodec
extends java.lang.Object

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 URLEncoder and 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,v 1.2 2006/05/23 01:06:07 schang Exp $
Author:
Apache Software Foundation

Field Summary
protected static byte ESCAPE_CHAR
           
protected static java.util.BitSet WWW_FORM_URL
          BitSet of www-form-url safe characters.
protected static java.util.BitSet WWW_FORM_URL_SPACE_NOT_SAFE
           
 
Constructor Summary
URLCodec()
           
 
Method Summary
static byte[] decode(byte[] bytes)
          Decodes an array of URL safe 7-bit characters into an array of original bytes.
static java.lang.Object decode(java.lang.Object pObject)
          Decodes a URL safe object into its original form.
static java.lang.String decode(java.lang.String pString)
          Decodes a URL safe string into its original form using the default string charset.
static java.lang.String decode(java.lang.String pString, java.lang.String charset)
          Decodes a URL safe string into its original form using the specified encoding.
static byte[] decodeUrl(byte[] bytes)
          Decodes an array of URL safe 7-bit characters into an array of original bytes.
static byte[] encode(byte[] bytes)
          Encodes an array of bytes into an array of URL safe 7-bit characters.
static byte[] encode(byte[] bytes, boolean withSpacesAsHexValue)
          Encodes an array of bytes into an array of URL safe 7-bit characters.
static java.lang.Object encode(java.lang.Object pObject)
          Encodes an object into its URL safe form.
static java.lang.Object encode(java.lang.Object pObject, boolean withSpacesAsHexValue)
          Encodes an object into its URL safe form.
static java.lang.String encode(java.lang.String pString)
          Encodes a string into its URL safe form using the default string charset.
static java.lang.String encode(java.lang.String pString, boolean withSpacesAsHexValue)
          Encodes a string into its URL safe form using the default string charset.
static java.lang.String encode(java.lang.String pString, java.lang.String charset)
          Encodes a string into its URL safe form using the specified string charset.
static java.lang.String encode(java.lang.String pString, java.lang.String charset, boolean withSpacesAsHexValue)
          Encodes a string into its URL safe form using the specified string charset.
static byte[] encodeUrl(java.util.BitSet urlsafe, byte[] bytes)
          Encodes an array of bytes into an array of URL safe 7-bit characters.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ESCAPE_CHAR

protected static byte ESCAPE_CHAR

WWW_FORM_URL

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


WWW_FORM_URL_SPACE_NOT_SAFE

protected static final java.util.BitSet WWW_FORM_URL_SPACE_NOT_SAFE
Constructor Detail

URLCodec

public URLCodec()
Method Detail

encodeUrl

public static 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.

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

decodeUrl

public static byte[] decodeUrl(byte[] bytes)
                        throws java.io.UnsupportedEncodingException
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:
java.io.UnsupportedEncodingException - Thrown if URL decoding is unsuccessful

encode

public static byte[] encode(byte[] bytes)
Encodes an array of bytes into an array of URL safe 7-bit characters. Unsafe characters are escaped. Spaces are encoded as the '+' character.

Parameters:
bytes - array of bytes to convert to URL safe characters
Returns:
array of bytes containing URL safe characters

encode

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

Parameters:
withSpacesAsHexValue - if true, spaces will be encoded with their hex value (%20), otherwise they're encoded with '+' character.
bytes - array of bytes to convert to URL safe characters
Returns:
array of bytes containing URL safe characters

decode

public static byte[] decode(byte[] bytes)
                     throws java.io.UnsupportedEncodingException
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:
java.io.UnsupportedEncodingException - Thrown if URL decoding is unsuccessful

encode

public static java.lang.String encode(java.lang.String pString,
                                      java.lang.String charset)
                               throws java.io.UnsupportedEncodingException
Encodes a string into its URL safe form using the specified string charset. Unsafe characters are escaped. Spaces are encoded with the '+' character.

Parameters:
pString - string to convert to a URL safe form
charset - the charset for pString
Returns:
URL safe string
Throws:
java.io.UnsupportedEncodingException - Thrown if charset is not supported

encode

public static java.lang.String encode(java.lang.String pString,
                                      java.lang.String charset,
                                      boolean withSpacesAsHexValue)
                               throws java.io.UnsupportedEncodingException
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
withSpacesAsHexValue - if true spaces will be encoded with their hex value (%20), otherwise they're encoded with '+' character.
Returns:
URL safe string
Throws:
java.io.UnsupportedEncodingException - Thrown if charset is not supported

encode

public static java.lang.String encode(java.lang.String pString)
                               throws java.io.UnsupportedEncodingException
Encodes a string into its URL safe form using the default string charset. Unsafe characters are escaped. Spaces are encoded as the '+' character.

Parameters:
pString - string to convert to a URL safe form
Returns:
URL safe string
Throws:
java.io.UnsupportedEncodingException - Thrown if URL encoding is unsuccessful
See Also:
getDefaultCharset()

encode

public static java.lang.String encode(java.lang.String pString,
                                      boolean withSpacesAsHexValue)
                               throws java.io.UnsupportedEncodingException
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
withSpacesAsHexValue - if true spaces will be encoded with their hex value (%20), otherwise they're encoded with '+' character.
Returns:
URL safe string
Throws:
java.io.UnsupportedEncodingException - Thrown if URL encoding is unsuccessful
See Also:
getDefaultCharset()

decode

public static java.lang.String decode(java.lang.String pString,
                                      java.lang.String charset)
                               throws java.io.UnsupportedEncodingException
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:
java.io.UnsupportedEncodingException - Thrown if charset is not supported

decode

public static java.lang.String decode(java.lang.String pString)
                               throws java.io.UnsupportedEncodingException
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:
java.io.UnsupportedEncodingException - Thrown if URL decoding is unsuccessful
See Also:
getDefaultCharset()

encode

public static java.lang.Object encode(java.lang.Object pObject)
                               throws java.io.UnsupportedEncodingException
Encodes an object into its URL safe form. Unsafe characters are escaped. Encodes spaces as '+' character.

Parameters:
pObject - string to convert to a URL safe form
Returns:
URL safe object
Throws:
java.io.UnsupportedEncodingException - Thrown if URL encoding is not applicable to objects of this type or if encoding is unsuccessful

encode

public static java.lang.Object encode(java.lang.Object pObject,
                                      boolean withSpacesAsHexValue)
                               throws java.io.UnsupportedEncodingException
Encodes an object into its URL safe form. Unsafe characters are escaped.

Parameters:
pObject - string to convert to a URL safe form
withSpacesAsHexValue - if true spaces will be encoded with their hex value (%20), otherwise they're encoded with '+' character.
Returns:
URL safe object
Throws:
java.io.UnsupportedEncodingException - Thrown if URL encoding is not applicable to objects of this type or if encoding is unsuccessful

decode

public static java.lang.Object decode(java.lang.Object pObject)
                               throws java.io.UnsupportedEncodingException
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:
java.io.UnsupportedEncodingException - Thrown if the argument is not a String or byte[]. Thrown if a failure condition is encountered during the decode process.


Copyright © 2002-2010 Atlassian. All Rights Reserved.