001 package railo.commons.activation; 002 003 // Imports 004 import java.io.IOException; 005 import java.io.InputStream; 006 import java.io.OutputStream; 007 008 import javax.activation.DataSource; 009 010 import railo.commons.io.IOUtil; 011 import railo.commons.io.res.Resource; 012 013 /** 014 * File Data Source. 015 */ 016 public final class ResourceDataSource implements DataSource { 017 018 /** 019 * File source. 020 */ 021 private final Resource _file; 022 023 /** 024 * Constructor of the class 025 * @param res source 026 */ 027 public ResourceDataSource(Resource res) { 028 _file = res; 029 } 030 031 /** 032 * Get name. 033 * @returns Name 034 */ 035 public String getName() { 036 return _file.getName(); 037 } 038 039 /** 040 * Get Resource. 041 * @returns Resource 042 */ 043 public Resource getResource() { 044 return _file; 045 } 046 047 /** 048 * Get input stream. 049 * @returns Input stream 050 * @throws IOException IO exception occurred 051 */ 052 public InputStream getInputStream() throws IOException { 053 return IOUtil.toBufferedInputStream(_file.getInputStream()); 054 } 055 056 /** 057 * Get content type. 058 * @returns Content type 059 */ 060 public String getContentType() { 061 InputStream is=null; 062 try { 063 return IOUtil.getMimeType(is=_file.getInputStream(), "application/unknow"); 064 } catch (IOException e) { 065 return "application/unknow"; 066 } 067 finally { 068 IOUtil.closeEL(is); 069 } 070 071 } 072 073 /** 074 * Get output stream. 075 * @returns Output stream 076 * @throws IOException IO exception occurred 077 */ 078 public OutputStream getOutputStream() throws IOException { 079 if (!_file.isWriteable()) { 080 throw new IOException("Cannot write"); 081 } 082 return IOUtil.toBufferedOutputStream(_file.getOutputStream()); 083 } 084 }