BoundedArchiveInputStream.java

  1. /*
  2.  *  Licensed to the Apache Software Foundation (ASF) under one or more
  3.  *  contributor license agreements.  See the NOTICE file distributed with
  4.  *  this work for additional information regarding copyright ownership.
  5.  *  The ASF licenses this file to You under the Apache License, Version 2.0
  6.  *  (the "License"); you may not use this file except in compliance with
  7.  *  the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17. package org.apache.commons.compress.utils;

  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.nio.ByteBuffer;

  21. /**
  22.  * NIO backed bounded input stream for reading a predefined amount of data from.
  23.  *
  24.  * @ThreadSafe this base class is thread safe but implementations must not be.
  25.  * @since 1.21
  26.  */
  27. public abstract class BoundedArchiveInputStream extends InputStream {

  28.     private final long end;
  29.     private ByteBuffer singleByteBuffer;
  30.     private long loc;

  31.     /**
  32.      * Constructs a new bounded input stream.
  33.      *
  34.      * @param start     position in the stream from where the reading of this bounded stream starts.
  35.      * @param remaining amount of bytes which are allowed to read from the bounded stream.
  36.      */
  37.     public BoundedArchiveInputStream(final long start, final long remaining) {
  38.         this.end = start + remaining;
  39.         if (this.end < start) {
  40.             // check for potential vulnerability due to overflow
  41.             throw new IllegalArgumentException("Invalid length of stream at offset=" + start + ", length=" + remaining);
  42.         }
  43.         loc = start;
  44.     }

  45.     @Override
  46.     public synchronized int read() throws IOException {
  47.         if (loc >= end) {
  48.             return -1;
  49.         }
  50.         if (singleByteBuffer == null) {
  51.             singleByteBuffer = ByteBuffer.allocate(1);
  52.         } else {
  53.             singleByteBuffer.rewind();
  54.         }
  55.         final int read = read(loc, singleByteBuffer);
  56.         if (read < 1) {
  57.             return -1;
  58.         }
  59.         loc++;
  60.         return singleByteBuffer.get() & 0xff;
  61.     }

  62.     @Override
  63.     public synchronized int read(final byte[] b, final int off, final int len) throws IOException {
  64.         if (loc >= end) {
  65.             return -1;
  66.         }
  67.         final long maxLen = Math.min(len, end - loc);
  68.         if (maxLen <= 0) {
  69.             return 0;
  70.         }
  71.         if (off < 0 || off > b.length || maxLen > b.length - off) {
  72.             throw new IndexOutOfBoundsException("offset or len are out of bounds");
  73.         }

  74.         final ByteBuffer buf = ByteBuffer.wrap(b, off, (int) maxLen);
  75.         final int ret = read(loc, buf);
  76.         if (ret > 0) {
  77.             loc += ret;
  78.         }
  79.         return ret;
  80.     }

  81.     /**
  82.      * Reads content of the stream into a {@link ByteBuffer}.
  83.      *
  84.      * @param pos position to start the read.
  85.      * @param buf buffer to add the read content.
  86.      * @return number of read bytes.
  87.      * @throws IOException if I/O fails.
  88.      */
  89.     protected abstract int read(long pos, ByteBuffer buf) throws IOException;
  90. }