001    /*
002     * Copyright (c) 2005 Henri Sivonen
003     *
004     * Permission is hereby granted, free of charge, to any person obtaining a 
005     * copy of this software and associated documentation files (the "Software"), 
006     * to deal in the Software without restriction, including without limitation 
007     * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
008     * and/or sell copies of the Software, and to permit persons to whom the 
009     * Software is furnished to do so, subject to the following conditions:
010     *
011     * The above copyright notice and this permission notice shall be included in 
012     * all copies or substantial portions of the Software.
013     *
014     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
015     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
016     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
017     * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
018     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
019     * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
020     * DEALINGS IN THE SOFTWARE.
021     */
022    
023    package fi.iki.hsivonen.io;
024    
025    import java.io.IOException;
026    import java.io.InputStream;
027    
028    
029    /**
030     * @version $Id: BoundednputStream.java,v 1.2 2005/07/24 07:35:24 hsivonen Exp $
031     * @author hsivonen
032     */
033    public class BoundednputStream extends InputStream {
034    
035        private InputStream delegate;
036    
037        private long counter = 0;
038    
039        private long limit;
040    
041        /**
042         * @param delegate
043         * @param limit
044         */
045        public BoundednputStream(InputStream delegate, long limit) {
046            this.delegate = delegate;
047            this.limit = limit;
048        }
049    
050        private void checkLimit() throws IOException {
051            if (counter > limit) {
052                throw new StreamBoundException();
053            }
054        }
055    
056        /**
057         * @see java.io.InputStream#available()
058         */
059        public int available() throws IOException {
060            return delegate.available();
061        }
062    
063        /**
064         * @see java.io.InputStream#close()
065         */
066        public void close() throws IOException {
067            delegate.close();
068        }
069    
070        /**
071         * @see java.io.InputStream#mark(int)
072         */
073        public void mark(int arg0) {
074            delegate.mark(arg0);
075        }
076    
077        /**
078         * @see java.io.InputStream#markSupported()
079         */
080        public boolean markSupported() {
081            return delegate.markSupported();
082        }
083    
084        /**
085         * @see java.io.InputStream#read()
086         */
087        public int read() throws IOException {
088            this.checkLimit();
089            this.counter++;
090            return delegate.read();
091        }
092    
093        /**
094         * @see java.io.InputStream#read(byte[])
095         */
096        public int read(byte[] arg0) throws IOException {
097            this.checkLimit();
098            int c = delegate.read(arg0);
099            this.counter += c;
100            return c;
101        }
102    
103        /**
104         * @see java.io.InputStream#read(byte[], int, int)
105         */
106        public int read(byte[] arg0, int arg1, int arg2) throws IOException {
107            this.checkLimit();
108            int c = delegate.read(arg0, arg1, arg2);
109            this.counter += c;
110            return c;
111        }
112    
113        /**
114         * @see java.io.InputStream#reset()
115         */
116        public void reset() throws IOException {
117            delegate.reset();
118        }
119    
120        /**
121         * @see java.io.InputStream#skip(long)
122         */
123        public long skip(long arg0) throws IOException {
124            return delegate.skip(arg0);
125        }
126    }