@ Override public void write ( char [ ] cbuf , int off , int len ) throws IOException { for ( int count = 0 ; count < len ; ++ off , ++ count ) { char c = cbuf [ off ] ; switch ( state ) { case TEXT : if ( c == ' ' ) { state = State . VARIABLE_MARK ; } else { targetWriter . write ( c ) ; } break ; case VARIABLE_MARK : if ( c == ' ' ) { state = State . VARIABLE ; variableBuilder . setLength ( 0 ) ; } else { targetWriter . write ( ' ' ) ; targetWriter . write ( c ) ; state = State . TEXT ; } break ; case VARIABLE : if ( c == ' ' ) { String variable = variables . get ( variableBuilder . toString ( ) ) ; if ( variable == null ) { // if variable not found it should be an internal variable used by processing file // for example ${BUILD} from ant files; copy as it is targetWriter . write ( "${" ) ; targetWriter . write ( variableBuilder . toString ( ) ) ; targetWriter . write ( ' ' ) ; } else { targetWriter . write ( variable ) ; } state = State . TEXT ; } else { variableBuilder . append ( c ) ; } break ; default : throw new IllegalStateException ( ) ; } } }
Inject values into given variables stream and write the result to target writer .