View Javadoc
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  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.nio.ByteBuffer;
22  
23  /**
24   * NIO backed bounded input stream for reading a predefined amount of data from.
25   *
26   * @ThreadSafe this base class is thread safe but implementations must not be.
27   * @since 1.21
28   */
29  public abstract class BoundedArchiveInputStream extends InputStream {
30  
31      private final long end;
32      private ByteBuffer singleByteBuffer;
33      private long loc;
34  
35      /**
36       * Constructs a new bounded input stream.
37       *
38       * @param start     position in the stream from where the reading of this bounded stream starts.
39       * @param remaining amount of bytes which are allowed to read from the bounded stream.
40       */
41      public BoundedArchiveInputStream(final long start, final long remaining) {
42          this.end = start + remaining;
43          if (this.end < start) {
44              // check for potential vulnerability due to overflow
45              throw new IllegalArgumentException("Invalid length of stream at offset=" + start + ", length=" + remaining);
46          }
47          loc = start;
48      }
49  
50      @Override
51      public synchronized int read() throws IOException {
52          if (loc >= end) {
53              return -1;
54          }
55          if (singleByteBuffer == null) {
56              singleByteBuffer = ByteBuffer.allocate(1);
57          } else {
58              singleByteBuffer.rewind();
59          }
60          final int read = read(loc, singleByteBuffer);
61          if (read < 1) {
62              return -1;
63          }
64          loc++;
65          return singleByteBuffer.get() & 0xff;
66      }
67  
68      @Override
69      public synchronized int read(final byte[] b, final int off, final int len) throws IOException {
70          if (loc >= end) {
71              return -1;
72          }
73          final long maxLen = Math.min(len, end - loc);
74          if (maxLen <= 0) {
75              return 0;
76          }
77          if (off < 0 || off > b.length || maxLen > b.length - off) {
78              throw new IndexOutOfBoundsException("offset or len are out of bounds");
79          }
80  
81          final ByteBuffer buf = ByteBuffer.wrap(b, off, (int) maxLen);
82          final int ret = read(loc, buf);
83          if (ret > 0) {
84              loc += ret;
85          }
86          return ret;
87      }
88  
89      /**
90       * Reads content of the stream into a {@link ByteBuffer}.
91       *
92       * @param pos position to start the read.
93       * @param buf buffer to add the read content.
94       * @return number of read bytes.
95       * @throws IOException if I/O fails.
96       */
97      protected abstract int read(long pos, ByteBuffer buf) throws IOException;
98  }