public class ReplaceInputStream
extends java.io.FilterInputStream
The substrings to be searched for and their replacing counterparts are given in a String matrix consisting of two columns and any rows, depending on the number of strings to be searched for replacement, e.g.:
. The stream is read-line-based and assumed to be a text stream. It is searched using the standardString[][] repMat = { {"subString1", "replaceString1" }, {"subString2", "replaceString2" }, ... };
indexOf
method of the
String
class, which may result in a rather high processing time
when a large number of replacement strings is given. While reading data from
the stream by using one of the read
methods, it is searched for
occurences of any of the search strings to be replaced by the corresponding
replacement string.
The following example reads text data from a fictitious "test.java" file, replaces any "String" substring by "StringBuffer", and any "public static" substring by "public static final", and writes the result to a "test1.java" file:
String[][] repStr = { { "String", "StringBuffer" }, { "public static", "public static final" } }; FileInputStream fis = new FileInputStream("test.java"); ReplaceInputStream ris = new ReplaceInputStream(fis, repStr); FileOutputStream fos = new FileOutputStream("test1.java"); byte[] b = new byte[8]; int i = ris.read(b); while (i != -1) { fos.write(b, 0, i); i = ris.read(b); } fos.flush(); fos.close(); ris.close();
Constructor and Description |
---|
ReplaceInputStream(java.io.InputStream is,
java.lang.String[][] replace)
Creates a
ReplaceInputStream from the given InputStream using
the replacements given. |
Modifier and Type | Method and Description |
---|---|
int |
getReplaceCount()
Gets the total number of replacements made so far.
|
int |
read()
Reads the next byte from this stream and returns it as int value between 0
and 255.
|
int |
read(byte[] b,
int off,
int len)
Reads up to
len bytes from this stream into the given byte
array. |
static void |
setLineSeparator(java.lang.String s)
Set the line separator String.
|
public ReplaceInputStream(java.io.InputStream is, java.lang.String[][] replace)
ReplaceInputStream
from the given InputStream using
the replacements given. When reading from this stream by using one of the
read
methods, every occurence of replace[i][0]
in the stream will be replaced by replace[i][1]
.is
- the input stream to be processed for replacing specific substringsreplace
- the replacement String matrix specifying substrings to be replaced
and corresponding replacement stringspublic int read(byte[] b, int off, int len) throws java.io.IOException
len
bytes from this stream into the given byte
array.
Actually a line is read from the text stream and searched for any occurance
of any of the specified search strings to be replaced by the corresponding
replacement string. len
bytes are written to the given byte
array, starting at off
.
read
in class java.io.FilterInputStream
b
- the byte array to which to read the dataoff
- the offset indicating the start position within the destination
byte array, to which the resulting bytes are writtenlen
- the maximum number of bytes to be readjava.io.IOException
- if an I/O error occurspublic int read() throws java.io.IOException
Data actually is read from the text stream line by line. Specified substrings are replaced by the corresponding replacement strings and one byte of the resulting data is returned by this method.
read
in class java.io.FilterInputStream
java.io.IOException
- if an I/O error occurspublic int getReplaceCount()
public static void setLineSeparator(java.lang.String s)
System.getProperty("line.separator")
, which will be \n (LF)
under Unix and \r\n (CR LF) under Windows systems. This method lets you set
the line separator manually to whatever string you want.