Class EncoderUtil


  • public class EncoderUtil
    extends java.lang.Object
    Static methods for encoding header field values. This includes encoded-words as defined in RFC 2047 or display-names of an e-mail address, for example.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  EncoderUtil.Encoding
      Selects one of the two encodings specified in RFC 2047.
      static class  EncoderUtil.Usage
      Indicates the intended usage of an encoded word.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private EncoderUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static int bEncodedLength​(byte[] bytes)  
      private static java.nio.charset.Charset determineCharset​(java.lang.String text)  
      private static EncoderUtil.Encoding determineEncoding​(byte[] bytes, EncoderUtil.Usage usage)  
      private static byte[] encode​(java.lang.String text, java.nio.charset.Charset charset)  
      static java.lang.String encodeAddressDisplayName​(java.lang.String displayName)
      Encodes the display-name portion of an address.
      static java.lang.String encodeAddressLocalPart​(java.lang.String localPart)
      Encodes the local part of an address specification as described in RFC 5322 section 3.4.1.
      static java.lang.String encodeB​(byte[] bytes)
      Encodes the specified byte array using the B encoding defined in RFC 2047.
      static void encodeB​(java.io.InputStream in, java.io.OutputStream out)
      Encodes the given stream using base64.
      private static java.lang.String encodeB​(java.lang.String prefix, java.lang.String text, int usedCharacters, java.nio.charset.Charset charset, byte[] bytes)  
      static java.lang.String encodeEncodedWord​(java.lang.String text, EncoderUtil.Usage usage)
      Encodes the specified text into an encoded word or a sequence of encoded words separated by space.
      static java.lang.String encodeEncodedWord​(java.lang.String text, EncoderUtil.Usage usage, int usedCharacters)
      Encodes the specified text into an encoded word or a sequence of encoded words separated by space.
      static java.lang.String encodeEncodedWord​(java.lang.String text, EncoderUtil.Usage usage, int usedCharacters, java.nio.charset.Charset charset, EncoderUtil.Encoding encoding)
      Encodes the specified text into an encoded word or a sequence of encoded words separated by space.
      static java.lang.String encodeHeaderParameter​(java.lang.String name, java.lang.String value)
      Encodes the specified strings into a header parameter as described in RFC 2045 section 5.1 and RFC 2183 section 2.
      static java.lang.String encodeIfNecessary​(java.lang.String text, EncoderUtil.Usage usage, int usedCharacters)
      Shortcut method that encodes the specified text into an encoded-word if the text has to be encoded.
      static java.lang.String encodeQ​(byte[] bytes, EncoderUtil.Usage usage)
      Encodes the specified byte array using the Q encoding defined in RFC 2047.
      static void encodeQ​(java.io.InputStream in, java.io.OutputStream out)
      Encodes the given stream using Quoted-Printable.
      private static java.lang.String encodeQ​(java.lang.String prefix, java.lang.String text, EncoderUtil.Usage usage, int usedCharacters, java.nio.charset.Charset charset, byte[] bytes)  
      static void encodeQBinary​(java.io.InputStream in, java.io.OutputStream out)
      Encodes the given stream using Quoted-Printable.
      static boolean hasToBeEncoded​(java.lang.String text, int usedCharacters)
      Determines if the specified string has to encoded into an encoded-word.
      private static char hexDigit​(int i)  
      private static java.util.BitSet initChars​(java.lang.String specials)  
      private static boolean isAtomPhrase​(java.lang.String str)  
      private static boolean isDotAtomText​(java.lang.String str)  
      static boolean isToken​(java.lang.String str)
      Tests whether the specified string is a token as defined in RFC 2045 section 5.1.
      private static int qEncodedLength​(byte[] bytes, EncoderUtil.Usage usage)  
      private static java.lang.String quote​(java.lang.String str)  
      • Methods inherited from class java.lang.Object

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

      • BASE64_TABLE

        private static final byte[] BASE64_TABLE
      • Q_REGULAR_CHARS

        private static final java.util.BitSet Q_REGULAR_CHARS
      • Q_RESTRICTED_CHARS

        private static final java.util.BitSet Q_RESTRICTED_CHARS
      • ENCODED_WORD_MAX_LENGTH

        private static final int ENCODED_WORD_MAX_LENGTH
        See Also:
        Constant Field Values
      • TOKEN_CHARS

        private static final java.util.BitSet TOKEN_CHARS
      • ATEXT_CHARS

        private static final java.util.BitSet ATEXT_CHARS
      • QUOTE

        private static final java.util.regex.Pattern QUOTE
    • Constructor Detail

      • EncoderUtil

        private EncoderUtil()
    • Method Detail

      • initChars

        private static java.util.BitSet initChars​(java.lang.String specials)
      • encodeAddressDisplayName

        public static java.lang.String encodeAddressDisplayName​(java.lang.String displayName)
        Encodes the display-name portion of an address. See RFC 5322 section 3.4 and RFC 2047 section 5.3. The specified string should not be folded.
        Parameters:
        displayName - display-name to encode.
        Returns:
        encoded display-name.
      • encodeAddressLocalPart

        public static java.lang.String encodeAddressLocalPart​(java.lang.String localPart)
        Encodes the local part of an address specification as described in RFC 5322 section 3.4.1. Leading and trailing CFWS should have been removed before calling this method. The specified string should not contain any illegal (control or non-ASCII) characters.
        Parameters:
        localPart - the local part to encode
        Returns:
        the encoded local part.
      • encodeHeaderParameter

        public static java.lang.String encodeHeaderParameter​(java.lang.String name,
                                                             java.lang.String value)
        Encodes the specified strings into a header parameter as described in RFC 2045 section 5.1 and RFC 2183 section 2. The specified strings should not contain any illegal (control or non-ASCII) characters.
        Parameters:
        name - parameter name.
        value - parameter value.
        Returns:
        encoded result.
      • encodeIfNecessary

        public static java.lang.String encodeIfNecessary​(java.lang.String text,
                                                         EncoderUtil.Usage usage,
                                                         int usedCharacters)
        Shortcut method that encodes the specified text into an encoded-word if the text has to be encoded.
        Parameters:
        text - text to encode.
        usage - whether the encoded-word is to be used to replace a text token or a word entity (see RFC 822).
        usedCharacters - number of characters already used up (0 <= usedCharacters <= 50).
        Returns:
        the specified text if encoding is not necessary or an encoded word or a sequence of encoded words otherwise.
      • hasToBeEncoded

        public static boolean hasToBeEncoded​(java.lang.String text,
                                             int usedCharacters)
        Determines if the specified string has to encoded into an encoded-word. Returns true if the text contains characters that don't fall into the printable ASCII character set or if the text contains a 'word' (sequence of non-whitespace characters) longer than 77 characters (including characters already used up in the line).
        Parameters:
        text - text to analyze.
        usedCharacters - number of characters already used up (0 <= usedCharacters <= 50).
        Returns:
        true if the specified text has to be encoded into an encoded-word, false otherwise.
      • encodeEncodedWord

        public static java.lang.String encodeEncodedWord​(java.lang.String text,
                                                         EncoderUtil.Usage usage)
        Encodes the specified text into an encoded word or a sequence of encoded words separated by space. The text is separated into a sequence of encoded words if it does not fit in a single one.

        The charset to encode the specified text into a byte array and the encoding to use for the encoded-word are detected automatically.

        This method assumes that zero characters have already been used up in the current line.

        Parameters:
        text - text to encode.
        usage - whether the encoded-word is to be used to replace a text token or a word entity (see RFC 822).
        Returns:
        the encoded word (or sequence of encoded words if the given text does not fit in a single encoded word).
        See Also:
        hasToBeEncoded(String, int)
      • encodeEncodedWord

        public static java.lang.String encodeEncodedWord​(java.lang.String text,
                                                         EncoderUtil.Usage usage,
                                                         int usedCharacters)
        Encodes the specified text into an encoded word or a sequence of encoded words separated by space. The text is separated into a sequence of encoded words if it does not fit in a single one.

        The charset to encode the specified text into a byte array and the encoding to use for the encoded-word are detected automatically.

        Parameters:
        text - text to encode.
        usage - whether the encoded-word is to be used to replace a text token or a word entity (see RFC 822).
        usedCharacters - number of characters already used up (0 <= usedCharacters <= 50).
        Returns:
        the encoded word (or sequence of encoded words if the given text does not fit in a single encoded word).
        See Also:
        hasToBeEncoded(String, int)
      • encodeEncodedWord

        public static java.lang.String encodeEncodedWord​(java.lang.String text,
                                                         EncoderUtil.Usage usage,
                                                         int usedCharacters,
                                                         java.nio.charset.Charset charset,
                                                         EncoderUtil.Encoding encoding)
        Encodes the specified text into an encoded word or a sequence of encoded words separated by space. The text is separated into a sequence of encoded words if it does not fit in a single one.
        Parameters:
        text - text to encode.
        usage - whether the encoded-word is to be used to replace a text token or a word entity (see RFC 822).
        usedCharacters - number of characters already used up (0 <= usedCharacters <= 50).
        charset - the Java charset that should be used to encode the specified string into a byte array. A suitable charset is detected automatically if this parameter is null.
        encoding - the encoding to use for the encoded-word (either B or Q). A suitable encoding is automatically chosen if this parameter is null.
        Returns:
        the encoded word (or sequence of encoded words if the given text does not fit in a single encoded word).
        See Also:
        hasToBeEncoded(String, int)
      • encodeB

        public static java.lang.String encodeB​(byte[] bytes)
        Encodes the specified byte array using the B encoding defined in RFC 2047.
        Parameters:
        bytes - byte array to encode.
        Returns:
        encoded string.
      • encodeQ

        public static java.lang.String encodeQ​(byte[] bytes,
                                               EncoderUtil.Usage usage)
        Encodes the specified byte array using the Q encoding defined in RFC 2047.
        Parameters:
        bytes - byte array to encode.
        usage - whether the encoded-word is to be used to replace a text token or a word entity (see RFC 822).
        Returns:
        encoded string.
      • encodeQBinary

        public static void encodeQBinary​(java.io.InputStream in,
                                         java.io.OutputStream out)
                                  throws java.io.IOException
        Encodes the given stream using Quoted-Printable. This assumes that stream is binary and therefore escapes all line endings.
        Parameters:
        in - not null
        out - not null
        Throws:
        java.io.IOException
      • encodeQ

        public static void encodeQ​(java.io.InputStream in,
                                   java.io.OutputStream out)
                            throws java.io.IOException
        Encodes the given stream using Quoted-Printable. This assumes that stream is text and therefore does not escape all line endings.
        Parameters:
        in - not null
        out - not null
        Throws:
        java.io.IOException
      • encodeB

        public static void encodeB​(java.io.InputStream in,
                                   java.io.OutputStream out)
                            throws java.io.IOException
        Encodes the given stream using base64.
        Parameters:
        in - not null
        out - not null
        Throws:
        java.io.IOException - if an I/O error occurs
      • isToken

        public static boolean isToken​(java.lang.String str)
        Tests whether the specified string is a token as defined in RFC 2045 section 5.1.
        Parameters:
        str - string to test.
        Returns:
        true if the specified string is a RFC 2045 token, false otherwise.
      • isAtomPhrase

        private static boolean isAtomPhrase​(java.lang.String str)
      • isDotAtomText

        private static boolean isDotAtomText​(java.lang.String str)
      • quote

        private static java.lang.String quote​(java.lang.String str)
      • encodeB

        private static java.lang.String encodeB​(java.lang.String prefix,
                                                java.lang.String text,
                                                int usedCharacters,
                                                java.nio.charset.Charset charset,
                                                byte[] bytes)
      • bEncodedLength

        private static int bEncodedLength​(byte[] bytes)
      • encodeQ

        private static java.lang.String encodeQ​(java.lang.String prefix,
                                                java.lang.String text,
                                                EncoderUtil.Usage usage,
                                                int usedCharacters,
                                                java.nio.charset.Charset charset,
                                                byte[] bytes)
      • qEncodedLength

        private static int qEncodedLength​(byte[] bytes,
                                          EncoderUtil.Usage usage)
      • encode

        private static byte[] encode​(java.lang.String text,
                                     java.nio.charset.Charset charset)
      • determineCharset

        private static java.nio.charset.Charset determineCharset​(java.lang.String text)
      • hexDigit

        private static char hexDigit​(int i)