Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  // Copyright 2004, 2005 The Apache Software Foundation
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //     http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
 // distributed under the License is distributed on an "AS IS" BASIS,
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
 package org.apache.tapestry.util.text;
 
 import java.io.Reader;

A Reader that provides some additional functionality, such as peek().

Author(s):
mb
Since:
4.0
 
 public class ExtendedReader extends Reader
 {
     private Reader _reader;
     private boolean _hasBufferedChar = false;
     private char _bufferedChar;
    
    
Creates a new extended reader that reads from the provided object.

Parameters:
in the Reader to get data from
 
     public ExtendedReader(Reader in)
     {
          = in;
     }

    
Returns the next character in the stream without actually comitting the read. Multiple consequtive invocations of this method should return the same value.

Returns:
the next character waiting in the stream or -1 if the end of the stream is reached
Throws:
java.io.IOException if an error occurs
 
     public synchronized int peek() throws IOException
     {
         if (!) {
             int bufferedChar = read();
             if (bufferedChar < 0)
                 return bufferedChar;
              = (charbufferedChar;
              = true;
         }
         return ;
     }
    
    
Determines whether the end of the stream is reached.

Returns:
true if at the end of stream
Throws:
java.io.IOException if an error occurs
 
     public synchronized boolean isEndOfStream() throws IOException
     {
         return peek() < 0;
     }

    
Skips the next characters until a character that does not match the provided rule is reached.

Parameters:
matcher the object determining whether a character should be skipped
Throws:
java.io.IOException if an error occurs
 
     public synchronized void skipCharacters(ICharacterMatcher matcherthrows IOException
     {
         while (true) {
             if (isEndOfStream())
                 break;
             char ch = (charpeek();
             if (!matcher.matches(ch))
                 break;
             read();
         }
     }
    
    
Reads the next characters until a character that does not match the provided rule is reached.

Parameters:
matcher the object determining whether a character should be read
Returns:
the string of characters read
Throws:
java.io.IOException if an error occurs
 
     public synchronized String readCharacters(ICharacterMatcher matcherthrows IOException
     {
         StringBuffer buf = new StringBuffer();
        while (true) {
            if (isEndOfStream())
                break;
            char ch = (charpeek();
            if (!matcher.matches(ch))
                break;
            buf.append(read());
        }
        return buf.toString();
    }
    
    
    public synchronized int read(char[] cbufint offint lenthrows IOException
    {
        int offset = off;
        if (len <= 0)
            return 0;
        int readLength = len;
        
        boolean extraChar = ;
        if () {
             = false;
            cbuf[offset++] = ;
            readLength--;
        }
        int read = .read(cbufoffsetreadLength);
        if (extraChar)
            read++;
        return read;
    }
    
    
    public synchronized boolean ready() throws IOException
    {
        if ()
            return true;
        return .ready();
    }
    
    
    public synchronized boolean markSupported()
    {
        return false;
    }
    
    
    public synchronized void reset() throws IOException
    {
         = false;
        .reset();
    }
    
    
    public synchronized long skip(long nthrows IOException
    {
        long skipChars = n;
        if ( && skipChars > 0) {
             = false;
            skipChars--;
        }
        return .skip(skipChars);
    }

    
    public synchronized void close() throws IOException
    {
         = false;
        .close();
    }
    
New to GrepCode? Check out our FAQ X