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.ByteArrayOutputStream;
022import java.io.IOException;
023import java.io.InputStream;
024
025import lucee.commons.io.IOUtil;
026import lucee.commons.io.res.Resource;
027import lucee.runtime.PageContext;
028import lucee.runtime.PageContextImpl;
029import lucee.runtime.exp.PageException;
030import lucee.runtime.op.Caster;
031import lucee.runtime.op.Decision;
032
033public class FileRead {
034
035        public static Object call(PageContext pc, Object path) throws PageException {
036                return _call(pc,Caster.toResource(pc,path,true),((PageContextImpl)pc).getResourceCharset().name());
037        }
038        
039        public static Object call(PageContext pc, Object obj, Object charsetOrSize) throws PageException {
040                if(charsetOrSize==null) return call(pc, obj);
041                
042                if(obj instanceof FileStreamWrapper) {
043                        return _call((FileStreamWrapper)obj,Caster.toIntValue(charsetOrSize));
044                }
045                Resource res = Caster.toResource(pc,obj,true);
046                String charset=Caster.toString(charsetOrSize);
047                if(Decision.isInteger(charset)){
048                        charset=((PageContextImpl)pc).getResourceCharset().name();
049                        return _call(pc,res,charset,Caster.toIntValue(charset));
050                }
051                
052                return _call(pc,res,charset);
053        }
054
055        private static Object _call(FileStreamWrapper fs, int size) throws PageException {
056                try {
057                        return fs.read(size);
058                } catch (IOException e) {
059                        throw Caster.toPageException(e);
060                }
061        }
062
063        private static Object _call(PageContext pc,Resource res, String charset) throws PageException {
064                pc.getConfig().getSecurityManager().checkFileLocation(res);
065                try {
066                        return IOUtil.toString(res,charset);
067                } catch (IOException e) {
068                        throw Caster.toPageException(e);
069                }
070        }
071        
072        private static Object _call(PageContext pc, Resource res, String charset,int size) throws PageException {
073                pc.getConfig().getSecurityManager().checkFileLocation(res);
074                
075                InputStream is=null;
076                ByteArrayOutputStream baos = new ByteArrayOutputStream();
077                try {
078                        is=res.getInputStream();
079                        IOUtil.copy(is, baos, 0, size);
080                        return new String(baos.toByteArray(),charset);
081                } 
082                catch (IOException e) {
083                        throw Caster.toPageException(e);
084                }
085                finally {
086                        IOUtil.closeEL(is);
087                }
088                
089                
090                // TODO Auto-generated method stub
091        }
092        
093}