PageRenderTime 132ms CodeModel.GetById 120ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/gnu/regexp/REFilterInputStream.java

#
Java | 121 lines | 57 code | 12 blank | 52 comment | 12 complexity | d2a3c6ccbc6374e999b23b44164738d3 MD5 | raw file
  1/*
  2 *  gnu/regexp/REFilterInputStream.java
  3 *  Copyright (C) 1998-2001 Wes Biggs
  4 *
  5 *  This library is free software; you can redistribute it and/or modify
  6 *  it under the terms of the GNU Lesser General Public License as published
  7 *  by the Free Software Foundation; either version 2.1 of the License, or
  8 *  (at your option) any later version.
  9 *
 10 *  This library is distributed in the hope that it will be useful,
 11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13 *  GNU Lesser General Public License for more details.
 14 *
 15 *  You should have received a copy of the GNU Lesser General Public License
 16 *  along with this program; if not, write to the Free Software
 17 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 18 */
 19
 20package gnu.regexp;
 21import java.io.FilterInputStream;
 22import java.io.InputStream;
 23
 24/**
 25 * Replaces instances of a given RE found within an InputStream
 26 * with replacement text.   The replacements are interpolated into the
 27 * stream when a match is found.
 28 *
 29 * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A>
 30 * @deprecated This class cannot properly handle all character
 31 *             encodings.  For proper handling, use the REFilterReader
 32 *             class instead.
 33 */
 34
 35public 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  /**
 45   * Creates an REFilterInputStream.  When reading from this stream,
 46   * occurrences of patterns matching the supplied regular expression
 47   * will be replaced with the supplied replacement text (the
 48   * metacharacters $0 through $9 may be used to refer to the full
 49   * match or subexpression matches).
 50   *
 51   * @param stream The InputStream to be filtered.
 52   * @param expr The regular expression to search for.
 53   * @param replace The text pattern to replace matches with.  
 54   */
 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  /**
 63   * Reads the next byte from the stream per the general contract of
 64   * InputStream.read().  Returns -1 on error or end of stream.
 65   */
 66  public int read() {
 67    // If we have buffered replace data, use it.
 68    if ((buffer != null) && (bufpos < buffer.length())) {
 69      return (int) buffer.charAt(bufpos++);
 70    }
 71
 72    // check if input is at a valid position
 73    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      // This is prone to infinite loops if replace string turns out empty.
 85      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  /** 
 97   * Returns false.  REFilterInputStream does not support mark() and
 98   * reset() methods. 
 99   */
100  public boolean markSupported() {
101    return false;
102  }
103
104  /** Reads from the stream into the provided array. */
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  /** Reads from the stream into the provided array. */
118  public int read(byte[] b) {
119    return read(b,0,b.length);
120  }
121}