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 */ 017package org.apache.commons.compress.utils; 018 019import java.io.IOException; 020import java.io.InputStream; 021import java.nio.ByteBuffer; 022 023/** 024 * NIO backed bounded input stream for reading a predefined amount of data from. 025 * @ThreadSafe this base class is thread safe but implementations must not be. 026 * @since 1.21 027 */ 028public abstract class BoundedArchiveInputStream extends InputStream { 029 030 private final long end; 031 private ByteBuffer singleByteBuffer; 032 private long loc; 033 034 /** 035 * Create a new bounded input stream. 036 * 037 * @param start position in the stream from where the reading of this bounded stream starts. 038 * @param remaining amount of bytes which are allowed to read from the bounded stream. 039 */ 040 public BoundedArchiveInputStream(final long start, final long remaining) { 041 this.end = start + remaining; 042 if (this.end < start) { 043 // check for potential vulnerability due to overflow 044 throw new IllegalArgumentException("Invalid length of stream at offset=" + start + ", length=" + remaining); 045 } 046 loc = start; 047 } 048 049 @Override 050 public synchronized int read() throws IOException { 051 if (loc >= end) { 052 return -1; 053 } 054 if (singleByteBuffer == null) { 055 singleByteBuffer = ByteBuffer.allocate(1); 056 } else { 057 singleByteBuffer.rewind(); 058 } 059 final int read = read(loc, singleByteBuffer); 060 if (read < 1) { 061 return -1; 062 } 063 loc++; 064 return singleByteBuffer.get() & 0xff; 065 } 066 067 @Override 068 public synchronized int read(final byte[] b, final int off, final int len) throws IOException { 069 if (loc >= end) { 070 return -1; 071 } 072 final long maxLen = Math.min(len, end - loc); 073 if (maxLen <= 0) { 074 return 0; 075 } 076 if (off < 0 || off > b.length || maxLen > b.length - off) { 077 throw new IndexOutOfBoundsException("offset or len are out of bounds"); 078 } 079 080 final ByteBuffer buf = ByteBuffer.wrap(b, off, (int) maxLen); 081 final int ret = read(loc, buf); 082 if (ret > 0) { 083 loc += ret; 084 } 085 return ret; 086 } 087 088 /** 089 * Read content of the stream into a {@link ByteBuffer}. 090 * @param pos position to start the read. 091 * @param buf buffer to add the read content. 092 * @return number of read bytes. 093 * @throws IOException if I/O fails. 094 */ 095 protected abstract int read(long pos, ByteBuffer buf) throws IOException; 096}