Class Coder


  • public class Coder
    extends java.lang.Object
    Provides static methods for encoding and decoding String data. For more information about percent-encoding, refer to RFC 3986.
    • Constructor Summary

      Constructors 
      Constructor Description
      Coder​(long cPtr, boolean cMemoryOwn)
      For internal use only.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String decodeHTML​(java.lang.String data)
      Decodes special characters within the specified HTML String.
      void delete()  
      static long getCPtr​(Coder obj)
      For internal use only.
      static java.lang.String getDefaultCharactersAllowedInPostedForm()
      Gets a String containing all the characters that, by default, are allowed to be posted in a Form.
      static java.lang.String getDefaultUnsafeCharacters()
      Gets a String containing the characters that, by default, are considered to be unsafe and must be percent-encoded.
      static java.lang.String postDataEncode​(java.lang.String data)
      Encodes a String so that it can be sent to a web server as POST data.
      static java.lang.String postDataEncode​(java.lang.String data, java.lang.String charactersAllowedInPostedForm)
      Encodes a String so that it can be sent to a web server as POST data, specifying the characters which shouldn't be encoded.
      static java.lang.String urlDecode​(java.lang.String urlFragment)
      Decodes a percent-encoded String.
      static java.lang.String urlEncode​(java.lang.String urlFragment)
      Encodes a String using percent-encoding.
      static java.lang.String urlEncode​(java.lang.String urlFragment, java.lang.String unsafeCharacters)
      Encodes a String using percent-encoding, specifying which characters should be encoded.
      static java.lang.String urlEncode​(java.lang.String urlFragment, java.lang.String unsafeCharacters, boolean encodeReservedCharacters)
      Encodes a String using percent-encoding, specifying which characters should be encoded (optionally including reserved characters).
      • Methods inherited from class java.lang.Object

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

      • Coder

        public Coder​(long cPtr,
                     boolean cMemoryOwn)
        For internal use only. Of no interest to the user.
        Parameters:
        cPtr - pointer to the CPP wrapped object
        cMemoryOwn - indicates if this object is responsible for memory management of the CPP object
    • Method Detail

      • getCPtr

        public static long getCPtr​(Coder obj)
        For internal use only. Of no interest to the user.
        Parameters:
        obj - a reference to an object of this class
        Returns:
        a long containing the address of the CPP wrapped object
      • delete

        public void delete()
      • urlDecode

        public static java.lang.String urlDecode​(java.lang.String urlFragment)
        Decodes a percent-encoded String. Converts percent-encoded characters such as %3F into the character they represent, in this case the question mark character ?.

        For more information about percent-encoding, refer to RFC 3986.

        e.g.

        
         String decoded = Coder.urlDecode("search%3Fq%3Dcamera");
         // decoded == search?q=camera
         
        Parameters:
        urlFragment - the text to decode; this is usually a portion of a Url
        Returns:
        the decoded text
      • urlEncode

        public static java.lang.String urlEncode​(java.lang.String urlFragment,
                                                 java.lang.String unsafeCharacters,
                                                 boolean encodeReservedCharacters)
        Encodes a String using percent-encoding, specifying which characters should be encoded (optionally including reserved characters).

        A character in the String will be percent-encoded if:

        • It is outside the printable ASCII range (i.e. is not in the ASCII range 32 to 127) OR
        • It is one of the defined unsafeCharacters OR
        • It is a reserved character, and encodeReservedCharacters has the value true

        For more information about percent-encoding, refer to RFC 3986.

        e.g.

        
         String encoded = Coder.urlEncode("search?q=camera", "<>\"%{}|\\^[]` \n\t\r&", true);
         // encoded == search%3Fq%3Dcamera
         
        Parameters:
        urlFragment - the text to percent-encode
        unsafeCharacters - a String containing a list of characters that need to be percent-encoded
        encodeReservedCharacters - true if reserved characters should be encoded
        Returns:
        the encoded text
        See Also:
        getDefaultUnsafeCharacters()
      • urlEncode

        public static java.lang.String urlEncode​(java.lang.String urlFragment,
                                                 java.lang.String unsafeCharacters)
        Encodes a String using percent-encoding, specifying which characters should be encoded.

        A character in the String will be percent-encoded if:

        • It is outside the printable ASCII range (i.e. is not in the ASCII range 32 to 127) OR
        • It is one of the defined unsafeCharacters

        For more information about percent-encoding, refer to RFC 3986.

        e.g.

        
         String encoded = Coder.urlEncode("search?q=camera", "<>\"%{}|\\^[]` \n\t\r&");
         // encoded == search%3Fq%3Dcamera
         
        Parameters:
        urlFragment - the text to percent-encode
        unsafeCharacters - a String containing a list of characters that need to be percent-encoded
        Returns:
        the encoded text
        See Also:
        getDefaultUnsafeCharacters()
      • urlEncode

        public static java.lang.String urlEncode​(java.lang.String urlFragment)
        Encodes a String using percent-encoding.

        A character in the String will be percent-encoded if:

        • It is outside the printable ASCII range (i.e. is not in the ASCII range 32 to 127) OR
        • It is one of the default unsafeCharacters (see getDefaultUnsafeCharacters())

        For more information about percent-encoding, refer to RFC 3986.

        e.g.

        
         String encoded = Coder.urlEncode("search?q=camera");
         // encoded == search%3Fq%3Dcamera
         
        Parameters:
        urlFragment - the text to percent-encode
        Returns:
        the encoded text
      • postDataEncode

        public static java.lang.String postDataEncode​(java.lang.String data)
        Encodes a String so that it can be sent to a web server as POST data. This is done using percent-encoding, in a similar way to urlEncode(String), with the exception that space characters will be converted to +, not %20
        Parameters:
        data - the text to encode
        Returns:
        the encoded text
      • postDataEncode

        public static java.lang.String postDataEncode​(java.lang.String data,
                                                      java.lang.String charactersAllowedInPostedForm)
        Encodes a String so that it can be sent to a web server as POST data, specifying the characters which shouldn't be encoded. This is done using percent-encoding, in a similar way to urlEncode(String), with the exception that space characters will be converted to +, not %20
        Parameters:
        data - the text to encode
        charactersAllowedInPostedForm - a String containing characters that can be present in POST data without being percent-encoded (see getDefaultCharactersAllowedInPostedForm())
        Returns:
        the encoded text
      • decodeHTML

        public static java.lang.String decodeHTML​(java.lang.String data)
        Decodes special characters within the specified HTML String. For example, &amp; will be converted to &.
        Parameters:
        data - the text to decode
        Returns:
        the decoded text
      • getDefaultCharactersAllowedInPostedForm

        public static java.lang.String getDefaultCharactersAllowedInPostedForm()
        Gets a String containing all the characters that, by default, are allowed to be posted in a Form.

        By default, a character will be percent-encoded within a form if it is:

        • Outside the ASCII range OR
        • Not an alpha-numeric character AND not in this list of allowed characters
        By default, the allowed list of characters is as follows:
        • ' ' - space
        • '-' - hyphen
        • '_' - underscore
        • '.' - full stop
        • '*' - asterisk
        Returns:
        a String containing the characters that, by default, are allowed to be posted in a Form
        See Also:
        postDataEncode(String)
      • getDefaultUnsafeCharacters

        public static java.lang.String getDefaultUnsafeCharacters()
        Gets a String containing the characters that, by default, are considered to be unsafe and must be percent-encoded.

        A character in a string will be percent-encoded if it is:

        • Outside the printable ASCII range (i.e. is not in the ASCII range 32 to 127) OR
        • One of these unsafe characters
        By default, the list of unsafe characters is as follows:
        • '<' - less-than sign
        • '>' - greater-than sign
        • '"' - quotation mark
        • '%' - percent sign
        • '{' - left curly bracket
        • '}' - right curly bracket
        • '|' - vertical line
        • '\' - reverse solidus
        • '^' - circumflex accent
        • '[' - left square bracket
        • ']' - right square bracket
        • '`' - grave accent
        • ' ' - space
        • '\r' - carriage return
        • '\n' - line feed
        • '\t' - tab
        • '&' - ampersand
        Returns:
        a String containing the characters that, by default, are considered unsafe and must be percent-encoded
        See Also:
        urlEncode(String, String, boolean)