|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.io.InputStream | +--java.io.FilterInputStream | +--iaik.utils.ReplaceInputStream
This class provides an utility for replacing substrings of a given stream by pre-defined other streams.
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();
Fields inherited from class java.io.FilterInputStream |
in |
Constructor Summary | |
ReplaceInputStream(InputStream is,
String[][] replace)
Creates a ReplaceInputStream from the given InputStream
using the replacements given.
|
Method Summary | |
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(String s)
Set the line separator String. |
Methods inherited from class java.io.FilterInputStream |
available, close, mark, markSupported, read, reset, skip |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public ReplaceInputStream(InputStream is, 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 stringsMethod Detail |
public int read(byte[] b, int off, int len) throws 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 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 readIOException
- if an I/O error occurspublic int read() throws 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 FilterInputStream
IOException
- if an I/O error occurspublic int getReplaceCount()
public static void setLineSeparator(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.
|
This Javadoc may contain text parts from Internet Standard specifications (RFC 2459, 3280, 3039, 2560, 1521, 821, 822, 2253, 1319, 1321, ,2630, 2631, 2268, 3058, 2984, 2104, 2144, 2040, 2311, 2279, see copyright note) and RSA Data Security Public-Key Cryptography Standards (PKCS#1,3,5,7,8,9,10,12, see copyright note). | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |