KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > media > NIOMediaBean


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

7
8 package org.jboss.media;
9
10 import java.io.ByteArrayInputStream JavaDoc;
11 import java.io.File JavaDoc;
12 import java.io.FileInputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.nio.ByteBuffer JavaDoc;
16 import java.nio.channels.Channels JavaDoc;
17 import java.nio.channels.FileChannel JavaDoc;
18 import java.nio.channels.ReadableByteChannel JavaDoc;
19
20 import javax.emb.ContentAccessException;
21 import javax.emb.Media;
22 import javax.emb.MediaException;
23 import javax.emb.MediaFormat;
24 import javax.emb.MediaFormatRegistry;
25 import javax.emb.MediaHeader;
26
27 import org.jboss.media.util.ByteBufferUtils;
28
29 /**
30  * An implementation of the <code>Media</code> interface using Java2 1.4's new I/O API.
31  *
32  * This class is not in use yet...
33  *
34  * @version <tt>$Revision: 1.2 $</tt>
35  * @author <a HREF="mailto:ricardoarguello@users.sourceforge.net">Ricardo Argüello</a>
36  */

37 public class NIOMediaBean implements Media
38 {
39    private String JavaDoc name;
40    private String JavaDoc mimeType;
41
42    // Check if it's direct? (shouldn't be)
43
private ByteBuffer JavaDoc byteBuffer;
44
45    private long size;
46
47    public NIOMediaBean(InputStream JavaDoc contentStream, String JavaDoc mimeType, String JavaDoc name)
48       throws MediaException
49    {
50       if (contentStream == null || name == null)
51       {
52          throw new NullPointerException JavaDoc();
53       }
54
55       if (mimeType == null)
56       {
57          mimeType = getFormat().getDefaultMimeType();
58       }
59       else
60       {
61          this.mimeType = mimeType;
62       }
63
64       this.name = name;
65
66       ReadableByteChannel JavaDoc byteChannel = Channels.newChannel(contentStream);
67
68       try
69       {
70          this.size = byteChannel.read(byteBuffer); // uh?
71
}
72       catch (IOException JavaDoc e)
73       {
74          throw new ContentAccessException(e.getMessage());
75       }
76       finally
77       {
78          try
79          {
80             contentStream.close();
81          }
82          catch (IOException JavaDoc exception)
83          {
84          }
85       }
86    }
87
88    public NIOMediaBean(File JavaDoc mediaFile, String JavaDoc mimeType) throws MediaException
89    {
90       try
91       {
92          // Hummmm: FileChannel fileChannel = new RandomAccessFile(mediaFile, "r").getChannel();
93
FileChannel JavaDoc fileChannel = new FileInputStream JavaDoc(mediaFile).getChannel();
94          this.size = fileChannel.size();
95          this.byteBuffer =
96             fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, (int) size);
97       }
98       catch (IOException JavaDoc e)
99       {
100          throw new ContentAccessException(e.getMessage());
101       }
102    }
103
104    public byte[] getContent() throws MediaException
105    {
106       return byteBuffer.array();
107    }
108
109    public int readContent(long position, byte[] buffer) throws MediaException
110    {
111       return readContent(position, buffer, 0, buffer.length);
112    }
113
114    public int readContent(long position, byte[] buffer, int offset, int length)
115       throws MediaException
116    {
117       byteBuffer.get(buffer, offset, length);
118
119       // This is not correct, find a better way!
120
return buffer.length;
121    }
122
123    public MediaFormat getFormat() throws MediaException
124    {
125       String JavaDoc fileExtension = getFileExtension(name);
126
127       if (fileExtension != null)
128       {
129          return MediaFormatRegistry.SINGLETON.lookup(fileExtension);
130       }
131       else
132       {
133          return null;
134       }
135    }
136
137    public MediaHeader getHeader() throws MediaException
138    {
139       InputStream JavaDoc content = new ByteArrayInputStream JavaDoc(this.getContent());
140       return getFormat().extractHeader(content);
141    }
142
143    public String JavaDoc getMimeType() throws MediaException
144    {
145       return mimeType;
146    }
147
148    public String JavaDoc getName() throws MediaException
149    {
150       return name;
151    }
152
153    public long getSize() throws MediaException
154    {
155       return size;
156    }
157
158    public Media getProxy() throws MediaException
159    {
160       InputStream JavaDoc content = new ByteArrayInputStream JavaDoc(this.getContent());
161       return this.getFormat().extractProxy(content);
162    }
163
164    // Private ------------------------------------------
165
private static String JavaDoc getFileName(String JavaDoc fileName)
166    {
167       int lastDotPosition = fileName.lastIndexOf('.');
168
169       if (lastDotPosition == -1)
170       {
171          return fileName;
172       }
173       else
174       {
175          return fileName.substring(0, lastDotPosition);
176       }
177    }
178
179    private static String JavaDoc getFileExtension(String JavaDoc fileName)
180    {
181       int lastDotPosition = fileName.lastIndexOf('.');
182
183       if (lastDotPosition == -1)
184       {
185          return null;
186       }
187       else
188       {
189          return fileName.substring(lastDotPosition + 1);
190       }
191    }
192
193    private InputStream JavaDoc getContentStream() throws MediaException
194    {
195       return ByteBufferUtils.newInputStream(byteBuffer);
196    }
197 }
Popular Tags