KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > util > text > ExtendedReader


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 JavaDoc;
18 import java.io.Reader JavaDoc;
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 JavaDoc
27 {
28     private Reader JavaDoc _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     public ExtendedReader(Reader JavaDoc in)
38     {
39         _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     public synchronized int peek() throws IOException JavaDoc
50     {
51         if (!_hasBufferedChar) {
52             int bufferedChar = read();
53             if (bufferedChar < 0)
54                 return bufferedChar;
55             _bufferedChar = (char) bufferedChar;
56             _hasBufferedChar = true;
57         }
58         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     public synchronized boolean isEndOfStream() throws IOException JavaDoc
68     {
69         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     public synchronized void skipCharacters(ICharacterMatcher matcher) throws IOException JavaDoc
79     {
80         while (true) {
81             if (isEndOfStream())
82                 break;
83             char ch = (char) peek();
84             if (!matcher.matches(ch))
85                 break;
86             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     public synchronized String JavaDoc readCharacters(ICharacterMatcher matcher) throws IOException JavaDoc
98     {
99         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
100         while (true) {
101             if (isEndOfStream())
102                 break;
103             char ch = (char) peek();
104             if (!matcher.matches(ch))
105                 break;
106             buf.append(read());
107         }
108         return buf.toString();
109     }
110     
111     /**
112      * @see java.io.FilterReader#read(char[], int, int)
113      */

114     public synchronized int read(char[] cbuf, int off, int len) throws IOException JavaDoc
115     {
116         if (len <= 0)
117             return 0;
118         
119         boolean extraChar = _hasBufferedChar;
120         if (_hasBufferedChar) {
121             _hasBufferedChar = false;
122             cbuf[off++] = _bufferedChar;
123             len--;
124         }
125
126         int read = _reader.read(cbuf, off, len);
127         if (extraChar)
128             read++;
129         return read;
130     }
131     
132     /**
133      * @see java.io.FilterReader#ready()
134      */

135     public synchronized boolean ready() throws IOException JavaDoc
136     {
137         if (_hasBufferedChar)
138             return true;
139         return _reader.ready();
140     }
141     
142     /**
143      * @see java.io.FilterReader#markSupported()
144      */

145     public synchronized boolean markSupported()
146     {
147         return false;
148     }
149     
150     /**
151      * @see java.io.FilterReader#reset()
152      */

153     public synchronized void reset() throws IOException JavaDoc
154     {
155         _hasBufferedChar = false;
156         _reader.reset();
157     }
158     
159     /**
160      * @see java.io.FilterReader#skip(long)
161      */

162     public synchronized long skip(long n) throws IOException JavaDoc
163     {
164         if (_hasBufferedChar && n > 0) {
165             _hasBufferedChar = false;
166             n--;
167         }
168         return _reader.skip(n);
169     }
170
171     /**
172      * @see java.io.Reader#close()
173      */

174     public synchronized void close() throws IOException JavaDoc
175     {
176         _hasBufferedChar = false;
177         _reader.close();
178     }
179     
180 }
181
Popular Tags