1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.util.text; |
16 |
| |
17 |
| import java.io.IOException; |
18 |
| import java.io.Reader; |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| public class ExtendedReader extends Reader |
27 |
| { |
28 |
| private Reader _reader; |
29 |
| private boolean _hasBufferedChar = false; |
30 |
| private char _bufferedChar; |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
88
| public ExtendedReader(Reader in)
|
38 |
| { |
39 |
88
| _reader = in;
|
40 |
| } |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
131309
| public synchronized int peek() throws IOException
|
50 |
| { |
51 |
131309
| if (!_hasBufferedChar) {
|
52 |
62901
| int bufferedChar = read();
|
53 |
62901
| if (bufferedChar < 0)
|
54 |
187
| return bufferedChar;
|
55 |
62714
| _bufferedChar = (char) bufferedChar;
|
56 |
62714
| _hasBufferedChar = true;
|
57 |
| } |
58 |
131122
| return _bufferedChar;
|
59 |
| } |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
57173
| public synchronized boolean isEndOfStream() throws IOException
|
68 |
| { |
69 |
57173
| return peek() < 0;
|
70 |
| } |
71 |
| |
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
4433
| public synchronized void skipCharacters(ICharacterMatcher matcher) throws IOException
|
79 |
| { |
80 |
4433
| while (true) {
|
81 |
57173
| if (isEndOfStream())
|
82 |
86
| break;
|
83 |
57087
| char ch = (char) peek();
|
84 |
57087
| if (!matcher.matches(ch))
|
85 |
4347
| break;
|
86 |
52740
| read();
|
87 |
| } |
88 |
| } |
89 |
| |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
0
| public synchronized String readCharacters(ICharacterMatcher matcher) throws IOException
|
98 |
| { |
99 |
0
| StringBuffer buf = new StringBuffer();
|
100 |
0
| while (true) {
|
101 |
0
| if (isEndOfStream())
|
102 |
0
| break;
|
103 |
0
| char ch = (char) peek();
|
104 |
0
| if (!matcher.matches(ch))
|
105 |
0
| break;
|
106 |
0
| buf.append(read());
|
107 |
| } |
108 |
0
| return buf.toString();
|
109 |
| } |
110 |
| |
111 |
| |
112 |
| |
113 |
| |
114 |
125634
| public synchronized int read(char[] cbuf, int off, int len) throws IOException
|
115 |
| { |
116 |
125634
| if (len <= 0)
|
117 |
0
| return 0;
|
118 |
| |
119 |
125634
| boolean extraChar = _hasBufferedChar;
|
120 |
125634
| if (_hasBufferedChar) {
|
121 |
62714
| _hasBufferedChar = false;
|
122 |
62714
| cbuf[off++] = _bufferedChar;
|
123 |
62714
| len--;
|
124 |
| } |
125 |
| |
126 |
125634
| int read = _reader.read(cbuf, off, len);
|
127 |
125634
| if (extraChar)
|
128 |
62714
| read++;
|
129 |
125634
| return read;
|
130 |
| } |
131 |
| |
132 |
| |
133 |
| |
134 |
| |
135 |
0
| public synchronized boolean ready() throws IOException
|
136 |
| { |
137 |
0
| if (_hasBufferedChar)
|
138 |
0
| return true;
|
139 |
0
| return _reader.ready();
|
140 |
| } |
141 |
| |
142 |
| |
143 |
| |
144 |
| |
145 |
0
| public synchronized boolean markSupported()
|
146 |
| { |
147 |
0
| return false;
|
148 |
| } |
149 |
| |
150 |
| |
151 |
| |
152 |
| |
153 |
0
| public synchronized void reset() throws IOException
|
154 |
| { |
155 |
0
| _hasBufferedChar = false;
|
156 |
0
| _reader.reset();
|
157 |
| } |
158 |
| |
159 |
| |
160 |
| |
161 |
| |
162 |
0
| public synchronized long skip(long n) throws IOException
|
163 |
| { |
164 |
0
| if (_hasBufferedChar && n > 0) {
|
165 |
0
| _hasBufferedChar = false;
|
166 |
0
| n--;
|
167 |
| } |
168 |
0
| return _reader.skip(n);
|
169 |
| } |
170 |
| |
171 |
| |
172 |
| |
173 |
| |
174 |
0
| public synchronized void close() throws IOException
|
175 |
| { |
176 |
0
| _hasBufferedChar = false;
|
177 |
0
| _reader.close();
|
178 |
| } |
179 |
| |
180 |
| } |