scg/ch14/io/NoCurlyFilterReader

From FANG

Jump to: navigation, search

01 package scg.ch14.io;
02 
03 import java.io.FilterReader;
04 import java.io.IOException;
05 import java.io.Reader;
06 
07 /**
08  * This class wraps a {@link Reader} and reads it while eliminating
09  * curly braces.Modeled after the SourceReader class presented in
10  * Harold, _Java I/O, 2E_
11  */
12 public class NoCurlyFilterReader
13   extends FilterReader {
14   /** state flag; have we already seen the end of the stream? */
15   private boolean endOfStream = false;
16 
17   /**
18    * New {@link NoCurlyFilterReader} wrapped around the {@link Reader}.
19    *
20    @param  in  the {@link Reader} where this reader gets characters.
21    */
22   public NoCurlyFilterReader(Reader in{
23     super(in);
24     this.endOfStream = false;
25   }
26 
27   /**
28    * Read one character while replacing '{' and ' }' with '|'
29    *
30    @return  the character read
31    */
32   @Override
33   public int read() throws IOException {
34     int ch;// the character to return
35 
36     ch = in.read();
37     if ((ch == '{'|| (ch == '}')) {
38       ch = '|';
39     }
40     return ch;
41   }
42 
43   /**
44    * Fill char array buffer with {@link #read()} (DRY!for char map
45    *
46    @param   text    character buffer where characters are to be placed
47    @param   offset  offset into text to begin writing
48    @param   length  number of characters desired
49    *
50    @return  number of char read or -1 if past end of stream
51    */
52   @Override
53   public int read(char[] text, int offset, int length)
54     throws IOException {
55     if (endOfStream{
56       return -1;// end already reached
57     }
58 
59     int charCount = 0;
60     for (int = offset; i < (offset + length); i++{
61       int temp = this.read();
62       if (temp == -1{
63         endOfStream = true;
64         break;
65       }
66       text[i] = (chartemp;
67       charCount++;
68     }
69     return charCount;
70   }
71 
72   /**
73    * Skip over the given number of characters.
74    *
75    @param   n  number of characters to skip over
76    *
77    @return  number of char skipped or -1 if past end of stream
78    */
79   @Override
80   public long skip(long nthrows IOException {
81     char[] chArray = new char[(intn];
82     int charCountSkipped = this.read(chArray);
83     return charCountSkipped;
84   }
85 }
86 
87 //Uploaded on Mon Mar 29 21:40:34 EDT 2010


Download/View scg/ch14/io/NoCurlyFilterReader.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