public class AMedia extends Object implements Media, Serializable
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).
| Modifier and Type | Field and Description |
|---|---|
protected InputStream |
_isdata
The input stream,
getStreamData() |
protected Reader |
_rddata
The input stream,
getReaderData() |
protected static Reader |
DYNAMIC_READER
Used if you want to implement a media whose reader is created
dynamically each time
getReaderData() is called. |
protected static InputStream |
DYNAMIC_STREAM
Used if you want to implement a media whose input stream is created
dynamically each time
getStreamData() is called. |
| Constructor and Description |
|---|
AMedia(File file,
String ctype,
String charset)
Construct with a file.
|
AMedia(String name,
String format,
String ctype,
byte[] data)
Construct with name, format, content type and binary data.
|
AMedia(String name,
String format,
String ctype,
File file,
boolean binary)
Construct with name, format, content type and a file.
|
AMedia(String name,
String format,
String ctype,
File file,
String charset)
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).
|
AMedia(String name,
String format,
String ctype,
Reader data)
Construct with name, format, content type and reader data (textual).
|
AMedia(String name,
String format,
String ctype,
String data)
Construct with name, format, content type and text data.
|
AMedia(String name,
String format,
String ctype,
URL url,
String charset)
Construct with name, format, content type and URL.
|
AMedia(URL url,
String ctype,
String charset)
Construct with a file.
|
| Modifier and Type | Method and Description |
|---|---|
byte[] |
getByteData()
Returns the raw data in byte array.
|
String |
getContentType()
Returns the content type, e.g., "image/jpeg", or null if not available.
|
String |
getFormat()
Returns the format name, e.g., "jpeg", or null if not available.
|
String |
getName()
Returns the name (usually filename) of this media, or null
if not available.
|
Reader |
getReaderData()
Returns the reader of this media to retrieve the data.
|
InputStream |
getStreamData()
Returns the input stream of this media.
|
String |
getStringData()
Returns the raw data in string.
|
boolean |
inMemory()
Returns whether the data is cached in memory (in form of
byte[] or String).
|
boolean |
isBinary()
Returns whether the format of this content is binary or text-based.
|
boolean |
isContentDisposition()
Whether to allow Content-Disposition or not when writing the media to response header.
|
void |
setContentDisposition(boolean cntDisposition)
Set whether to allow Content-Disposition
or not when writing the media to response header.
|
String |
toString() |
protected static final InputStream DYNAMIC_STREAM
getStreamData() is called.AMedia(String,String,String,InputStream)protected static final Reader DYNAMIC_READER
getReaderData() is called.AMedia(String,String,String,Reader)protected transient InputStream _isdata
getStreamData()protected transient Reader _rddata
getReaderData()public AMedia(String name, String format, String ctype, byte[] data)
It tries to construct format and ctype from each other or name.
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 nullpublic AMedia(String name, String format, String ctype, String data)
It tries to construct format and ctype from each other or name.
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 nullpublic AMedia(String name, String format, String ctype, InputStream data)
It tries to construct format and ctype from each other or name.
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 time getStreamData()
is called, you shall pass DYNAMIC_STREAM
as the data argument. Then, override getStreamData() to return
the correct stream.
Note: the caller of getStreamData() has to close
the returned input stream.public AMedia(String name, String format, String ctype, Reader data)
It tries to construct format and ctype from each other or name.
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 time getReaderData()
is called, you shall pass DYNAMIC_READER
as the data argument. Then, override getReaderData() to return
the correct reader.public AMedia(String name, String format, String ctype, File file, boolean binary) throws FileNotFoundException
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 of InputStream.read()
after InputStream.close() is called.
See also RepeatableInputStream and RepeatableReader.
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.FileNotFoundExceptionpublic AMedia(String name, String format, String ctype, File file, String charset) throws FileNotFoundException
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 of InputStream.read()
after InputStream.close() is called.
See also RepeatableInputStream and RepeatableReader.
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.FileNotFoundExceptionpublic AMedia(File file, String ctype, String charset) throws FileNotFoundException
ctype - the content type; might be null.
If null, it is retrieved from the file name's extension.FileNotFoundExceptionpublic AMedia(String name, String format, String ctype, URL url, String charset) throws FileNotFoundException
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 of InputStream.read()
after InputStream.close() is called.
See also RepeatableInputStream and RepeatableReader.
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.FileNotFoundExceptionpublic AMedia(URL url, String ctype, String charset) throws FileNotFoundException
ctype - the content type; might be null.
If null, it is retrieved from the file name's extension.FileNotFoundExceptionpublic void setContentDisposition(boolean cntDisposition)
public boolean isBinary()
MediaMedia.getByteData() or Media.getStreamData()
to retrieve its content.
If false, use Media.getStringData() or Media.getReaderData()
to retrieve its content.isBinary in interface MediaMedia.getStringData(),
Media.getByteData(),
Media.getReaderData(),
Media.getStreamData()public boolean inMemory()
MediainMemory in interface MediaMedia.getStringData(),
Media.getByteData(),
Media.getReaderData(),
Media.getStreamData()public byte[] getByteData()
MediaIt 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 from Media.getStreamData(). Furthermore, it
also implies you can not invoke this method again.
getByteData in interface MediaMedia.getStringData()public String getStringData()
MediaIf the data is not cached in memory (Media.inMemory() return false),
the data will be read from Media.getReaderData(). Furthermore, it
also implies you can not invoke this method again.
getStringData in interface MediaMedia.getByteData()public InputStream getStreamData()
Note: the caller has to invoke InputStream.close()
after using the input stream returned by getStreamData().
getStreamData in interface MediaIllegalStateException - if the media is not binary
isBinary().Media.getReaderData()public Reader getReaderData()
Note: the caller has to invoke Reader.close()
after using the input stream returned by getReaderData().
getReaderData in interface MediaIllegalStateException - if the media is binary
isBinary().Media.getStreamData()public String getName()
Mediapublic String getFormat()
MediagetFormat in interface MediaMedia.getContentType()public String getContentType()
MediagetContentType in interface MediaMedia.getFormat()public boolean isContentDisposition()
MediaDefault: true
isContentDisposition in interface MediaCopyright © 2018. All rights reserved.