001    /****************************************************************
002     * Licensed to the Apache Software Foundation (ASF) under one   *
003     * or more contributor license agreements.  See the NOTICE file *
004     * distributed with this work for additional information        *
005     * regarding copyright ownership.  The ASF licenses this file   *
006     * to you under the Apache License, Version 2.0 (the            *
007     * "License"); you may not use this file except in compliance   *
008     * with the License.  You may obtain a copy of the License at   *
009     *                                                              *
010     *   http://www.apache.org/licenses/LICENSE-2.0                 *
011     *                                                              *
012     * Unless required by applicable law or agreed to in writing,   *
013     * software distributed under the License is distributed on an  *
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
015     * KIND, either express or implied.  See the License for the    *
016     * specific language governing permissions and limitations      *
017     * under the License.                                           *
018     ****************************************************************/
019    
020    package org.apache.james.mime4j.io;
021    
022    import java.io.InputStream;
023    import java.io.IOException;
024    
025    public class LimitedInputStream extends PositionInputStream {
026    
027        private final long limit;
028    
029        public LimitedInputStream(InputStream instream, long limit) {
030            super(instream);
031            if (limit < 0) {
032                throw new IllegalArgumentException("Limit may not be negative");
033            }
034            this.limit = limit;
035        }
036    
037        private void enforceLimit() throws IOException {
038            if (position >= limit) {
039                throw new IOException("Input stream limit exceeded");
040            }
041        }
042        
043        @Override
044        public int read() throws IOException {
045            enforceLimit();
046            return super.read();
047        }
048    
049        @Override
050        public int read(byte b[], int off, int len) throws IOException {
051            enforceLimit();
052            len = Math.min(len, getBytesLeft());
053            return super.read(b, off, len);
054        }
055    
056        @Override
057        public long skip(long n) throws IOException {
058            enforceLimit();
059            n = Math.min(n, getBytesLeft());
060            return super.skip(n);
061        }
062    
063        private int getBytesLeft() {
064            return (int)Math.min(Integer.MAX_VALUE, limit - position);
065        }
066        
067    }