Class RFC2231Utility


  • final class RFC2231Utility
    extends java.lang.Object
    Utility class to decode/encode character set on HTTP Header fields based on RFC 2231. This implementation adheres to RFC 5987 in particular, which was defined for HTTP headers.

    RFC 5987 builds on RFC 2231, but has lesser scope like mandatory charset definition and no parameter continuation

    See Also:
    RFC 2231, RFC 5987
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static byte[] HEX_DECODE
      The Hexadecimal decode value.
      private static char[] HEX_DIGITS
      The Hexadecimal values char array.
      private static byte MASK
      The Hexadecimal representation of 127.
      private static int MASK_128
      The Hexadecimal representation of 128.
      private static char PERCENT
      Percent character '37'.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private RFC2231Utility()
      Private constructor so that no instances can be created.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) static java.lang.String decodeText​(java.lang.String encodedText)
      Decodes a string of text obtained from a HTTP header as per RFC 2231
      private static byte[] fromHex​(java.lang.String text)
      Converts text to their corresponding Hex value.
      private static java.lang.String getJavaCharset​(java.lang.String mimeCharset)  
      (package private) static boolean hasEncodedValue​(java.lang.String paramName)
      Checks if Asterisk (*) at the end of parameter name to indicate, if it has charset and language information to decode the value.
      (package private) static java.lang.String stripDelimiter​(java.lang.String paramName)
      If paramName has Asterisk (*) at the end, it will be stripped off, else the passed value will be returned.
      • Methods inherited from class java.lang.Object

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

      • HEX_DIGITS

        private static final char[] HEX_DIGITS
        The Hexadecimal values char array.
      • MASK

        private static final byte MASK
        The Hexadecimal representation of 127.
        See Also:
        Constant Field Values
      • MASK_128

        private static final int MASK_128
        The Hexadecimal representation of 128.
        See Also:
        Constant Field Values
      • HEX_DECODE

        private static final byte[] HEX_DECODE
        The Hexadecimal decode value.
    • Constructor Detail

      • RFC2231Utility

        private RFC2231Utility()
        Private constructor so that no instances can be created. This class contains only static utility methods.
    • Method Detail

      • decodeText

        static java.lang.String decodeText​(java.lang.String encodedText)
                                    throws java.io.UnsupportedEncodingException
        Decodes a string of text obtained from a HTTP header as per RFC 2231

        Eg 1. us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A will be decoded to This is ***fun***

        Eg 2. iso-8859-1'en'%A3%20rate will be decoded to £ rate

        Eg 3. UTF-8''%c2%a3%20and%20%e2%82%ac%20rates will be decoded to £ and € rates

        Parameters:
        encodedText - Text to be decoded has a format of <charset>'<language>'<encoded_value> and ASCII only
        Returns:
        Decoded text based on charset encoding
        Throws:
        java.io.UnsupportedEncodingException - The requested character set wasn't found.
      • fromHex

        private static byte[] fromHex​(java.lang.String text)
        Converts text to their corresponding Hex value.
        Parameters:
        text - ASCII text input
        Returns:
        Byte array of characters decoded from ASCII table
      • getJavaCharset

        private static java.lang.String getJavaCharset​(java.lang.String mimeCharset)
      • hasEncodedValue

        static boolean hasEncodedValue​(java.lang.String paramName)
        Checks if Asterisk (*) at the end of parameter name to indicate, if it has charset and language information to decode the value.
        Parameters:
        paramName - The parameter, which is being checked.
        Returns:
        true, if encoded as per RFC 2231, false otherwise
      • stripDelimiter

        static java.lang.String stripDelimiter​(java.lang.String paramName)
        If paramName has Asterisk (*) at the end, it will be stripped off, else the passed value will be returned.
        Parameters:
        paramName - The parameter, which is being inspected.
        Returns:
        stripped paramName of Asterisk (*), if RFC2231 encoded