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.runtime.functions.file; 020 021import java.io.IOException; 022 023import lucee.commons.io.res.Resource; 024import lucee.commons.lang.ExceptionUtil; 025import lucee.commons.lang.StringUtil; 026import lucee.runtime.PageContext; 027import lucee.runtime.PageContextImpl; 028import lucee.runtime.exp.PageException; 029import lucee.runtime.op.Caster; 030 031public class FileAppend { 032 033 public static String call(PageContext pc, String path, Object data) throws PageException { 034 return call(pc,path,data,((PageContextImpl)pc).getResourceCharset().name()); 035 } 036 037 public static String call(PageContext pc, String path, Object data,String charset) throws PageException { 038 FileStreamWrapper fsw=null; 039 if(StringUtil.isEmpty(charset,true)) 040 charset=((PageContextImpl)pc).getResourceCharset().name(); 041 042 try { 043 Resource res = Caster.toResource(pc,path,false); 044 pc.getConfig().getSecurityManager().checkFileLocation(res); 045 fsw=new FileStreamWrapperWrite(res,charset,true,false); 046 fsw.write(data); 047 } 048 catch (IOException e) { 049 throw Caster.toPageException(e); 050 } 051 finally { 052 closeEL(fsw); 053 } 054 return null; 055 } 056 057 private static void closeEL(FileStreamWrapper fsw) { 058 if(fsw==null)return; 059 try { 060 fsw.close(); 061 } catch (Throwable t) { 062 ExceptionUtil.rethrowIfNecessary(t); 063 } 064 } 065}