ikrs.io
Class ReplacingInputStream
java.lang.Object
java.io.InputStream
ikrs.io.ReplacingInputStream
- All Implemented Interfaces:
- java.io.Closeable
public class ReplacingInputStream
- extends java.io.InputStream
The ReplacingInputStream is an InputStream implementation that allows to
specify a byte sequence replacement map which will be applied directly
on processing the underlying stream.
Imagine you want to read a large text file and want to replace some tokens.
Usually you would convert the file contents - a byte sequence - into a
String and then call the replace/replaceAll method. If there are n tokens
to be replaced you would have to call replace n times.
Instead the ReplacingInputStream matches the given byte patterns directly
while the bytes are read. It uses a MultiStopMarkInputStream to locate
the desired tokens.
Example (pseudo code):
byte[] data = "This %patternA% a %patternB%.".getBytes();
Map replacementMap = { ( "%patternA%".getBytes() => "is".getBytes() ),
( "$patternB%".getbytes() => "test".getBytes() )
};
InputStream in = new ReplacingInputStream( data, replacementMap );
Reading from the stream will then result in the bytes representing:
"This is a test."
Constructor Summary |
ReplacingInputStream(java.io.InputStream in,
java.util.Map<byte[],byte[]> replacementMap)
Create a new ReplacingInputStream. |
Method Summary |
int |
available()
|
void |
close()
|
boolean |
isClosed()
Determines if this input stream was closed. |
static void |
main(java.lang.String[] argv)
|
int |
read()
|
Methods inherited from class java.io.InputStream |
mark, markSupported, read, read, reset, skip |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
ReplacingInputStream
public ReplacingInputStream(java.io.InputStream in,
java.util.Map<byte[],byte[]> replacementMap)
throws java.lang.NullPointerException
- Create a new ReplacingInputStream.
Note A: the passed replacement map will not be copied. Concurrent
modifications might lead to thread collisions.
Note B: the key tokens will be replaced in the order they appear
in the map when using an iterator from the entry set.
- Parameters:
in
- The underlying input stream, must not be null.replacementMap
- The key token / replacement token map.
- Throws:
java.lang.NullPointerException
- If the passed input stream is null or the
replacement map is null or contains any null
entries.
read
public int read()
throws java.io.IOException
- Specified by:
read
in class java.io.InputStream
- Throws:
java.io.IOException
available
public int available()
throws java.io.IOException
- Overrides:
available
in class java.io.InputStream
- Throws:
java.io.IOException
close
public void close()
throws java.io.IOException
- Specified by:
close
in interface java.io.Closeable
- Overrides:
close
in class java.io.InputStream
- Throws:
java.io.IOException
isClosed
public boolean isClosed()
- Determines if this input stream was closed.
- Returns:
- true If the close() method was already called.
main
public static void main(java.lang.String[] argv)