KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > media > format > image > iio > IIOMediaFormat


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

6
7 package org.jboss.media.format.image.iio;
8
9 import java.io.IOException JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.rmi.RemoteException JavaDoc;
12
13 import javax.emb.MediaException;
14 import javax.emb.MediaHeader;
15 import javax.imageio.ImageReader JavaDoc;
16 import javax.imageio.metadata.IIOMetadata JavaDoc;
17 import javax.imageio.stream.FileCacheImageInputStream JavaDoc;
18 import javax.imageio.stream.ImageInputStream JavaDoc;
19
20 import org.jboss.media.format.EmbeddedMediaFormat;
21
22 /**
23  * Implementation of the <code>MediaFormat</code> interface based on the Java
24  * Image I/O API to be used on embedded media.
25  *
26  * @version <tt>$Revision: 1.3 $</tt>
27  * @author <a HREF="mailto:ricardoarguello@users.sourceforge.net">Ricardo
28  * Argüello</a>
29  */

30 public abstract class IIOMediaFormat extends EmbeddedMediaFormat
31 {
32    protected ImageReader JavaDoc imageReader;
33
34    /**
35     * Constructor.
36     *
37     * @param imageReader
38     */

39    public IIOMediaFormat(ImageReader JavaDoc imageReader)
40    {
41       super();
42       this.imageReader = imageReader;
43    }
44
45    /**
46     * @see javax.emb.MediaFormat#extractHeader(javax.emb.Media)
47     */

48    public abstract MediaHeader extractHeader(InputStream JavaDoc content)
49       throws MediaException;
50
51    /**
52     * @see javax.emb.MediaFormat#getDefaultMimeType()
53     */

54    public String JavaDoc getDefaultMimeType()
55    
56    {
57       try
58       {
59          // FIXME: This is wrong!
60
return "image/" + imageReader.getFormatName();
61       } catch (IOException JavaDoc e)
62       {
63          // ignore?
64
return null;
65       }
66    }
67
68    /**
69     * @see javax.emb.MediaFormat#isStreamingDesirable()
70     */

71    public boolean isStreamingDesirable()
72    {
73       return false;
74    }
75
76    protected IIOMetadata JavaDoc extractImageMetadata(InputStream JavaDoc content)
77       throws MediaException
78    {
79       ImageInputStream JavaDoc imageInputStream = null;
80
81       try
82       {
83          imageInputStream = new FileCacheImageInputStream JavaDoc(content, null);
84
85          imageReader.setInput(imageInputStream);
86          IIOMetadata JavaDoc imageMetadata = imageReader.getImageMetadata(0);
87
88          return imageMetadata;
89       } catch (RemoteException JavaDoc re)
90       {
91          throw new RuntimeException JavaDoc(re.getMessage());
92       } catch (IOException JavaDoc ioe)
93       {
94          throw new MediaException(ioe.getMessage());
95       } finally
96       {
97          try
98          {
99             imageInputStream.close();
100             content.close();
101          } catch (IOException JavaDoc ignore)
102          {
103          }
104       }
105    }
106 }
Popular Tags