javax.emb
Class MediaBean

java.lang.Object
  extended byjavax.emb.MediaBean
All Implemented Interfaces:
Media (src) , java.io.Serializable

public class MediaBean
extends java.lang.Object
implements Media (src)

This class implements the Media interface and models media objects that are transient, immutable, and local. In order to avoid excessive memory use, a temporary file may be used to store and access the media content while an instance is alive.

See Also:
Serialized Form

Field Summary
 
Fields inherited from interface javax.emb.Media (src)
MIME_TYPE_UNKNOWN
 
Constructor Summary
MediaBean(java.io.File mediaFile, java.lang.String mimeType)
          This constructor wraps a new instance around the given media file.
MediaBean(java.io.InputStream contentStream, java.lang.String mimeType, java.lang.String name)
          This constructor initializes a new instance with content copied from the given content stream, and the given mime type and name.
 
Method Summary
 byte[] getContent()
          Returns the complete media content as a byte array.
 MediaFormat (src) getFormat()
          Queries a suitable media format instance from the MediaFormatRegistry singleton and returns it.
 MediaHeader (src) getHeader()
          Returns the receiver's header information.
 java.lang.String getMimeType()
          Returns a mime type for the receiver's content as a string.
 java.lang.String getName()
          Returns the receiver's non-unique name as a String.
 Media (src) getProxy()
          Returns a media object that can be used as a proxy for the receiver, e.g.
 long getSize()
          This operation returns the receiver's content size in number of bytes as a long value.
 int readContent(long position, byte[] buffer)
          Similarly to input streams, this method fills the given buffer with content read from the media object.
 int readContent(long position, byte[] buffer, int offset, int length)
          Similar to input streams, this method fills the given buffer with content read from the media object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MediaBean

public MediaBean(java.io.InputStream contentStream,
                 java.lang.String mimeType,
                 java.lang.String name)
          throws MediaException (src) 
This constructor initializes a new instance with content copied from the given content stream, and the given mime type and name. The given name is a file name that should be associated with the object. If the given mimeType is null, one is chosen automatically by analyzing the given file extension, or Media.MIME_TYPE_UNKNOWN is assigned in case none can be derived.

This constructor is suitable for media content regardless of its size. Please note that the given input stream is closed once the content is read completely. Also note that a temporary file is used internally to maintain the content. The implementation tries to delete this file once the instance is garbage collected, and again once the process dies in case the former attempt fails.

Parameters:
contentStream - media content stream.
mimeType - media mime type.
name - media name.
Throws:
ContentAccessException (src) - if something goes wrong during content transfer.
java.lang.NullPointerException - if the given content stream or name is null.
MediaException (src)

MediaBean

public MediaBean(java.io.File mediaFile,
                 java.lang.String mimeType)
          throws MediaException (src) 
This constructor wraps a new instance around the given media file. The name property of the receiver is initialized to the name of the given file (without path information). If the given mimeType is null, one is chosen automatically by analyzing the file extension, or Media.MIME_TYPE_UNKNOWN is assigned in case none can be derived. This constructor is useful to invoke a media related service on an existing media file without copying the content of the latter. Please note that the file is not deleted once an instance referencing it is garbage collected.

Parameters:
mediaFile - a file.
mimeType - the optional MIME type of the content.
Throws:
ContentAccessException (src) - if something goes wrong during content transfer, or if the given file is not present or cannot be accessed.
java.lang.NullPointerException - if the given media file is null.
MediaException (src)
Method Detail

getContent

public byte[] getContent()
                  throws MediaException (src) 
Description copied from interface: Media (src)
Returns the complete media content as a byte array. This method is intended for convenient access to media content up to a few MB in size. Streaming I/O access as defined in readContent() should be used instead to access large content.

Specified by:
getContent in interface Media (src)
Returns:
the media content.
Throws:
MediaException (src)
See Also:
Media.getContent()

getFormat

public MediaFormat (src)  getFormat()
                      throws MediaException (src) 
Description copied from interface: Media (src)
Queries a suitable media format instance from the MediaFormatRegistry singleton and returns it. The file extension contained in the name property is used as a key for this query.

Specified by:
getFormat in interface Media (src)
Returns:
the media format.
Throws:
MediaException (src)
See Also:
Media.getFormat()

getHeader

public MediaHeader (src)  getHeader()
                      throws MediaException (src) 
Description copied from interface: Media (src)
Returns the receiver's header information. Queries a suitable media format instance from the MediaFormatRegistry singleton and determines the header information based on it.

Specified by:
getHeader in interface Media (src)
Returns:
the media header.
Throws:
MediaException (src)
See Also:
Media.getHeader()

getMimeType

public java.lang.String getMimeType()
                             throws MediaException (src) 
Description copied from interface: Media (src)
Returns a mime type for the receiver's content as a string. This allows media content to be written directly to a Servlet output stream, as this technique requires a mime type to be specified in case the output is different from HTML. If no mime type has been set explicitly a default mime type is returned, possibly based on the receiver's format.

Specified by:
getMimeType in interface Media (src)
Returns:
the mime type.
Throws:
MediaException (src)
See Also:
Media.getMimeType()

getName

public java.lang.String getName()
                         throws MediaException (src) 
Description copied from interface: Media (src)
Returns the receiver's non-unique name as a String. The name is used as a file name hint in case the media content is to be stored in a file system and therefore may only contain characters that are valid in file names. It contains a file extension that represents the receiver's media format.

Specified by:
getName in interface Media (src)
Returns:
the media name.
Throws:
MediaException (src)
See Also:
Media.getName()

getProxy

public Media (src)  getProxy()
               throws MediaException (src) 
Description copied from interface: Media (src)
Returns a media object that can be used as a proxy for the receiver, e.g. a thumbnail, an icon, a short video/audio clip. Servlets can use this operation whenever a thumbnail or sound bite is required to represent a given media and give users an up front impression of the content. The value returned may have any format and is never null.

Specified by:
getProxy in interface Media (src)
Returns:
the media proxy
Throws:
MediaException (src)
See Also:
Media.getProxy()

getSize

public long getSize()
             throws MediaException (src) 
Description copied from interface: Media (src)
This operation returns the receiver's content size in number of bytes as a long value.

Specified by:
getSize in interface Media (src)
Returns:
the media size.
Throws:
MediaException (src)
See Also:
Media.getSize()

readContent

public int readContent(long position,
                       byte[] buffer)
                throws MediaException (src) 
Description copied from interface: Media (src)
Similarly to input streams, this method fills the given buffer with content read from the media object. The media content is copied from the given position, and the buffer is filled beginning with offset 0. In case the buffer is larger than the amount of bytes available, it is only partially filled. The buffer may also be filled partially if the implementation uses non-blocking I/O. The method returns the number of bytes copied into the buffer, or -1 in case the given position equals the content size.

Specified by:
readContent in interface Media (src)
Parameters:
position - the position in media to copy from.
buffer - the byte array to store content.
Returns:
the number of bytes copied into the buffer.
Throws:
MediaException (src)
See Also:
Media.readContent(long, byte[])

readContent

public int readContent(long position,
                       byte[] buffer,
                       int offset,
                       int length)
                throws MediaException (src) 
Description copied from interface: Media (src)
Similar to input streams, this method fills the given buffer with content read from the media object. The media content is copied from the given position, and the buffer is filled beginning from the given offset with a maximum amount of bytes defined by the given length. In case the buffer is larger than the amount of bytes available, it is only partially filled. The buffer may also be filled partially if the implementation uses non-blocking I/O. The method returns the number of bytes copied into the buffer, or -1 in case the given position equals the content size.

Specified by:
readContent in interface Media (src)
Parameters:
position - the position in media to copy from.
buffer - the byte array to store content.
offset - the offset in the buffer to begin storing content.
length - the number of bytes to copy from the content.
Returns:
number of bytes copied into the buffer.
Throws:
MediaException (src)
See Also:
Media.readContent(long, byte[], int, int)