Class PercentCodec

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

    public class PercentCodec
    extends java.lang.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
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.BitSet alwaysEncodeChars
      The bit set used to store the character that should be always encoded
      private int alwaysEncodeCharsMax
      The minimum and maximum code of the bytes that is inserted in the bit set, used to prevent look-ups
      private int alwaysEncodeCharsMin
      The minimum and maximum code of the bytes that is inserted in the bit set, used to prevent look-ups
      private static byte ESCAPE_CHAR
      The escape character used by the Percent-Encoding in order to introduce an encoded character.
      private boolean plusForSpace
      The flag defining if the space character should be encoded as '+'
    • 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

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private boolean canEncode​(byte c)  
      private boolean containsSpace​(byte[] bytes)  
      byte[] decode​(byte[] bytes)
      Decode bytes encoded with Percent-Encoding based on RFC 3986.
      java.lang.Object decode​(java.lang.Object obj)
      Decodes a byte[] Object, whose bytes are encoded with Percent-Encoding.
      private byte[] doEncode​(byte[] bytes, int expectedLength, boolean willEncode)  
      byte[] encode​(byte[] bytes)
      Percent-Encoding based on RFC 3986.
      java.lang.Object encode​(java.lang.Object obj)
      Encodes an object into using the Percent-Encoding.
      private int expectedDecodingBytes​(byte[] bytes)  
      private int expectedEncodingBytes​(byte[] bytes)  
      private boolean inAlwaysEncodeCharsRange​(byte c)  
      private void insertAlwaysEncodeChar​(byte b)
      Inserts a single character into a BitSet and maintains the min and max of the characters of the BitSet alwaysEncodeChars in order to avoid look-ups when a byte is out of this range.
      private void insertAlwaysEncodeChars​(byte[] alwaysEncodeCharsArray)
      Adds the byte array into a BitSet for faster lookup
      private boolean isAsciiChar​(byte c)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • ESCAPE_CHAR

        private static final byte ESCAPE_CHAR
        The escape character used by the Percent-Encoding in order to introduce an encoded character.
        See Also:
        Constant Field Values
      • alwaysEncodeChars

        private final java.util.BitSet alwaysEncodeChars
        The bit set used to store the character that should be always encoded
      • plusForSpace

        private final boolean plusForSpace
        The flag defining if the space character should be encoded as '+'
      • alwaysEncodeCharsMin

        private int alwaysEncodeCharsMin
        The minimum and maximum code of the bytes that is inserted in the bit set, used to prevent look-ups
      • alwaysEncodeCharsMax

        private int alwaysEncodeCharsMax
        The minimum and maximum code of the bytes that is inserted in the bit set, used to prevent look-ups
    • Constructor Detail

      • 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 Detail

      • insertAlwaysEncodeChars

        private void insertAlwaysEncodeChars​(byte[] alwaysEncodeCharsArray)
        Adds the byte array into a BitSet for faster lookup
        Parameters:
        alwaysEncodeCharsArray -
      • insertAlwaysEncodeChar

        private void insertAlwaysEncodeChar​(byte b)
        Inserts a single character into a BitSet and maintains the min and max of the characters of the BitSet alwaysEncodeChars in order to avoid look-ups when a byte is out of this range.
        Parameters:
        b - the byte that is candidate for min and max limit
      • 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.
      • doEncode

        private byte[] doEncode​(byte[] bytes,
                                int expectedLength,
                                boolean willEncode)
      • expectedEncodingBytes

        private int expectedEncodingBytes​(byte[] bytes)
      • containsSpace

        private boolean containsSpace​(byte[] bytes)
      • canEncode

        private boolean canEncode​(byte c)
      • inAlwaysEncodeCharsRange

        private boolean inAlwaysEncodeCharsRange​(byte c)
      • isAsciiChar

        private boolean isAsciiChar​(byte c)
      • decode

        public byte[] decode​(byte[] bytes)
                      throws DecoderException
        Decode 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.
      • expectedDecodingBytes

        private int expectedDecodingBytes​(byte[] bytes)
      • encode

        public java.lang.Object encode​(java.lang.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 - if the object is not a byte array
      • decode

        public java.lang.Object decode​(java.lang.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 - if the object is not a byte array