Clover coverage report - Code Coverage for tapestry release 4.0-rc-2
Coverage timestamp: Sat Dec 17 2005 09:39:46 PST
file stats: LOC: 180   Methods: 11
NCLOC: 95   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ExtendedReader.java 59.1% 54.2% 45.5% 54.3%
coverage coverage
 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  88 public ExtendedReader(Reader in)
 38    {
 39  88 _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  134358 public synchronized int peek() throws IOException
 50    {
 51  134358 if (!_hasBufferedChar) {
 52  64427 int bufferedChar = read();
 53  64427 if (bufferedChar < 0)
 54  187 return bufferedChar;
 55  64240 _bufferedChar = (char) bufferedChar;
 56  64240 _hasBufferedChar = true;
 57    }
 58  134171 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  58696 public synchronized boolean isEndOfStream() throws IOException
 68    {
 69  58696 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  4433 public synchronized void skipCharacters(ICharacterMatcher matcher) throws IOException
 79    {
 80  4433 while (true) {
 81  58696 if (isEndOfStream())
 82  86 break;
 83  58610 char ch = (char) peek();
 84  58610 if (!matcher.matches(ch))
 85  4347 break;
 86  54263 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  128686 public synchronized int read(char[] cbuf, int off, int len) throws IOException
 115    {
 116  128686 if (len <= 0)
 117  0 return 0;
 118   
 119  128686 boolean extraChar = _hasBufferedChar;
 120  128686 if (_hasBufferedChar) {
 121  64240 _hasBufferedChar = false;
 122  64240 cbuf[off++] = _bufferedChar;
 123  64240 len--;
 124    }
 125   
 126  128686 int read = _reader.read(cbuf, off, len);
 127  128686 if (extraChar)
 128  64240 read++;
 129  128686 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    }