001    package railo.runtime.functions.file;
002    
003    import java.io.IOException;
004    
005    import railo.commons.io.res.Resource;
006    import railo.commons.lang.StringUtil;
007    import railo.runtime.PageContext;
008    import railo.runtime.exp.PageException;
009    import railo.runtime.op.Caster;
010    
011    public class FileAppend {
012    
013            public static String call(PageContext pc, String path, Object data) throws PageException {
014                    return call(pc,path,data,pc.getConfig().getResourceCharset());
015            }
016            
017            public static String call(PageContext pc, String path, Object data,String charset) throws PageException {
018                    FileStreamWrapper fsw=null;
019                    if(StringUtil.isEmpty(charset,true))
020                            charset=pc.getConfig().getResourceCharset();
021                    
022                    try {
023                            Resource res = Caster.toResource(pc,path,false);
024                            pc.getConfig().getSecurityManager().checkFileLocation(res);
025                            fsw=new FileStreamWrapperWrite(res,charset,true,false);
026                            fsw.write(data);        
027                    } 
028                    catch (IOException e) {
029                            throw Caster.toPageException(e);
030                    }
031                    finally {
032                            closeEL(fsw);
033                    }
034                    return null;
035            }
036    
037            private static void closeEL(FileStreamWrapper fsw) {
038                    if(fsw==null)return;
039                    try {
040                            fsw.close();
041                    } catch (Throwable t) {}
042            }
043    }