KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > emb > GenericMediaFormat


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license. See terms of license at gnu.org.
5  */

6
7 package javax.emb;
8
9 import java.io.InputStream;
10 import java.net.URL;
11
12 /**
13  * This class offers a generic implementation of the {@link MediaFormat}
14  * interface for use with all kinds of embedded media formats. Note that this
15  * class should not be used to represent non-embedded media formats, as it does
16  * not handle parent/child relationships. By registering instances of this
17  * class with the {@link MediaFormatRegistry}described in the followup
18  * section, various media formats can be supported in case no format specific
19  * support is available.
20  *
21  * <p>The public constant <code>DEFAULT_PROXY</code> contains an image that
22  * can be used as a default proxy for all kinds of media formats or in case the
23  * generation of a proxy fails for whatever reason.
24  *
25  * @version <tt>$Revision: 1.4 $</tt>
26  * @author <a HREF="mailto:ricardoarguello@users.sourceforge.net">Ricardo
27  * Argüello</a>
28  */

29 public class GenericMediaFormat implements MediaFormat
30 {
31    // TODO: Implement a default proxy
32
public static final Media DEFAULT_PROXY = null;
33
34    /**
35     * Returns the content of the given media segment array's first element, or
36     * an empty byte array if no element is passed.
37     *
38     * @param mediaLocation the intended location of the media to be assembled.
39     * @param mediaSegments the segments that are to be assembled.
40     * @return the assembled content.
41     * @throws javax.emb.FormatSyntaxException if the given media segment array
42     * contains more than one element, or if an element is passed that
43     * contains a child link that is not <code>null</code>.
44     * @throws java.lang.NullPointerException if the given segment array is
45     * <code>null</code>.
46     * @throws javax.emb.ContentTooLargeException if the assembled content is
47     * larger than the maximum Java array size of 2 GB.
48     */

49    public byte[] assembleContent(
50       URL mediaLocation,
51       MediaSegment[] mediaSegments)
52       throws MediaException
53    {
54       if (mediaSegments == null)
55       {
56          throw new NullPointerException();
57       }
58
59       if (mediaSegments.length == 0)
60       {
61          return new byte[0];
62       }
63
64       if (mediaSegments.length > 1)
65       {
66          throw new FormatSyntaxException("Media segment array contains more than one element.");
67       }
68
69       MediaSegment mediaSegment = mediaSegments[0];
70
71       if (mediaSegment.getChildLocation() != null)
72       {
73          throw new FormatSyntaxException("Element passed contains a child link that is not null.");
74       }
75
76       return mediaSegment.getContent();
77    }
78
79    /**
80     * Disassembles the given media content. As embedded media doesn – t contain
81     * any links and therefore consists of a single media segment, this generic
82     * implementation returns a one-element array of media segments, with a
83     * segment element composed of <code>null</code> as child location and the
84     * given media content as content.
85     *
86     * @param mediaLocation the location of the media to be disassembled.
87     * @param mediaContent the content to be disassembled.
88     * @return the segments.
89     * @throws java.lang.NullPointerException if the content passed is <code>null</code>.
90     */

91    public MediaSegment[] disassembleContent(
92       URL mediaLocation,
93       byte[] mediaContent)
94       throws MediaException
95    {
96       if (mediaContent == null)
97       {
98          throw new NullPointerException();
99       }
100
101       MediaSegment mediaSegment = new MediaSegment();
102       mediaSegment.setContent(mediaContent);
103
104       return new MediaSegment[] { mediaSegment };
105    }
106
107    /**
108     * Returns an instance of class GenericMediaHeader, as this class cannot
109     * make any assumptions about header fields.
110     *
111     * @param content the content stream to extract the header from.
112     * @return the media header.
113     * @throws java.lang.NullPointerException if the value passed is <code>null</code>.
114     */

115    public MediaHeader extractHeader(InputStream mediaContent)
116       throws MediaException
117    {
118       if (mediaContent == null)
119       {
120          throw new NullPointerException();
121       }
122
123       return new GenericMediaHeader();
124    }
125
126    /**
127     * Returns a default image that is suitable to represent all kinds of
128     * content.
129     *
130     * @param mediaContent the media content.
131     * @return the proxy.
132     * @throws javax.emb.MediaException if proxy can't be generated.
133     */

134    public Media extractProxy(InputStream mediaContent) throws MediaException
135    {
136       // TODO: Return a generic surrogate image, use a static byte array.
137
return null;
138    }
139
140    /**
141     * Returns <code>Media.MIME_TYPE_UNKNOWN</code>, as this class cannot
142     * make any assumptions about mime types.
143     *
144     * @return <code>Media.MIME_TYPE_UNKNOWN</code>.
145     */

146    public String getDefaultMimeType()
147    {
148       return Media.MIME_TYPE_UNKNOWN;
149    }
150
151    /**
152     * Returns <code>true</code>, as this class assumes media to be embedded.
153     *
154     * @return <code>true</code>.
155     */

156    public boolean isEmbedded()
157    {
158       return true;
159    }
160
161    /**
162     * Returns <code>false</code>, as this class cannot make any assumptions
163     * about media to be streamable or not.
164     *
165     * @return <code>false</code>.
166     */

167    public boolean isStreamingDesirable()
168    {
169       return false;
170    }
171 }
Popular Tags