scg/ch14/io/SansCommentFilterReader

From FANG

Jump to: navigation, search

001 package scg.ch14.io;
002 
003 import java.io.FilterReader;
004 import java.io.IOException;
005 import java.io.Reader;
006 
007 /**
008  * This class wraps a {@link Reader} and reads it while eliminating
009  * end-of-line comments. End-of-line comments are defined to be the same
010  * as Java end-of-line comments, beginning with a double slash and
011  * extending to the end of the current line. The whole end-of-line
012  * comment is treated as consisting of the end-of-line character (\n or
013  * \r) found after the end of the comment.<br />
014  <br />
015  * This class is modeled after the SourceReader class presented in
016  * Harold, _Java I/O, 2E_, published by O'Reilly. The Unicode reading
017  * methods were modified for the task at hand. Also, the variable c was
018  * universally replaced with ch (this author (blad) is so pedantic).
019  */
020 public class SansCommentFilterReader
021   extends FilterReader {
022   /** /** state flag; have we already seen the end of the stream? */
023   private boolean endOfStream = false;
024 
025   /** the extra, read-ahead character (-1 if not useful) */
026   private int readAheadCh;
027 
028   /**
029    * Construct a new {@link SansCommentFilterReader} wrapped around the
030    * given {@link Reader}.
031    *
032    @param  in  the {@link Reader} where this reader gets characters.
033    */
034   public SansCommentFilterReader(Reader in{
035     super(in);
036     this.endOfStream = false;
037     this.readAheadCh = -1;
038   }
039 
040   /**
041    * Read one character while skipping end-of-line comments. An
042    * end-of-line comment is offset with a pair of slashes, Java-style:
043    * // this is comment
044    *
045    @return  returns the character read
046    */
047   @Override
048   public int read() throws IOException {
049     int ch;// the character to return
050 
051     // was there already a character read ahead when dealing with a
052     // slash? If so, return that character
053     if (readAheadCh != -1{
054       ch = readAheadCh;
055       readAheadCh = -1;
056       return ch;
057     }
058 
059     ch = in.read();
060 
061     // it can't start a comment so return it
062     if (ch != '/'{
063       return ch;
064     }
065 
066     // is it the start of a comment?
067     readAheadCh = in.read();
068 
069     // not start of comment; keep readAheadCh and return ch
070     if (readAheadCh != '/'{
071       return ch;
072     }
073 
074     readAheadCh = -1;// readAheadCh part of comment
075     while ((ch != '\n'&& (ch != '\r')) {// read to end-of-line
076       ch = in.read();
077     }
078 
079     return ch;// return end-of-line character that terminated loop
080   }
081 
082   /**
083    * Read length characters into text starting at offset. Written to use
084    {@link #read()} so only that method need know anything about
085    * comments. Do Not Repeat Yourself.
086    *
087    @param   text    character buffer where characters are to be placed
088    @param   offset  the offset into text where characters are to be
089    *                  saved
090    @param   length  number of characters desired
091    *
092    @return  number of characters read or -1 if read is past end of
093    *          input stream
094    */
095   @Override
096   public int read(char[] text, int offset, int length)
097     throws IOException {
098     if (endOfStream{
099       return -1;// end already reached
100     }
101 
102     int charCount = 0;
103     for (int = offset; i < (offset + length); i++{
104       int temp = this.read();
105       if (temp == -1{
106         endOfStream = true;
107         break;
108       }
109       text[i] = (chartemp;
110       charCount++;
111     }
112     return charCount;
113   }
114 
115   /**
116    * Skip over the given number of characters. Characters are counted
117    * after removal of comments
118    *
119    @param   n  number of characters to skip over
120    *
121    @return  the number of characters actually skipped over; -1 if skip
122    *          is past end of input stream
123    */
124   @Override
125   public long skip(long nthrows IOException {
126     char[] chArray = new char[(intn];
127     int charCountSkipped = this.read(chArray);
128     return charCountSkipped;
129   }
130 }
131 
132 //Uploaded on Mon Mar 29 21:40:05 EDT 2010


Download/View scg/ch14/io/SansCommentFilterReader.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter

Games
Games