1 package media;
2
3 import java.io.FilterInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6
7
8
9
10
11 public class LimitedInputStream extends FilterInputStream {
12 private final long byteLimit;
13 private long byteRead = 0;
14
15
16 public LimitedInputStream(InputStream in, long byteLimit){
17 super(in);
18 this.byteLimit = byteLimit;
19 }
20
21 @Override
22 public int read() throws IOException {
23 int bite = super.read();
24 this.byteRead++;
25 if(this.byteRead > byteLimit)
26 throw new ReadLimitExceededException();
27 return bite;
28 }
29
30 @Override
31 public int read(byte[] b) throws IOException {
32 return super.read(b);
33 }
34
35 @Override
36 public int read(byte[] b, int off, int len) throws IOException {
37 int byteRead = super.read(b,off,len);
38 this.byteRead += byteRead;
39 if(this.byteRead > byteLimit)
40 throw new ReadLimitExceededException();
41 return byteRead;
42 }
43 }