View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.io;
21  
22  import java.io.FilterInputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  
26  import static com.google.common.base.Preconditions.checkArgument;
27  import static com.google.common.base.Preconditions.checkNotNull;
28  import org.apache.hadoop.hbase.classification.InterfaceAudience;
29  
30  /**
31   * Copied from guava source code v15 (LimitedInputStream)
32   * Guava deprecated LimitInputStream in v14 and removed it in v15. Copying this class here
33   * allows to be compatible with guava 11 to 15+.
34   */
35  @InterfaceAudience.Private
36  public final class LimitInputStream extends FilterInputStream {
37    private long left;
38    private long mark = -1;
39  
40    public LimitInputStream(InputStream in, long limit) {
41      super(in);
42      checkNotNull(in);
43      checkArgument(limit >= 0, "limit must be non-negative");
44      left = limit;
45    }
46  
47    @Override
48    public int available() throws IOException {
49      return (int) Math.min(in.available(), left);
50    }
51  
52    // it's okay to mark even if mark isn't supported, as reset won't work
53    @Override
54    public synchronized void mark(int readLimit) {
55      in.mark(readLimit);
56      mark = left;
57    }
58  
59    @Override
60    public int read() throws IOException {
61      if (left == 0) {
62        return -1;
63      }
64  
65      int result = in.read();
66      if (result != -1) {
67        --left;
68      }
69      return result;
70    }
71  
72    @Override
73    public int read(byte[] b, int off, int len) throws IOException {
74      if (left == 0) {
75        return -1;
76      }
77  
78      len = (int) Math.min(len, left);
79      int result = in.read(b, off, len);
80      if (result != -1) {
81        left -= result;
82      }
83      return result;
84    }
85  
86    @Override
87    public synchronized void reset() throws IOException {
88      if (!in.markSupported()) {
89        throw new IOException("Mark not supported");
90      }
91      if (mark == -1) {
92        throw new IOException("Mark not set");
93      }
94  
95      in.reset();
96      left = mark;
97    }
98  
99    @Override
100   public long skip(long n) throws IOException {
101     n = Math.min(n, left);
102     long skipped = in.skip(n);
103     left -= skipped;
104     return skipped;
105   }
106 }