public class URLCodec extends java.lang.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
URLEncoder
and URLDecoder
on older Java platforms, as these classes in Java versions below
1.4 rely on the platform's default charset encoding.
Modifier and Type | Field and Description |
---|---|
protected java.lang.String |
charset
The default charset used for string decoding and encoding.
|
protected static byte |
ESCAPE_CHAR
Release 1.5 made this field final.
|
protected static java.util.BitSet |
WWW_FORM_URL
BitSet of www-form-url safe characters.
|
Constructor and Description |
---|
URLCodec()
Default constructor.
|
URLCodec(java.lang.String charset)
Constructor which allows for the selection of a default charset
|
Modifier and Type | Method and Description |
---|---|
byte[] |
decode(byte[] bytes)
Decodes an array of URL safe 7-bit characters into an array of
original bytes.
|
java.lang.Object |
decode(java.lang.Object pObject)
Decodes a URL safe object into its original form.
|
java.lang.String |
decode(java.lang.String pString)
Decodes a URL safe string into its original form using the default
string charset.
|
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.
|
byte[] |
encode(byte[] bytes)
Encodes an array of bytes into an array of URL safe 7-bit
characters.
|
java.lang.Object |
encode(java.lang.Object pObject)
Encodes an object into its URL safe form.
|
java.lang.String |
encode(java.lang.String pString)
Encodes a string into its URL safe form using the default string
charset.
|
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 byte[] |
encodeUrl(java.util.BitSet urlsafe,
byte[] bytes)
Encodes an array of bytes into an array of URL safe 7-bit characters.
|
java.lang.String |
getDefaultCharset()
The default charset used for string decoding and encoding.
|
java.lang.String |
getEncoding()
Deprecated.
Use
getDefaultCharset() , will be removed in 2.0. |
protected java.lang.String charset
protected static final byte ESCAPE_CHAR
protected static final java.util.BitSet WWW_FORM_URL
public URLCodec()
public URLCodec(java.lang.String charset)
charset
- the default string charset to use.public static final byte[] encodeUrl(java.util.BitSet urlsafe, byte[] bytes)
urlsafe
- bitset of characters deemed URL safebytes
- array of bytes to convert to URL safe characterspublic static final byte[] decodeUrl(byte[] bytes) throws DecoderException
bytes
- array of URL safe charactersDecoderException
- Thrown if URL decoding is unsuccessfulpublic byte[] encode(byte[] bytes)
encode
in interface BinaryEncoder
bytes
- array of bytes to convert to URL safe characterspublic byte[] decode(byte[] bytes) throws DecoderException
decode
in interface BinaryDecoder
bytes
- array of URL safe charactersDecoderException
- Thrown if URL decoding is unsuccessfulpublic java.lang.String encode(java.lang.String pString, java.lang.String charset) throws java.io.UnsupportedEncodingException
pString
- string to convert to a URL safe formcharset
- the charset for pStringjava.io.UnsupportedEncodingException
- Thrown if charset is not supportedpublic java.lang.String encode(java.lang.String pString) throws EncoderException
encode
in interface StringEncoder
pString
- string to convert to a URL safe formEncoderException
- Thrown if URL encoding is unsuccessfulgetDefaultCharset()
public java.lang.String decode(java.lang.String pString, java.lang.String charset) throws DecoderException, java.io.UnsupportedEncodingException
pString
- URL safe string to convert into its original formcharset
- the original string charsetDecoderException
- Thrown if URL decoding is unsuccessfuljava.io.UnsupportedEncodingException
- Thrown if charset is not
supportedpublic java.lang.String decode(java.lang.String pString) throws DecoderException
decode
in interface StringDecoder
pString
- URL safe string to convert into its original formDecoderException
- Thrown if URL decoding is unsuccessfulgetDefaultCharset()
public java.lang.Object encode(java.lang.Object pObject) throws EncoderException
encode
in interface Encoder
pObject
- string to convert to a URL safe formEncoderException
- Thrown if URL encoding is not
applicable to objects of this type or
if encoding is unsuccessfulpublic java.lang.Object decode(java.lang.Object pObject) throws DecoderException
decode
in interface Decoder
pObject
- URL safe object to convert into its original formDecoderException
- Thrown if the argument is not a String
or byte[]
. Thrown if a failure condition is
encountered during the decode process.public java.lang.String getDefaultCharset()
public java.lang.String getEncoding()
getDefaultCharset()
, will be removed in 2.0.String
encoding used for decoding and encoding.This software is published under the BSD license. Copyright © 2003-${build.year}, CRIXP AG, Switzerland, All rights reserved. Use is subject to license terms.