Package com.helger.css.utils
Class CSSDataURL
- java.lang.Object
-
- com.helger.css.utils.CSSDataURL
-
@NotThreadSafe public class CSSDataURL extends Object
This class represents a single CSS data URL (RFC 2397).- Author:
- Philip Helger
-
-
Constructor Summary
Constructors Constructor Description CSSDataURL()Default constructor.CSSDataURL(com.helger.commons.mime.IMimeType aMimeType, boolean bBase64Encoded, byte[] aContent)ConstructorCSSDataURL(com.helger.commons.mime.IMimeType aMimeType, boolean bBase64Encoded, byte[] aContent, Charset aCharset, String sContent)Full constructor
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(Object o)StringgetAsString()StringgetAsString(boolean bOptimizedVersion)CharsetgetCharset()static CharsetgetCharsetFromMimeTypeOrDefault(com.helger.commons.mime.IMimeType aMimeType)Determine the charset from the passed MIME type.StringgetContentAsBase64EncodedString()StringgetContentAsString()Get the data content of this Data URL as String.StringgetContentAsString(Charset aCharset)Get the data content of this Data URL as String in the specified charset.byte[]getContentBytes()Get a copy of all content bytes.intgetContentLength()com.helger.commons.mime.IMimeTypegetMimeType()inthashCode()booleanisBase64Encoded()StringtoString()voidwriteContentBytes(OutputStream aOS)Write all the binary content to the passed output stream.
-
-
-
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-trueif the content of this data should be Base64 encoded. It is recommended to set this totrueif you have binary data like images.aContent- The content of the data URL as a byte array. May not benull.
-
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 benull. If you don't know provide the default MIME type fromCSSDataURLHelper.DEFAULT_MIME_TYPE.bBase64Encoded-trueif the data URL String representation should be Base64 encoded,falseif not. It is recommended to set this totrueif you have binary data like images.aContent- The content of the data URL as a byte array. May not benullbut may be empty. This content may not be Base64 encoded!aCharset- The charset to be used to encode the String. May not benull. The default isCSSDataURLHelper.DEFAULT_CHARSET.sContent- The String representation of the content. It must match the byte array in the specified charset. If this parameter isnullthan the String content representation is lazily created ingetContentAsString().
-
-
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_TYPEmust be used.
-
isBase64Encoded
public boolean isBase64Encoded()
- Returns:
trueif 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
nullbut 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 benull.- Throws:
IOException- from OutputStream
-
getCharset
@Nonnull public Charset getCharset()
- Returns:
- The charset to be used for String encoding. May not be
null. The default isCSSDataURLHelper.DEFAULT_CHARSET.
-
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-nullString 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.
-
getContentAsBase64EncodedString
@Nonnull public String getContentAsBase64EncodedString()
Get the content as a Base64 encodedStringin theCharsetspecified bygetCharset(). The encoding is applied independent of theisBase64Encoded()state.- Returns:
- 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 benull.- 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-trueto 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.
-
-