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.io.res.util;
020
021import java.io.IOException;
022import java.io.InputStream;
023
024import lucee.commons.io.res.Resource;
025
026public class ResourceInputStream extends InputStream {
027
028        private final Resource res;
029        private final InputStream is;
030
031        public ResourceInputStream(Resource res, InputStream is) {
032                this.res=res;
033                this.is=is; 
034        }
035        
036        @Override
037        public int read() throws IOException {
038                return is.read();
039        }
040
041        @Override
042        public int available() throws IOException {
043                return is.available();
044        }
045
046        @Override
047        public void close() throws IOException {
048                try {
049                        is.close();
050                }
051                finally {
052                        res.getResourceProvider().unlock(res);
053                }
054        }
055
056        @Override
057        public void mark(int readlimit) {
058                is.mark(readlimit);
059        }
060
061        @Override
062        public boolean markSupported() {
063                return is.markSupported();
064        }
065
066        @Override
067        public int read(byte[] b, int off, int len) throws IOException {
068                return is.read(b, off, len);
069        }
070
071        @Override
072        public int read(byte[] b) throws IOException {
073                return is.read(b);
074        }
075
076        @Override
077        public synchronized void reset() throws IOException {
078                is.reset();
079        }
080
081        @Override
082        public long skip(long n) throws IOException {
083                return is.skip(n);
084        }
085
086        /**
087         * @return the InputStream
088         */
089        public InputStream getInputStream() {
090                return is;
091        }
092
093        /**
094         * @return the Resource
095         */
096        public Resource getResource() {
097                return res;
098        }
099
100}