View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.compress.archivers.sevenz;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.nio.ByteBuffer;
24  import java.nio.channels.SeekableByteChannel;
25  
26  final class BoundedSeekableByteChannelInputStream extends InputStream {
27      private static final int MAX_BUF_LEN = 8192;
28      private final ByteBuffer buffer;
29      private final SeekableByteChannel channel;
30      private long bytesRemaining;
31  
32      BoundedSeekableByteChannelInputStream(final SeekableByteChannel channel, final long size) {
33          this.channel = channel;
34          this.bytesRemaining = size;
35          this.buffer = ByteBuffer.allocate(size < MAX_BUF_LEN && size > 0 ? (int) size : MAX_BUF_LEN);
36      }
37  
38      @Override
39      public void close() {
40          // the nested channel is controlled externally
41      }
42  
43      @Override
44      public int read() throws IOException {
45          if (bytesRemaining > 0) {
46              --bytesRemaining;
47              final int read = read(1);
48              if (read < 0) {
49                  return read;
50              }
51              return buffer.get() & 0xff;
52          }
53          return -1;
54      }
55  
56      /**
57       * Reads up to len bytes of data from the input stream into an array of bytes.
58       *
59       * <p>
60       * An attempt is made to read as many as len bytes, but a smaller number may be read. The number of bytes actually read is returned as an integer.
61       * </p>
62       *
63       * <p>
64       * This implementation may return 0 if the underlying {@link SeekableByteChannel} is non-blocking and currently hasn't got any bytes available.
65       * </p>
66       */
67      @Override
68      public int read(final byte[] b, final int off, final int len) throws IOException {
69          if (len == 0) {
70              return 0;
71          }
72          if (bytesRemaining <= 0) {
73              return -1;
74          }
75          int bytesToRead = len;
76          if (bytesToRead > bytesRemaining) {
77              bytesToRead = (int) bytesRemaining;
78          }
79          final int bytesRead;
80          final ByteBuffer buf;
81          if (bytesToRead <= buffer.capacity()) {
82              buf = buffer;
83              bytesRead = read(bytesToRead);
84          } else {
85              buf = ByteBuffer.allocate(bytesToRead);
86              bytesRead = channel.read(buf);
87              buf.flip();
88          }
89          if (bytesRead >= 0) {
90              buf.get(b, off, bytesRead);
91              bytesRemaining -= bytesRead;
92          }
93          return bytesRead;
94      }
95  
96      private int read(final int len) throws IOException {
97          buffer.rewind().limit(len);
98          final int read = channel.read(buffer);
99          buffer.flip();
100         return read;
101     }
102 }