Class CSSDataURL


  • @NotThreadSafe
    public class CSSDataURL
    extends Object
    This class represents a single CSS data URL (RFC 2397).
    Author:
    Philip Helger
    • Constructor Detail

      • CSSDataURL

        public CSSDataURL()
        Default constructor. Default MIME type, no Base64 encoding and no content.
      • CSSDataURL

        public CSSDataURL​(@Nonnull
                          com.helger.commons.mime.IMimeType aMimeType,
                          boolean bBase64Encoded,
                          @Nonnull
                          byte[] aContent)
        Constructor
        Parameters:
        aMimeType - The MIME type to be used. If it contains a charset, this charset will be used otherwise the default charset will be used.
        bBase64Encoded - true if the content of this data should be Base64 encoded. It is recommended to set this to true if you have binary data like images.
        aContent - The content of the data URL as a byte array. May not be null.
      • CSSDataURL

        public CSSDataURL​(@Nonnull
                          com.helger.commons.mime.IMimeType aMimeType,
                          boolean bBase64Encoded,
                          @Nonnull
                          byte[] aContent,
                          @Nonnull
                          Charset aCharset,
                          @Nullable
                          String sContent)
        Full constructor
        Parameters:
        aMimeType - The MIME type to be used. May not be null. If you don't know provide the default MIME type from CSSDataURLHelper.DEFAULT_MIME_TYPE.
        bBase64Encoded - true if the data URL String representation should be Base64 encoded, false if not. It is recommended to set this to true if you have binary data like images.
        aContent - The content of the data URL as a byte array. May not be null but may be empty. This content may not be Base64 encoded!
        aCharset - The charset to be used to encode the String. May not be null. The default is CSSDataURLHelper.DEFAULT_CHARSET.
        sContent - The String representation of the content. It must match the byte array in the specified charset. If this parameter is null than the String content representation is lazily created in getContentAsString().
    • Method Detail

      • getCharsetFromMimeTypeOrDefault

        @Nonnull
        public static Charset getCharsetFromMimeTypeOrDefault​(@Nullable
                                                              com.helger.commons.mime.IMimeType aMimeType)
        Determine the charset from the passed MIME type. If no charset was found, return the default charset.
        Parameters:
        aMimeType - The MIME type to investigate.
        Returns:
        Never null.
      • getMimeType

        @Nonnull
        public com.helger.commons.mime.IMimeType getMimeType()
        Returns:
        The MIME type of the data URL. If none was specified, than the default MIME Type CSSDataURLHelper.DEFAULT_MIME_TYPE must be used.
      • isBase64Encoded

        public boolean isBase64Encoded()
        Returns:
        true if the parsed data URL was Base64 encoded or if this data URL should be Base64 encoded.
      • getContentLength

        @Nonnegative
        public int getContentLength()
        Returns:
        The length of the content in bytes. Always ≥ 0.
      • getContentBytes

        @Nonnull
        @ReturnsMutableCopy
        public byte[] getContentBytes()
        Get a copy of all content bytes. No Base64 encoding is performed in this method.
        Returns:
        A copy of the binary data of the data URL. Neither null but maybe empty.
      • writeContentBytes

        public void writeContentBytes​(@Nonnull @WillNotClose
                                      OutputStream aOS)
                               throws IOException
        Write all the binary content to the passed output stream. No Base64 encoding is performed in this method.
        Parameters:
        aOS - The output stream to write to. May not be null.
        Throws:
        IOException - from OutputStream
      • getContentAsString

        @Nonnull
        public String getContentAsString()
        Get the data content of this Data URL as String. If no String representation was provided in the constructor, than it is lazily created inside this method in which case instances of this class are not thread-safe. If a non-null String was provided in the constructor, this object is immutable. No Base64 encoding is performed in this method.
        Returns:
        The content in a String representation using the charset of this object. Never null.
      • getContentAsString

        @Nonnull
        public String getContentAsString​(@Nonnull
                                         Charset aCharset)
        Get the data content of this Data URL as String in the specified charset. No Base64 encoding is performed in this method.
        Parameters:
        aCharset - The charset to be used. May not be null.
        Returns:
        The content in a String representation using the provided charset. Never null.
      • getAsString

        @Nonnull
        public String getAsString()
        Returns:
        The complete representation of the data URL, starting with "data:". All data is emitted, even if it is the default value. Base64 encoding is performed in this method.
      • getAsString

        @Nonnull
        public String getAsString​(boolean bOptimizedVersion)
        Parameters:
        bOptimizedVersion - true to create optimized version
        Returns:
        The complete representation of the data URL, starting with "data:". All data is emitted, even if it is the default value.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object