    001/*
    002 *  Licensed to the Apache Software Foundation (ASF) under one or more
    003 *  contributor license agreements.  See the NOTICE file distributed with
    004 *  this work for additional information regarding copyright ownership.
    005 *  The ASF licenses this file to You under the Apache License, Version 2.0
    006 *  (the "License"); you may not use this file except in compliance with
    007 *  the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 *  Unless required by applicable law or agreed to in writing, software
    012 *  distributed under the License is distributed on an "AS IS" BASIS,
    013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 *  See the License for the specific language governing permissions and
    015 *  limitations under the License.
    016 *
    017 */
    018package org.apache.commons.compress.utils;
    019
    020import java.io.IOException;
    021import java.io.InputStream;
    022
    023/**
    024 * A stream that limits reading from a wrapped stream to a given number of bytes.
    025 * @NotThreadSafe
    026 * @since 1.6
    027 */
    028public class BoundedInputStream extends InputStream {
    029    private final InputStream in;
    030    private long bytesRemaining;
    031
    032    /**
    033     * Creates the stream that will at most read the given amount of
    034     * bytes from the given stream.
    035     * @param in the stream to read from
    036     * @param size the maximum amount of bytes to read
    037     */
    038    public BoundedInputStream(final InputStream in, final long size) {
    039        this.in = in;
    040        bytesRemaining = size;
    041    }
    042
    043    @Override
    044    public void close() {
    045        // there isn't anything to close in this stream and the nested
    046        // stream is controlled externally
    047    }
    048
    049    /**
    050     * @return bytes remaining to read
    051     * @since 1.21
    052     */
    053    public long getBytesRemaining() {
    054        return bytesRemaining;
    055    }
    056
    057    @Override
    058    public int read() throws IOException {
    059        if (bytesRemaining > 0) {
    060            --bytesRemaining;
    061            return in.read();
    062        }
    063        return -1;
    064    }
    065
    066    @Override
    067    public int read(final byte[] b, final int off, final int len) throws IOException {
    068        if (len == 0) {
    069            return 0;
    070        }
    071        if (bytesRemaining == 0) {
    072            return -1;
    073        }
    074        int bytesToRead = len;
    075        if (bytesToRead > bytesRemaining) {
    076            bytesToRead = (int) bytesRemaining;
    077        }
    078        final int bytesRead = in.read(b, off, bytesToRead);
    079        if (bytesRead >= 0) {
    080            bytesRemaining -= bytesRead;
    081        }
    082        return bytesRead;
    083    }
    084
    085    /**
    086     * @since 1.20
    087     */
    088    @Override
    089    public long skip(final long n) throws IOException {
    090        final long bytesToSkip = Math.min(bytesRemaining, n);
    091        final long bytesSkipped = in.skip(bytesToSkip);
    092        bytesRemaining -= bytesSkipped;
    093
    094        return bytesSkipped;
    095    }
    096}