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 FileWrite {
012    
013            public static String call(PageContext pc, Object obj, Object data) throws PageException {
014                    return call(pc,obj,data,pc.getConfig().getResourceCharset());
015            }
016            
017            public static String call(PageContext pc, Object obj, Object data,String charset) throws PageException {
018                    FileStreamWrapper fsw=null;
019                    boolean close=false;
020                    if(StringUtil.isEmpty(charset,true))
021                            charset=pc.getConfig().getResourceCharset();
022                    try {
023                            try {
024                                    if(obj instanceof FileStreamWrapper) {
025                                            fsw=(FileStreamWrapper)obj;
026                                    }
027                                    else {
028                                            close=true;
029                                            Resource res = Caster.toResource(obj,false);
030                                            pc.getConfig().getSecurityManager().checkFileLocation(res);
031                                            fsw=new FileStreamWrapperWrite(res,charset,false,false);
032                                    }
033                                    fsw.write(data);
034                            }
035                            finally {
036                                    if(close && fsw!=null)fsw.close();
037                            }
038                            
039                    } 
040                    catch (IOException e) {
041                            throw Caster.toPageException(e);
042                    }
043                    return null;
044            }
045    }