java.lang.Object
i2p.susi.webmail.encoding.Encoding
i2p.susi.webmail.encoding.HeaderLine

public class HeaderLineextends Encoding
References for the RFC 2047 encoded-word rules this class implements.
  • http://en.wikipedia.org/wiki/MIME#Encoded-Word
  • http://tools.ietf.org/html/rfc2047
  • https://jeffreystedfast.blogspot.com/2013/09/time-for-rant-on-mime-parsers.html
  • https://jeffreystedfast.blogspot.com/2013/08/why-decoding-rfc2047-encoded-headers-is.html
  • Field Summary

    Modifier and Type
    Field
    Description
    private static final int
    Largest encoded-word the decoder accepts.
    static final String
    Identifier this encoding is registered under, returned by getName().

    Fields inherited from class Encoding

    _log
    Modifier and Type
    Field
    Description
    protected final Log
    Logger for the encoding subclass.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Decode all the header lines, up through \r\n\r\n, and puts them in the ReadBuffer, including the \r\n\r\n.
    encode(byte[] in)
    This will split multibyte chars across lines, see 4th ref above.
    Encode a single header line ONLY.
    The header's name.
    private static boolean
    Whether the char is a control or non-ASCII character.
    private static boolean
    isPSafe(char c)
    Whether the char is safe unencoded in an encoded-word.
    private static boolean
    isPSpecial(char c)
    Whether the char is an RFC 2047 special.
    private static boolean
    isWhiteSpace(char c)
    Whether the char is header whitespace.
    (package private) static String
    malformedText(String situation, String action)
    Describes a malformed encoded-word: what was wrong with it and what the decoder did about it.

    Methods inherited from class Encoding

    decode, decode, decode, decode, decode, encode
    Modifier and Type
    Method
    Description
    decode(byte[] in)
    Decode a byte array using default offset and length.
    decode(byte[] in, int offset, int length)
    Decode a portion of a byte array.
    Decode a buffer into another buffer.
    void
    decode(Buffer in, Buffer out)
    Decode a buffer into another buffer using the input stream.
    Decode a string.
    void
    Encode an input stream of bytes to a ASCII or ISO-8859-1 String.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • NAME

      public static final String NAME
      Identifier this encoding is registered under, returned by getName().
      See Also:
    • DECODE_MAX

      private static final int DECODE_MAX
      Largest encoded-word the decoder accepts. It could be 75 for quoted-printable only, but base64 words are longer.
      See Also:
  • Constructor Details

    • HeaderLine

      public HeaderLine()
      Constructor. A header line is decoded from the raw header text handed to each call, so a bare instance is ready to use.
  • Method Details

    • getName

      public String getName()
      The header's name.
      Specified by:
      getName in class Encoding
      Returns:
      the constant NAME, "HEADERLINE"
    • encode

      public String encode(byte[] in) throws EncodingException
      This will split multibyte chars across lines, see 4th ref above.
      Specified by:
      encode in class Encoding
      Parameters:
      in - the byte array to encode
      Returns:
      the encoded string, SMTP-safe
      Throws:
      UnsupportedOperationException - always, since this byte[] overload is not implemented; use encode(String)
      EncodingException - if encoding fails
    • isWhiteSpace

      private static boolean isWhiteSpace(char c)
      Whether the char is header whitespace.
      Parameters:
      c - the character to test
      Returns:
      true for space and tab only
    • isControlOrMultiByte

      private static boolean isControlOrMultiByte(char c)
      Whether the char is a control or non-ASCII character.
      Parameters:
      c - the character to test
      Returns:
      true for control characters below 32 except space and tab, and for characters 127 and above
    • isPSpecial

      private static boolean isPSpecial(char c)
      Whether the char is an RFC 2047 special.
      Parameters:
      c - the character to test
      Returns:
      true for one of ! * + - / = _
    • isPSafe

      private static boolean isPSafe(char c)
      Whether the char is safe unencoded in an encoded-word.
      Parameters:
      c - the character to test
      Returns:
      true for letters, digits and the RFC 2047 specials
    • encode

      public String encode(String str) throws EncodingException
      Encode a single header line ONLY. Do NOT include the \r\n. Returns a string of one or more lines including the trailing \r\n. Field-name will not be encoded, must be less than 62 chars. The fieldBody is treated as "unstructured text", which is suitable only for the field names "Subject" and "Comments". We do NOT tokenize into structured fields. To make things easy, we either encode the whole field body as RFC 2047, or don't encode at all. If it's too long for a single line, we encode it, even if we didn't otherwise have to. We don't do quoted-string. This will not split multibyte chars, including supplementary chars, across lines.
      Overrides:
      encode in class Encoding
      Parameters:
      str - must start with "field-name: ", must have non-whitespace after that
      Returns:
      the encoded string, SMTP-safe
      Throws:
      EncodingException - if encoding fails
      See Also:
    • malformedText

      static String malformedText(String situation, String action)
      Describes a malformed encoded-word: what was wrong with it and what the decoder did about it. The header text is not quoted back, since it is what the reader is trying to fix, and a hex dump of the bytes would run to many lines for a per-header message.
      Parameters:
      situation - what was wrong with the encoded-word
      action - what the decoder did about it
      Returns:
      the log message
    • decode

      public void decode(InputStream in, Buffer bout) throws IOException
      Decode all the header lines, up through \r\n\r\n, and puts them in the ReadBuffer, including the \r\n\r\n.
      Specified by:
      decode in class Encoding
      Parameters:
      in - the input stream to read data from
      bout - the buffer to write decoded data to
      Throws:
      IOException - if reading from input stream fails
      See Also: