1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.shared.filtering;
20
21 import java.io.IOException;
22 import java.io.Reader;
23
24
25
26
27
28
29
30
31
32 public class BoundedReader extends Reader {
33
34 private final Reader target;
35
36 int pos = 0;
37
38 int readAheadLimit;
39
40 public BoundedReader(Reader target, int readAheadLimit) throws IOException {
41 this.target = target;
42 target.mark(readAheadLimit);
43 this.readAheadLimit = readAheadLimit;
44 }
45
46
47 @Override public void close() throws IOException {
48 target.close();
49 }
50
51 @Override public void reset() throws IOException {
52 pos = 0;
53 target.reset();
54 }
55
56 @Override public void mark(int readAheadLimit) throws IOException {
57 this.readAheadLimit = readAheadLimit;
58 target.mark(readAheadLimit);
59 }
60
61 @Override public int read() throws IOException {
62 if (pos >= readAheadLimit) return -1;
63 pos++;
64 return target.read();
65 }
66
67 @Override public int read(char[] cbuf, int off, int len) throws IOException{
68 int c;
69 for (int i = 0; i < len; i++){
70 c = read();
71 if (c == -1) return i;
72 cbuf[off + i] = (char) c;
73 }
74 return len;
75 }
76 }