de.fhg.igd.io
Class VariableSubstitutionInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by de.fhg.igd.io.VariableSubstitutionInputStream
All Implemented Interfaces:
Closeable

public class VariableSubstitutionInputStream
extends InputStream

This class wrappes an InputStream and provides substitution of variables while reading from it. Variables to be substituted have to be embraced by braces and preceded by a dollar sign (${variable}). A simple $ without following opening brace is forwarded as is. The leading backslash in \${ escapes the dollar sign. Hence, this character sequence is forwarded as single dollar sign without forcing any variable substition, too.
The variables to be substituted are given as name/value-Map.

Version:
$Id: VariableSubstitutionInputStream.java 1913 2007-08-08 02:41:53Z jpeters $
Author:
Jan Peters

Field Summary
private  PushbackInputStream pis_
          The wrapper for the given input stream.
private  SortedMap variables_
          Internal map which stores the variables to substitute as key/value-pairs (both of type String).
 
Constructor Summary
VariableSubstitutionInputStream(InputStream is, Map variables)
          Creates a VariableSubstitutionInputStream which wrappes the given InputStream.
 
Method Summary
 int available()
          Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream.
 void close()
          Closes the wrapped input stream and releases any system resources associated with the stream.
private  void init(InputStream is, Map variables)
          Initializes the class instance.
private  byte[] lookup(String key)
          Looks up the given variable.
static void main(String[] argv)
          Just for testing purpose.
 int read()
          Reads the next byte of data from the input stream.
 
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
 

Field Detail

pis_

private PushbackInputStream pis_
The wrapper for the given input stream.


variables_

private SortedMap variables_
Internal map which stores the variables to substitute as key/value-pairs (both of type String).

Constructor Detail

VariableSubstitutionInputStream

public VariableSubstitutionInputStream(InputStream is,
                                       Map variables)
Creates a VariableSubstitutionInputStream which wrappes the given InputStream.

Parameters:
is - the InputStream to be wrapped.
variables - the name/value Map used for variable substitution.
Method Detail

init

private void init(InputStream is,
                  Map variables)
Initializes the class instance.


lookup

private byte[] lookup(String key)
Looks up the given variable.


read

public int read()
         throws IOException
Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

A subclass must provide an implementation of this method.

Specified by:
read in class InputStream
Returns:
the next byte of data, or -1 if the end of the stream is reached.
Throws:
IOException - if an I/O error occurs.

available

public int available()
              throws IOException
Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream.

Since the number of bytes available can not be determined in advance the available method for this class always returns 0.

Overrides:
available in class InputStream
Returns:
The number of bytes that can be read from this input stream without blocking.
Throws:
IOException - if an I/O error occurs.

close

public void close()
           throws IOException
Closes the wrapped input stream and releases any system resources associated with the stream.

The close method of InputStream does nothing.

Specified by:
close in interface Closeable
Overrides:
close in class InputStream
Throws:
IOException - if an I/O error occurs.

main

public static void main(String[] argv)
Just for testing purpose.



Copyright © Fraunhofer Gesellschaft. All Rights Reserved.