2 * gnu/regexp/REFilterInputStream.java
3 * Copyright (C) 1998-2001 Wes Biggs
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.
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.
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.
21 import java.io.FilterInputStream;
22 import java.io.InputStream;
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.
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
35 public class REFilterInputStream extends FilterInputStream {
38 private String replace;
39 private String buffer;
42 private CharIndexedInputStream stream;
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).
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.
55 public REFilterInputStream(InputStream stream, RE expr, String replace) {
57 this.stream = new CharIndexedInputStream(stream,0);
59 this.replace = replace;
63 * Reads the next byte from the stream per the general contract of
64 * InputStream.read(). Returns -1 on error or end of stream.
67 // If we have buffered replace data, use it.
68 if ((buffer != null) && (bufpos < buffer.length())) {
69 return (int) buffer.charAt(bufpos++);
72 // check if input is at a valid position
73 if (!stream.isValid()) return -1;
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);
84 // This is prone to infinite loops if replace string turns out empty.
85 if (buffer.length() > 0) {
86 return buffer.charAt(0);
89 char ch = stream.charAt(0);
90 if (ch == CharIndexed.OUT_OF_BOUNDS) return -1;
97 * Returns false. REFilterInputStream does not support mark() and
100 public boolean markSupported() {
104 /** Reads from the stream into the provided array. */
105 public int read(byte[] b, int off, int len) {
110 if (i == -1) return (ok == 0) ? -1 : ok;
117 /** Reads from the stream into the provided array. */
118 public int read(byte[] b) {
119 return read(b,0,b.length);