001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.commons.activation; 020 021// Imports 022import java.io.IOException; 023import java.io.InputStream; 024import java.io.OutputStream; 025 026import javax.activation.DataSource; 027 028import lucee.commons.io.IOUtil; 029import lucee.commons.io.res.Resource; 030 031/** 032 * File Data Source. 033 */ 034public final class ResourceDataSource implements DataSource { 035 036 /** 037 * File source. 038 */ 039 private final Resource _file; 040 041 /** 042 * Constructor of the class 043 * @param res source 044 */ 045 public ResourceDataSource(Resource res) { 046 _file = res; 047 } 048 049 /** 050 * Get name. 051 * @returns Name 052 */ 053 public String getName() { 054 return _file.getName(); 055 } 056 057 /** 058 * Get Resource. 059 * @returns Resource 060 */ 061 public Resource getResource() { 062 return _file; 063 } 064 065 /** 066 * Get input stream. 067 * @returns Input stream 068 * @throws IOException IO exception occurred 069 */ 070 public InputStream getInputStream() throws IOException { 071 return IOUtil.toBufferedInputStream(_file.getInputStream()); 072 } 073 074 /** 075 * Get content type. 076 * @returns Content type 077 */ 078 public String getContentType() { 079 InputStream is=null; 080 try { 081 return IOUtil.getMimeType(is=_file.getInputStream(), "application/unknow"); 082 } catch (IOException e) { 083 return "application/unknow"; 084 } 085 finally { 086 IOUtil.closeEL(is); 087 } 088 089 } 090 091 /** 092 * Get output stream. 093 * @returns Output stream 094 * @throws IOException IO exception occurred 095 */ 096 public OutputStream getOutputStream() throws IOException { 097 if (!_file.isWriteable()) { 098 throw new IOException("Cannot write"); 099 } 100 return IOUtil.toBufferedOutputStream(_file.getOutputStream()); 101 } 102}