1 19 20 package gnu.regexp; 21 import java.io.FilterInputStream ; 22 import java.io.InputStream ; 23 24 34 35 public class REFilterInputStream extends FilterInputStream { 36 37 private RE expr; 38 private String replace; 39 private String buffer; 40 private int bufpos; 41 private int offset; 42 private CharIndexedInputStream stream; 43 44 55 public REFilterInputStream(InputStream stream, RE expr, String replace) { 56 super(stream); 57 this.stream = new CharIndexedInputStream(stream,0); 58 this.expr = expr; 59 this.replace = replace; 60 } 61 62 66 public int read() { 67 if ((buffer != null) && (bufpos < buffer.length())) { 69 return (int) buffer.charAt(bufpos++); 70 } 71 72 if (!stream.isValid()) return -1; 74 75 REMatch mymatch = new REMatch(expr.getNumSubs(),offset,0); 76 if (expr.match(stream, mymatch)) { 77 mymatch.end[0] = mymatch.index; 78 mymatch.finish(stream); 79 stream.move(mymatch.toString().length()); 80 offset += mymatch.toString().length(); 81 buffer = mymatch.substituteInto(replace); 82 bufpos = 1; 83 84 if (buffer.length() > 0) { 86 return buffer.charAt(0); 87 } 88 } 89 char ch = stream.charAt(0); 90 if (ch == CharIndexed.OUT_OF_BOUNDS) return -1; 91 stream.move(1); 92 offset++; 93 return ch; 94 } 95 96 100 public boolean markSupported() { 101 return false; 102 } 103 104 105 public int read(byte[] b, int off, int len) { 106 int i; 107 int ok = 0; 108 while (len-- > 0) { 109 i = read(); 110 if (i == -1) return (ok == 0) ? -1 : ok; 111 b[off++] = (byte) i; 112 ok++; 113 } 114 return ok; 115 } 116 117 118 public int read(byte[] b) { 119 return read(b,0,b.length); 120 } 121 } 122 | Popular Tags |