1 20 21 package gnu.regexp; 22 import java.io.FilterReader ; 23 import java.io.Reader ; 24 25 31 32 public class REFilterReader extends FilterReader { 33 34 private RE expr; 35 private String replace; 36 private String buffer; 37 private int bufpos; 38 private int offset; 39 private CharIndexedReader stream; 40 41 52 public REFilterReader(Reader stream, RE expr, String replace) { 53 super(stream); 54 this.stream = new CharIndexedReader(stream,0); 55 this.expr = expr; 56 this.replace = replace; 57 } 58 59 63 public int read() { 64 if ((buffer != null) && (bufpos < buffer.length())) { 66 return (int) buffer.charAt(bufpos++); 67 } 68 69 if (!stream.isValid()) return -1; 71 72 REMatch mymatch = new REMatch(expr.getNumSubs(),offset,0); 73 if (expr.match(stream,mymatch)) { 74 mymatch.end[0] = mymatch.index; 75 mymatch.finish(stream); 76 stream.move(mymatch.toString().length()); 77 offset += mymatch.toString().length(); 78 buffer = mymatch.substituteInto(replace); 79 bufpos = 1; 80 81 if (buffer.length() > 0) { 82 return buffer.charAt(0); 83 } 84 } 85 char ch = stream.charAt(0); 86 if (ch == CharIndexed.OUT_OF_BOUNDS) return -1; 87 stream.move(1); 88 offset++; 89 return ch; 90 } 91 92 96 public boolean markSupported() { 97 return false; 98 } 99 100 101 public int read(char[] b, int off, int len) { 102 int i; 103 int ok = 0; 104 while (len-- > 0) { 105 i = read(); 106 if (i == -1) return (ok == 0) ? -1 : ok; 107 b[off++] = (char) i; 108 ok++; 109 } 110 return ok; 111 } 112 113 114 public int read(char[] b) { 115 return read(b,0,b.length); 116 } 117 } 118 | Popular Tags |