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