Class MimeTypeParser

java.lang.Object
com.helger.commons.mime.MimeTypeParser

@Immutable public final class MimeTypeParser extends Object
This class handles the String parsing of MIME types.
Author:
Philip Helger
  • Method Details

    • getAllTSpecialChars

      @Nonnull @ReturnsMutableCopy public static char[] getAllTSpecialChars()
      Returns:
      A copy of the array with all TSpecial chars. Never null.
    • isTSpecialChar

      public static boolean isTSpecialChar(char c)
      Check if the passed character is a special character according to RFC 2045 chapter 5.1
      Parameters:
      c - The character to check
      Returns:
      true if the character is a special character, false otherwise.
    • isTokenChar

      public static boolean isTokenChar(char c)
      Check if the passed character is a valid token character. According to RFC 2045 this can be any (US-ASCII) CHAR except SPACE, CTLs, or tspecials
      Parameters:
      c - The character to check.
      Returns:
      true if the passed character is a valid token character, false otherwise
    • isToken

      public static boolean isToken(@Nullable String sToken)
      Check if the passed string is a valid MIME token by checking that the length is at least 1 and all chars match the isTokenChar(char) condition.
      Parameters:
      sToken - The token to be checked. May be null.
      Returns:
      true if the passed string is valid token, false otherwise.
    • parseMimeType

      @Nullable public static MimeType parseMimeType(@Nullable String sMimeType) throws MimeTypeParserException
      Try to convert the string representation of a MIME type to an object. The default quoting algorithm CMimeType.DEFAULT_QUOTING is used to unquote strings.
      Parameters:
      sMimeType - The string representation to be converted. May be null.
      Returns:
      null if the parsed string is empty.
      Throws:
      MimeTypeParserException - In case of an error
    • parseMimeType

      @Nullable public static MimeType parseMimeType(@Nullable String sMimeType, @Nonnull EMimeQuoting eQuotingAlgorithm) throws MimeTypeParserException
      Try to convert the string representation of a MIME type to an object. The default quoting algorithm CMimeType.DEFAULT_QUOTING is used to un-quote strings.
      Parameters:
      sMimeType - The string representation to be converted. May be null.
      eQuotingAlgorithm - The quoting algorithm to be used to un-quote parameter values. May not be null.
      Returns:
      null if the parsed string is empty.
      Throws:
      MimeTypeParserException - In case of an error
    • safeParseMimeType

      @Nullable public static MimeType safeParseMimeType(@Nullable String sMimeType)
      Try to convert the string representation of a MIME type to an object. The default quoting algorithm CMimeType.DEFAULT_QUOTING is used to unquote strings. Compared to parseMimeType(String) this method swallows all MimeTypeParserException and simply returns null. Additionally if the string is RFC 2616 encoded, it is decoded before parsing (since 9.4.6).
      Parameters:
      sMimeType - The string representation to be converted. May be null.
      Returns:
      null if the parsed string is empty or not a valid mime type.
    • safeParseMimeType

      @Nullable public static MimeType safeParseMimeType(@Nullable String sMimeType, @Nonnull EMimeQuoting eQuotingAlgorithm)
      Try to convert the string representation of a MIME type to an object. The default quoting algorithm CMimeType.DEFAULT_QUOTING is used to unquote strings. Compared to parseMimeType(String, EMimeQuoting) this method swallows all MimeTypeParserException and simply returns null. Additionally if the string is RFC 2616 encoded, it is decoded before parsing (since 9.4.6).
      Parameters:
      sMimeType - The string representation to be converted. May be null.
      eQuotingAlgorithm - The quoting algorithm to be used to un-quote parameter values. May not be null.
      Returns:
      null if the parsed string is empty or not a valid mime type.
      Since:
      9.4.6