Class AMedia
- All Implemented Interfaces:
Serializable,Media
AMedia is serializable, but, if you are using InputStream or Reader,
you have to extend this class, and provide the implementation to
serialize and deserialize _isdata or _rddata
(they are both transient).
- Author:
- tomyeh
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected InputStreamThe input stream,getStreamData()protected ReaderThe input stream,getReaderData()protected static final ReaderUsed if you want to implement a media whose reader is created dynamically each timegetReaderData()is called.protected static final InputStreamUsed if you want to implement a media whose input stream is created dynamically each timegetStreamData()is called. -
Constructor Summary
ConstructorsConstructorDescriptionConstruct with a file.Construct with name, format, content type and binary data.Construct with name, format, content type and a file.Construct with name, format, content type and a file.AMedia(String name, String format, String ctype, InputStream data) Construct with name, format, content type and stream data (binary).Construct with name, format, content type and reader data (textual).Construct with name, format, content type and text data.Construct with name, format, content type and URL.Construct with a file. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]Returns the raw data in byte array.Returns the content type, e.g., "image/jpeg", or null if not available.Returns the format name, e.g., "jpeg", or null if not available.getName()Returns the name (usually filename) of this media, or null if not available.Returns the reader of this media to retrieve the data.Returns the input stream of this media.Returns the raw data in string.booleaninMemory()Returns whether the data is cached in memory (in form of byte[] or String).booleanisBinary()Returns whether the format of this content is binary or text-based.booleanWhether to allow Content-Disposition or not when writing the media to response header.voidsetContentDisposition(boolean cntDisposition) Set whether to allow Content-Disposition or not when writing the media to response header.toString()
-
Field Details
-
DYNAMIC_STREAM
Used if you want to implement a media whose input stream is created dynamically each timegetStreamData()is called.- See Also:
-
DYNAMIC_READER
Used if you want to implement a media whose reader is created dynamically each timegetReaderData()is called.- See Also:
-
_isdata
The input stream,getStreamData() -
_rddata
The input stream,getReaderData()
-
-
Constructor Details
-
AMedia
Construct with name, format, content type and binary data.It tries to construct format and ctype from each other or name.
- Parameters:
name- the name (usually filename); might be null.format- the format; might be null. Example: "html" and "xml"ctype- the content type; might be null. Example: "text/html" and "text/xml;charset=UTF-8".data- the binary data; never null
-
AMedia
Construct with name, format, content type and text data.It tries to construct format and ctype from each other or name.
- Parameters:
name- the name (usually filename); might be null.format- the format; might be null.ctype- the content type; might be null.data- the text data; never null
-
AMedia
Construct with name, format, content type and stream data (binary).It tries to construct format and ctype from each other or name.
- Parameters:
name- the name (usually filename); might be null.format- the format; might be null.ctype- the content type; might be null.data- the binary data; never null. If the input stream is created dynamically each timegetStreamData()is called, you shall passDYNAMIC_STREAMas the data argument. Then, overridegetStreamData()to return the correct stream. Note: the caller ofgetStreamData()has to close the returned input stream.
-
AMedia
Construct with name, format, content type and reader data (textual).It tries to construct format and ctype from each other or name.
- Parameters:
name- the name (usually filename); might be null.format- the format; might be null.ctype- the content type; might be null.data- the string data; never null If the reader is created dynamically each timegetReaderData()is called, you shall passDYNAMIC_READERas the data argument. Then, overridegetReaderData()to return the correct reader.
-
AMedia
public AMedia(String name, String format, String ctype, File file, boolean binary) throws FileNotFoundException Construct with name, format, content type and a file.Unlike others, it uses the so-called repeatable input stream or reader (depending on binary or not) to represent the file, so the input stream (
getStreamData()) or the reader (getReaderData()) will be re-opened in the next invocation ofInputStream.read()afterInputStream.close()is called. See alsoRepeatableInputStreamandRepeatableReader.- Parameters:
name- the name (usually filename); might be null. If null, the file name is used.format- the format; might be null.ctype- the content type; might be null.file- the file; never null.binary- whether it is binary. If not binary, "UTF-8" is assumed.- Throws:
FileNotFoundException
-
AMedia
public AMedia(String name, String format, String ctype, File file, String charset) throws FileNotFoundException Construct with name, format, content type and a file.Unlike others, it uses the so-called repeatable input stream or reader (depending on charset is null or not) to represent the file, so the input stream (
getStreamData()) or the reader (getReaderData()) will be re-opened in the next invocation ofInputStream.read()afterInputStream.close()is called. See alsoRepeatableInputStreamandRepeatableReader.- Parameters:
name- the name (usually filename); might be null. If null, the file name is used.format- the format; might be null.ctype- the content type; might be null.file- the file; never null.charset- the charset. If null, it is assumed to be binary.- Throws:
FileNotFoundException
-
AMedia
Construct with a file. It is the same as AMedia(null, null, ctype, file, charset).- Parameters:
ctype- the content type; might be null. If null, it is retrieved from the file name's extension.- Throws:
FileNotFoundException- Since:
- 3.0.8
-
AMedia
public AMedia(String name, String format, String ctype, URL url, String charset) throws FileNotFoundException Construct with name, format, content type and URL.Unlike others, it uses the so-called repeatable input stream or reader (depending on charset is null or not) to represent the resource, so the input stream (
getStreamData()) or the reader (getReaderData()) will be re-opened in the next invocation ofInputStream.read()afterInputStream.close()is called. See alsoRepeatableInputStreamandRepeatableReader.- Parameters:
name- the name; might be null. If null, URL's name is used.format- the format; might be null.ctype- the content type; might be null.url- the resource URL; never null.- Throws:
FileNotFoundException- Since:
- 3.0.8
-
AMedia
Construct with a file. It is the same as AMedia(null, null, ctype, url, charset).- Parameters:
ctype- the content type; might be null. If null, it is retrieved from the file name's extension.- Throws:
FileNotFoundException- Since:
- 3.0.8
-
-
Method Details
-
setContentDisposition
public void setContentDisposition(boolean cntDisposition) Set whether to allow Content-Disposition or not when writing the media to response header.- Since:
- 7.0.0
-
isBinary
public boolean isBinary()Description copied from interface:MediaReturns whether the format of this content is binary or text-based. If true, useMedia.getByteData()orMedia.getStreamData()to retrieve its content. If false, useMedia.getStringData()orMedia.getReaderData()to retrieve its content. -
inMemory
public boolean inMemory()Description copied from interface:MediaReturns whether the data is cached in memory (in form of byte[] or String). -
getByteData
public byte[] getByteData()Description copied from interface:MediaReturns the raw data in byte array.It might not be a copy, so don't modify it directly unless you know what you are doing.
If the data is not cached in memory (
Media.inMemory()return false), the data will be read fromMedia.getStreamData(). Furthermore, it also implies you can not invoke this method again.- Specified by:
getByteDatain interfaceMedia- See Also:
-
getStringData
Description copied from interface:MediaReturns the raw data in string.If the data is not cached in memory (
Media.inMemory()return false), the data will be read fromMedia.getReaderData(). Furthermore, it also implies you can not invoke this method again.- Specified by:
getStringDatain interfaceMedia- See Also:
-
getStreamData
Returns the input stream of this media.Note: the caller has to invoke
InputStream.close()after using the input stream returned by this method.It wraps
getByteData()withByteArrayInputStreamifinMemory()returns true.- Specified by:
getStreamDatain interfaceMedia- Throws:
IllegalStateException- if the mediaisBinary()returns false- See Also:
-
getReaderData
Returns the reader of this media to retrieve the data.Note: the caller has to invoke
Reader.close()after using the input stream returned by this method.It wraps
getStringData()withStringReader, ifinMemory()returns true.- Specified by:
getReaderDatain interfaceMedia- Throws:
IllegalStateException- ifisBinary()returns true- See Also:
-
getName
Description copied from interface:MediaReturns the name (usually filename) of this media, or null if not available. -
getFormat
Description copied from interface:MediaReturns the format name, e.g., "jpeg", or null if not available. -
getContentType
Description copied from interface:MediaReturns the content type, e.g., "image/jpeg", or null if not available.- Specified by:
getContentTypein interfaceMedia- See Also:
-
isContentDisposition
public boolean isContentDisposition()Description copied from interface:MediaWhether to allow Content-Disposition or not when writing the media to response header.Default: true
- Specified by:
isContentDispositionin interfaceMedia
-
toString
-