• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.io;
18 
19 import com.google.common.base.Preconditions;
20 
21 import java.io.FilterInputStream;
22 import java.io.InputStream;
23 import java.io.IOException;
24 
25 /**
26  * An InputStream that limits the number of bytes which can be read.
27  *
28  * @author Charles Fry
29  * @since 2009.09.15 <b>tentative</b>
30  */
31 public class LimitInputStream extends FilterInputStream {
32 
33   private long left;
34   private long mark = -1;
35 
36   /**
37    * Wraps another input stream, limiting the number of bytes which can be read.
38    *
39    * @param in the input stream to be wrapped
40    * @param limit the maximum number of bytes to be read
41    */
LimitInputStream(InputStream in, long limit)42   public LimitInputStream(InputStream in, long limit) {
43     super(in);
44     Preconditions.checkNotNull(in);
45     Preconditions.checkArgument(limit >= 0, "limit must be non-negative");
46     left = limit;
47   }
48 
available()49   @Override public int available() throws IOException {
50     return (int) Math.min(in.available(), left);
51   }
52 
mark(int readlimit)53   @Override public void mark(int readlimit) {
54     in.mark(readlimit);
55     mark = left;
56     // it's okay to mark even if mark isn't supported, as reset won't work
57   }
58 
read()59   @Override public int read() throws IOException {
60     if (left == 0) {
61       return -1;
62     }
63 
64     int result = in.read();
65     if (result != -1) {
66       --left;
67     }
68     return result;
69   }
70 
read(byte[] b, int off, int len)71   @Override public int read(byte[] b, int off, int len) throws IOException {
72     if (left == 0) {
73       return -1;
74     }
75 
76     len = (int) Math.min(len, left);
77     int result = in.read(b, off, len);
78     if (result != -1) {
79       left -= result;
80     }
81     return result;
82   }
83 
reset()84   @Override public void reset() throws IOException {
85     if (!in.markSupported()) {
86       throw new IOException("Mark not supported");
87     }
88     if (mark == -1) {
89       throw new IOException("Mark not set");
90     }
91 
92     in.reset();
93     left = mark;
94   }
95 
skip(long n)96   @Override public long skip(long n) throws IOException {
97     n = Math.min(n, left);
98     long skipped = in.skip(n);
99     left -= skipped;
100     return skipped;
101   }
102 
103 }
104