1 7 8 package org.jboss.media; 9 10 import java.io.ByteArrayInputStream ; 11 import java.io.File ; 12 import java.io.FileInputStream ; 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.nio.ByteBuffer ; 16 import java.nio.channels.Channels ; 17 import java.nio.channels.FileChannel ; 18 import java.nio.channels.ReadableByteChannel ; 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 37 public class NIOMediaBean implements Media 38 { 39 private String name; 40 private String mimeType; 41 42 private ByteBuffer byteBuffer; 44 45 private long size; 46 47 public NIOMediaBean(InputStream contentStream, String mimeType, String name) 48 throws MediaException 49 { 50 if (contentStream == null || name == null) 51 { 52 throw new NullPointerException (); 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 byteChannel = Channels.newChannel(contentStream); 67 68 try 69 { 70 this.size = byteChannel.read(byteBuffer); } 72 catch (IOException e) 73 { 74 throw new ContentAccessException(e.getMessage()); 75 } 76 finally 77 { 78 try 79 { 80 contentStream.close(); 81 } 82 catch (IOException exception) 83 { 84 } 85 } 86 } 87 88 public NIOMediaBean(File mediaFile, String mimeType) throws MediaException 89 { 90 try 91 { 92 FileChannel fileChannel = new FileInputStream (mediaFile).getChannel(); 94 this.size = fileChannel.size(); 95 this.byteBuffer = 96 fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, (int) size); 97 } 98 catch (IOException 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 return buffer.length; 121 } 122 123 public MediaFormat getFormat() throws MediaException 124 { 125 String 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 content = new ByteArrayInputStream (this.getContent()); 140 return getFormat().extractHeader(content); 141 } 142 143 public String getMimeType() throws MediaException 144 { 145 return mimeType; 146 } 147 148 public String 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 content = new ByteArrayInputStream (this.getContent()); 161 return this.getFormat().extractProxy(content); 162 } 163 164 private static String getFileName(String 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 getFileExtension(String 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 getContentStream() throws MediaException 194 { 195 return ByteBufferUtils.newInputStream(byteBuffer); 196 } 197 } | Popular Tags |