PercentCodec (Apache Commons Codec 1.22.1 API)
Description: declaration: package: org.apache.commons.codec.net, class: PercentCodec

Package org.apache.commons.codec.net

# Class PercentCodec

java.lang.Object
org.apache.commons.codec.net.PercentCodec

All Implemented Interfaces:
  `BinaryDecoder`, `BinaryEncoder`, `Decoder`, `Encoder`

public class PercentCodec extends Object implements BinaryEncoder, BinaryDecoder
Implements the Percent-Encoding scheme, as described in HTTP 1.1 specification. For extensibility, an array of special US-ASCII characters can be specified in order to perform proper URI encoding for the different parts of the URI.

This class is immutable. It is also thread-safe besides using BitSet which is not thread-safe, but its public interface only call the access

Since:
  1.12
See Also:
  - Percent-Encoding

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `PercentCodec()`
  Constructs a Percent coded that will encode all the non US-ASCII characters using the Percent-Encoding while it will not encode all the US-ASCII characters, except for character '%' that is used as escape character for Percent-Encoding.
  `PercentCodec(byte[] alwaysEncodeChars, boolean plusForSpace)`
  Constructs a Percent codec by specifying the characters that belong to US-ASCII that should always be encoded.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `byte[]`
  `decode(byte[] bytes)`
  Decodes bytes encoded with Percent-Encoding based on RFC 3986.
  `Object`
  `decode(Object obj)`
  Decodes a byte[] Object, whose bytes are encoded with Percent-Encoding.
  `byte[]`
  `encode(byte[] bytes)`
  Percent-Encoding based on RFC 3986.
  `Object`
  `encode(Object obj)`
  Encodes an object into using the Percent-Encoding.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### PercentCodec

    public PercentCodec()
    Constructs a Percent coded that will encode all the non US-ASCII characters using the Percent-Encoding while it will not encode all the US-ASCII characters, except for character '%' that is used as escape character for Percent-Encoding.

  - ### PercentCodec

    public PercentCodec(byte[] alwaysEncodeChars, boolean plusForSpace)
    Constructs a Percent codec by specifying the characters that belong to US-ASCII that should always be encoded. The rest US-ASCII characters will not be encoded, except for character '%' that is used as escape character for Percent-Encoding.
    Parameters:
      `alwaysEncodeChars` - The unsafe characters that should always be encoded.
      `plusForSpace` - The flag defining if the space character should be encoded as '+'.

- ## Method Details

  - ### decode

    public byte[] decode(byte[] bytes) throws DecoderException
    Decodes bytes encoded with Percent-Encoding based on RFC 3986. The reverse process is performed in order to decode the encoded characters to Unicode.
    Specified by:
      `decode` in interface `BinaryDecoder`
    Parameters:
      `bytes` - A byte array which has been encoded with the appropriate encoder.
    Returns:
      A byte array that contains decoded content.
    Throws:
      `DecoderException` - A decoder exception is thrown if a Decoder encounters a failure condition during the decode process.

  - ### decode

    public Object decode(Object obj) throws DecoderException
    Decodes a byte[] Object, whose bytes are encoded with Percent-Encoding.
    Specified by:
      `decode` in interface `Decoder`
    Parameters:
      `obj` - The object to decode.
    Returns:
      The decoding result byte[] as Object.
    Throws:
      `DecoderException` - Thrown if the object is not a byte array.

  - ### encode

    public byte[] encode(byte[] bytes) throws EncoderException
    Percent-Encoding based on RFC 3986. The non US-ASCII characters are encoded, as well as the US-ASCII characters that are configured to be always encoded.
    Specified by:
      `encode` in interface `BinaryEncoder`
    Parameters:
      `bytes` - Data to be encoded.
    Returns:
      A byte array containing the encoded data.
    Throws:
      `EncoderException` - thrown if the Encoder encounters a failure condition during the encoding process.

  - ### encode

    public Object encode(Object obj) throws EncoderException
    Encodes an object into using the Percent-Encoding. Only byte[] objects are accepted.
    Specified by:
      `encode` in interface `Encoder`
    Parameters:
      `obj` - The object to encode.
    Returns:
      The encoding result byte[] as Object.
    Throws:
      `EncoderException` - Thrown if the object is not a byte array.