iaik.utils
Class ReplaceInputStream

java.lang.Object
  |
  +--java.io.InputStream
        |
        +--java.io.FilterInputStream
              |
              +--iaik.utils.ReplaceInputStream

public class ReplaceInputStream
extends FilterInputStream

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.:

 String[][] repMat = { {"subString1", "replaceString1" },
{"subString2", "replaceString2" },
...
};
 
. The stream is read-line-based and assumed to be a text stream. It is searched using the standard 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();
 

Version:
File Revision 17

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

ReplaceInputStream

public ReplaceInputStream(InputStream is,
                          String[][] replace)
Creates a 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].
Parameters:
is - the input stream to be processed for replacing specific substrings
replace - the replacement String matrix specifying substrings to be replaced and corresponding replacement strings
Method Detail

read

public int read(byte[] b,
                int off,
                int len)
         throws IOException
Reads up to 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.

Overrides:
read in class FilterInputStream
Parameters:
b - the byte array to which to read the data
off - the offset indicating the start position within the destination byte array, to which the resulting bytes are written
len - the maximum number of bytes to be read
Returns:
the number of bytes actually read from this stream, or -1 if the end of the stream already has been reached
Throws:
IOException - if an I/O error occurs

read

public int read()
         throws IOException
Reads the next byte from this stream and returns it as int value between 0 and 255.

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.

Overrides:
read in class FilterInputStream
Returns:
the next byte as int value between 0 and 255, or -1 if the end of the stream already has been reached
Throws:
IOException - if an I/O error occurs

getReplaceCount

public int getReplaceCount()
Gets the total number of replacements made so far.
Returns:
the number of replacements made so far

setLineSeparator

public static void setLineSeparator(String s)
Set the line separator String. Per default lines will be separated by 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).

IAIK-JCE 3.1 with IAIK-JCE CC Core 3.1, (c) 1997-2004 IAIK