1 7 8 package javax.emb; 9 10 import java.io.File; 11 import java.io.FileInputStream; 12 import java.io.FileNotFoundException; 13 import java.io.FileOutputStream; 14 import java.io.IOException; 15 import java.io.InputStream; 16 import java.io.OutputStream; 17 import java.io.RandomAccessFile; 18 19 29 public class MediaBean implements Media 30 { 31 32 private String name; 33 34 35 private String mimeType; 36 37 38 private transient File tempFile; 39 40 63 public MediaBean(InputStream contentStream, String mimeType, String name) 64 throws MediaException 65 { 66 if (contentStream == null || name == null) 67 { 68 throw new NullPointerException(); 69 } 70 71 this.name = name; 72 73 if (mimeType != null) 74 { 75 this.mimeType = mimeType; 76 } 77 else 78 { 79 try 80 { 81 MediaFormatRegistry.SINGLETON.lookup(getFileExtension(name)); 83 } 84 catch (FormatNotFoundException e) 85 { 86 this.mimeType = Media.MIME_TYPE_UNKNOWN; 87 } 88 } 89 90 String tempFilePrefix = getFileName(name); 91 String tempFileSuffix = getFileExtension(name); 92 93 try 94 { 95 tempFile = File.createTempFile(tempFilePrefix, tempFileSuffix); 97 tempFile.deleteOnExit(); 98 99 OutputStream tempFileStream = new FileOutputStream(tempFile); 101 102 try 103 { 104 int DEFAULT_BUFFER_SIZE = 65536; 105 byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; 106 int bytesRead; 107 108 while ((bytesRead = contentStream.read(buffer)) != -1) 110 { 111 tempFileStream.write(buffer, 0, bytesRead); 112 } 113 } 114 catch (IOException e) 115 { 116 throw new ContentAccessException(e.getMessage()); 118 } 119 finally 120 { 121 try 122 { 123 tempFileStream.close(); 124 } 125 catch (IOException ignore) 126 { 127 } 128 } 129 } 130 catch (IOException e) 131 { 132 throw new ContentAccessException(e.getMessage()); 134 } 135 finally 136 { 137 try 138 { 139 contentStream.close(); 140 } 141 catch (IOException ignore) 142 { 143 } 144 } 145 } 146 147 166 public MediaBean(File mediaFile, String mimeType) throws MediaException 167 { 168 if (mediaFile == null) 169 { 170 throw new NullPointerException(); 171 } 172 173 if (!mediaFile.exists()) 174 { 175 throw new ContentAccessException("The given file is not present or cannot be accessed."); 176 } 177 178 this.tempFile = mediaFile; 179 this.name = mediaFile.getName(); 180 181 if (mimeType != null) 182 { 183 this.mimeType = mimeType; 184 } 185 else 186 { 187 try 188 { 189 MediaFormatRegistry.SINGLETON.lookup(getFileExtension(name)); 191 } 192 catch (FormatNotFoundException e) 193 { 194 this.mimeType = Media.MIME_TYPE_UNKNOWN; 195 } 196 } 197 } 198 199 202 public byte[] getContent() throws MediaException 203 { 204 long size = getSize(); 205 206 if (size > Integer.MAX_VALUE) 207 { 208 throw new ContentTooLargeException("Content exceeds maximum Java array size."); 209 } 210 211 return getContent(0, (int) size); 212 } 213 214 217 public MediaFormat getFormat() throws MediaException 218 { 219 String fileExtension = getFileExtension(name); 220 return MediaFormatRegistry.SINGLETON.lookup(fileExtension); 221 } 222 223 226 public MediaHeader getHeader() throws MediaException 227 { 228 return getFormat().extractHeader(getContentStream()); 229 } 230 231 234 public String getMimeType() throws MediaException 235 { 236 return mimeType; 237 } 238 239 242 public String getName() throws MediaException 243 { 244 return name; 245 } 246 247 250 public Media getProxy() throws MediaException 251 { 252 return getFormat().extractProxy(getContentStream()); 253 } 254 255 258 public long getSize() throws MediaException 259 { 260 return tempFile.length(); 261 } 262 263 266 public int readContent(long position, byte[] buffer) throws MediaException 267 { 268 return this.readContent(position, buffer, 0, buffer.length); 269 } 270 271 274 public int readContent(long position, byte[] buffer, int offset, int length) 275 throws MediaException 276 { 277 if (position < 0 || position > getSize()) 278 { 279 throw new IndexOutOfBoundsException(); 280 } 281 282 if (position < 0) 283 { 284 throw new NegativeArraySizeException(); 285 } 286 287 if (buffer == null) 288 { 289 throw new NullPointerException(); 290 } 291 292 294 int bytesRead = 0; 295 296 try 297 { 298 InputStream contentStream = new FileInputStream(tempFile); 299 long contentPosition = contentStream.skip(position); 300 bytesRead = contentStream.read(buffer, offset, length); 301 } 302 catch (IOException e) 303 { 304 throw new ContentAccessException(e.getMessage()); 305 } 306 307 return bytesRead; 308 } 309 310 private byte[] getContent(long position, int length) throws MediaException 311 { 312 if (position < 0 || position > getSize()) 313 { 314 throw new IndexOutOfBoundsException(); 315 } 316 317 int contentLength; 318 319 if ((getSize() - position) < (long) length) 320 { 321 contentLength = (int) (getSize() - position); 322 } 323 else 324 { 325 contentLength = length; 326 } 327 328 RandomAccessFile randomAccessFile = null; 329 330 try 331 { 332 randomAccessFile = new RandomAccessFile(tempFile, "r"); 333 334 byte[] content = new byte[contentLength]; 335 336 randomAccessFile.seek(position); 337 randomAccessFile.read(content); 338 339 return content; 340 } 341 catch (FileNotFoundException e) 342 { 343 throw new ContentAccessException(e.getMessage()); 344 } 345 catch (IOException e) 346 { 347 throw new ContentAccessException(e.getMessage()); 348 } 349 finally 350 { 351 if (randomAccessFile != null) 352 { 353 try 354 { 355 randomAccessFile.close(); 356 } 357 catch (IOException ignore) 358 { 359 } 360 } 361 } 362 } 363 364 376 private InputStream getContentStream() throws MediaException 377 { 378 try 379 { 380 return new FileInputStream(tempFile); 381 } 382 catch (FileNotFoundException e) 383 { 384 throw new ContentAccessException(e.getMessage()); 385 } 386 } 387 388 private static String getFileName(String name) 389 { 390 int lastDotPosition = name.lastIndexOf('.'); 391 392 if (lastDotPosition == -1) 393 { 394 return name; 395 } 396 else 397 { 398 return name.substring(0, lastDotPosition); 399 } 400 } 401 402 private static String getFileExtension(String name) 403 { 404 int lastDotPosition = name.lastIndexOf('.'); 405 406 if (lastDotPosition == -1) 407 { 408 return null; 409 } 410 else 411 { 412 return name.substring(lastDotPosition + 1); 413 } 414 } 415 } | Popular Tags |