|
|||||||||||||||||||
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ExtendedReader.java | 59.1% | 54.2% | 45.5% | 54.3% |
|
1 | // Copyright 2004, 2005 The Apache Software Foundation | |
2 | // | |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | // you may not use this file except in compliance with the License. | |
5 | // You may obtain a copy of the License at | |
6 | // | |
7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
8 | // | |
9 | // Unless required by applicable law or agreed to in writing, software | |
10 | // distributed under the License is distributed on an "AS IS" BASIS, | |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | // See the License for the specific language governing permissions and | |
13 | // limitations under the License. | |
14 | ||
15 | package org.apache.tapestry.util.text; | |
16 | ||
17 | import java.io.IOException; | |
18 | import java.io.Reader; | |
19 | ||
20 | /** | |
21 | * A Reader that provides some additional functionality, such as peek(). | |
22 | * | |
23 | * @author mb | |
24 | * @since 4.0 | |
25 | */ | |
26 | public class ExtendedReader extends Reader | |
27 | { | |
28 | private Reader _reader; | |
29 | private boolean _hasBufferedChar = false; | |
30 | private char _bufferedChar; | |
31 | ||
32 | /** | |
33 | * Creates a new extended reader that reads from the provided object | |
34 | * | |
35 | * @param in the Reader to get data from | |
36 | */ | |
37 | 79 | public ExtendedReader(Reader in) |
38 | { | |
39 | 79 | _reader = in; |
40 | } | |
41 | ||
42 | /** | |
43 | * Returns the next character in the stream without actually comitting the read. | |
44 | * Multiple consequtive invocations of this method should return the same value. | |
45 | * | |
46 | * @return the next character waiting in the stream or -1 if the end of the stream is reached | |
47 | * @throws IOException if an error occurs | |
48 | */ | |
49 | 119606 | public synchronized int peek() throws IOException |
50 | { | |
51 | 119606 | if (!_hasBufferedChar) { |
52 | 57214 | int bufferedChar = read(); |
53 | 57214 | if (bufferedChar < 0) |
54 | 188 | return bufferedChar; |
55 | 57026 | _bufferedChar = (char) bufferedChar; |
56 | 57026 | _hasBufferedChar = true; |
57 | } | |
58 | 119418 | return _bufferedChar; |
59 | } | |
60 | ||
61 | /** | |
62 | * Determines whether the end of the stream is reached | |
63 | * | |
64 | * @return true if at the end of stream | |
65 | * @throws IOException if an error occurs | |
66 | */ | |
67 | 52526 | public synchronized boolean isEndOfStream() throws IOException |
68 | { | |
69 | 52526 | return peek() < 0; |
70 | } | |
71 | ||
72 | /** | |
73 | * Skips the next characters until a character that does not match the provided rule is reached. | |
74 | * | |
75 | * @param matcher the object determining whether a character should be skipped | |
76 | * @throws IOException if an error occurs | |
77 | */ | |
78 | 3890 | public synchronized void skipCharacters(ICharacterMatcher matcher) throws IOException |
79 | { | |
80 | 3890 | while (true) { |
81 | 52526 | if (isEndOfStream()) |
82 | 77 | break; |
83 | 52449 | char ch = (char) peek(); |
84 | 52449 | if (!matcher.matches(ch)) |
85 | 3813 | break; |
86 | 48636 | read(); |
87 | } | |
88 | } | |
89 | ||
90 | /** | |
91 | * Reads the next characters until a character that does not match the provided rule is reached. | |
92 | * | |
93 | * @param matcher the object determining whether a character should be read | |
94 | * @return the string of characters read | |
95 | * @throws IOException if an error occurs | |
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 | * @see java.io.FilterReader#read(char[], int, int) | |
113 | */ | |
114 | 114259 | public synchronized int read(char[] cbuf, int off, int len) throws IOException |
115 | { | |
116 | 114259 | if (len <= 0) |
117 | 0 | return 0; |
118 | ||
119 | 114259 | boolean extraChar = _hasBufferedChar; |
120 | 114259 | if (_hasBufferedChar) { |
121 | 57026 | _hasBufferedChar = false; |
122 | 57026 | cbuf[off++] = _bufferedChar; |
123 | 57026 | len--; |
124 | } | |
125 | ||
126 | 114259 | int read = _reader.read(cbuf, off, len); |
127 | 114259 | if (extraChar) |
128 | 57026 | read++; |
129 | 114259 | return read; |
130 | } | |
131 | ||
132 | /** | |
133 | * @see java.io.FilterReader#ready() | |
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 | * @see java.io.FilterReader#markSupported() | |
144 | */ | |
145 | 0 | public synchronized boolean markSupported() |
146 | { | |
147 | 0 | return false; |
148 | } | |
149 | ||
150 | /** | |
151 | * @see java.io.FilterReader#reset() | |
152 | */ | |
153 | 0 | public synchronized void reset() throws IOException |
154 | { | |
155 | 0 | _hasBufferedChar = false; |
156 | 0 | _reader.reset(); |
157 | } | |
158 | ||
159 | /** | |
160 | * @see java.io.FilterReader#skip(long) | |
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 | * @see java.io.Reader#close() | |
173 | */ | |
174 | 0 | public synchronized void close() throws IOException |
175 | { | |
176 | 0 | _hasBufferedChar = false; |
177 | 0 | _reader.close(); |
178 | } | |
179 | ||
180 | } |
|