aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/taskdefs/Redirector.java
blob: 3b35d2313ce4375ef70ac338c7798e9fae9a8b96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package org.apache.tools.ant.taskdefs;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.Arrays;
import java.util.Vector;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectComponent;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.filters.util.ChainReaderHelper;
import org.apache.tools.ant.types.FilterChain;
import org.apache.tools.ant.util.ConcatFileInputStream;
import org.apache.tools.ant.util.KeepAliveOutputStream;
import org.apache.tools.ant.util.LazyFileOutputStream;
import org.apache.tools.ant.util.LeadPipeInputStream;
import org.apache.tools.ant.util.LineOrientedOutputStreamRedirector;
import org.apache.tools.ant.util.OutputStreamFunneler;
import org.apache.tools.ant.util.ReaderInputStream;
import org.apache.tools.ant.util.StringUtils;
import org.apache.tools.ant.util.TeeOutputStream;

/**
 * The Redirector class manages the setup and connection of input and output
 * redirection for an Ant project component.
 *
 * @since Ant 1.6
 */
public class Redirector {
    private static final int STREAMPUMPER_WAIT_INTERVAL = 1000;

    private static final String DEFAULT_ENCODING = System
            .getProperty("file.encoding");

    private class PropertyOutputStream extends ByteArrayOutputStream {
        private final String property;

        private boolean closed = false;

        PropertyOutputStream(final String property) {
            super();
            this.property = property;
        }

        @Override
        public void close() throws IOException {
            synchronized (outMutex) {
                if (!closed && !(appendOut && appendProperties)) {
                    setPropertyFromBAOS(this, property);
                    closed = true;
                }
            }
        }
    }

    /**
     * The file(s) from which standard input is being taken. If > 1, files'
     * content will be concatenated in the order received.
     */
    private File[] input;

    /**
     * The file(s) receiving standard output. Will also receive standard error
     * unless standard error is redirected or logError is true.
     */
    private File[] out;

    /**
     * The file(s) to which standard error is being redirected
     */
    private File[] error;

    /**
     * Indicates if standard error should be logged to Ant's log system rather
     * than the output. This has no effect if standard error is redirected to a
     * file or property.
     */
    private boolean logError = false;

    /**
     * Buffer used to capture output for storage into a property
     */
    private PropertyOutputStream baos = null;

    /**
     * Buffer used to capture error output for storage into a property
     */
    private PropertyOutputStream errorBaos = null;

    /** The name of the property into which output is to be stored */
    private String outputProperty;

    /** The name of the property into which error output is to be stored */
    private String errorProperty;

    /** String from which input is taken */
    private String inputString;

    /** Flag which indicates if error and output files are to be appended. */
    private boolean appendOut = false;

    private boolean appendErr = false;

    /** Flag which indicates that output should be always sent to the log */
    private boolean alwaysLogOut = false;

    private boolean alwaysLogErr = false;

    /** Flag which indicates whether files should be created even when empty. */
    private boolean createEmptyFilesOut = true;

    private boolean createEmptyFilesErr = true;

    /** The task for which this redirector is working */
    private final ProjectComponent managingTask;

    /** The stream for output data */
    private OutputStream outputStream = null;

    /** The stream for error output */
    private OutputStream errorStream = null;

    /** The stream for input */
    private InputStream inputStream = null;

    /** Stream which is used for line oriented output */
    private PrintStream outPrintStream = null;

    /** Stream which is used for line oriented error output */
    private PrintStream errorPrintStream = null;

    /** The output filter chains */
    private Vector<FilterChain> outputFilterChains;

    /** The error filter chains */
    private Vector<FilterChain> errorFilterChains;

    /** The input filter chains */
    private Vector<FilterChain> inputFilterChains;

    /** The output encoding */
    private String outputEncoding = DEFAULT_ENCODING;

    /** The error encoding */
    private String errorEncoding = DEFAULT_ENCODING;

    /** The input encoding */
    private String inputEncoding = DEFAULT_ENCODING;

    /** Whether to complete properties settings **/
    private boolean appendProperties = true;

    /** The thread group used for starting <code>StreamPumper</code> threads */
    private final ThreadGroup threadGroup = new ThreadGroup("redirector");

    /** whether to log the inputstring */
    private boolean logInputString = true;

    /** Mutex for in */
    private final Object inMutex = new Object();

    /** Mutex for out */
    private final Object outMutex = new Object();

    /** Mutex for err */
    private final Object errMutex = new Object();

    /** Is the output binary or can we safely split it into lines? */
    private boolean outputIsBinary = false;

    /**
     * Create a redirector instance for the given task
     *
     * @param managingTask
     *            the task for which the redirector is to work
     */
    public Redirector(final Task managingTask) {
        this((ProjectComponent) managingTask);
    }

    /**
     * Create a redirector instance for the given task
     *
     * @param managingTask
     *            the project component for which the redirector is to work
     * @since Ant 1.6.3
     */
    public Redirector(final ProjectComponent managingTask) {
        this.managingTask = managingTask;
    }

    /**
     * Set the input to use for the task
     *
     * @param input
     *            the file from which input is read.
     */
    public void setInput(final File input) {
        setInput((input == null) ? null : new File[] {input});
    }

    /**
     * Set the input to use for the task
     *
     * @param input
     *            the files from which input is read.
     */
    public void setInput(final File[] input) {
        synchronized (inMutex) {
            if (input == null) {
                this.input = null;
            } else {
                this.input = input.clone();
            }
        }
    }

    /**
     * Set the string to use as input
     *
     * @param inputString
     *            the string which is used as the input source
     */
    public void setInputString(final String inputString) {
        synchronized (inMutex) {
            this.inputString = inputString;
        }
    }

    /**
     * Set whether to include the value of the input string in log messages.
     * Defaults to true.
     *
     * @param logInputString
     *            true or false.
     * @since Ant 1.7
     */
    public void setLogInputString(final boolean logInputString) {
        this.logInputString = logInputString;
    }

    /**
     * Set a stream to use as input.
     *
     * @param inputStream
     *            the stream from which input will be read
     * @since Ant 1.6.3
     */
    /* public */void setInputStream(final InputStream inputStream) {
        synchronized (inMutex) {
            this.inputStream = inputStream;
        }
    }

    /**
     * File the output of the process is redirected to. If error is not
     * redirected, it too will appear in the output
     *
     * @param out
     *            the file to which output stream is written
     */
    public void setOutput(final File out) {
        setOutput((out == null) ? null : new File[] {out});
    }

    /**
     * Files the output of the process is redirected to. If error is not
     * redirected, it too will appear in the output
     *
     * @param out
     *            the files to which output stream is written
     */
    public void setOutput(final File[] out) {
        synchronized (outMutex) {
            if (out == null) {
                this.out = null;
            } else {
                this.out = out.clone();
            }
        }
    }

    /**
     * Set the output encoding.
     *
     * @param outputEncoding
     *            <code>String</code>.
     */
    public void setOutputEncoding(final String outputEncoding) {
        if (outputEncoding == null) {
            throw new IllegalArgumentException(
                    "outputEncoding must not be null");
        }
        synchronized (outMutex) {
            this.outputEncoding = outputEncoding;
        }
    }

    /**
     * Set the error encoding.
     *
     * @param errorEncoding
     *            <code>String</code>.
     */
    public void setErrorEncoding(final String errorEncoding) {
        if (errorEncoding == null) {
            throw new IllegalArgumentException("errorEncoding must not be null");
        }
        synchronized (errMutex) {
            this.errorEncoding = errorEncoding;
        }
    }

    /**
     * Set the input encoding.
     *
     * @param inputEncoding
     *            <code>String</code>.
     */
    public void setInputEncoding(final String inputEncoding) {
        if (inputEncoding == null) {
            throw new IllegalArgumentException("inputEncoding must not be null");
        }
        synchronized (inMutex) {
            this.inputEncoding = inputEncoding;
        }
    }

    /**
     * Controls whether error output of exec is logged. This is only useful when
     * output is being redirected and error output is desired in the Ant log
     *
     * @param logError
     *            if true the standard error is sent to the Ant log system and
     *            not sent to output.
     */
    public void setLogError(final boolean logError) {
        synchronized (errMutex) {
            this.logError = logError;
        }
    }

    /**
     * This <code>Redirector</code>'s subordinate
     * <code>PropertyOutputStream</code>s will not set their respective
     * properties <code>while (appendProperties &amp;&amp; append)</code>.
     *
     * @param appendProperties
     *            whether to append properties.
     */
    public void setAppendProperties(final boolean appendProperties) {
        synchronized (outMutex) {
            this.appendProperties = appendProperties;
        }
    }

    /**
     * Set the file to which standard error is to be redirected.
     *
     * @param error
     *            the file to which error is to be written
     */
    public void setError(final File error) {
        setError((error == null) ? null : new File[] {error});
    }

    /**
     * Set the files to which standard error is to be redirected.
     *
     * @param error
     *            the file to which error is to be written
     */
    public void setError(final File[] error) {
        synchronized (errMutex) {
            if (error == null) {
                this.error = null;
            } else {
                this.error = error.clone();
            }
        }
    }

    /**
     * Property name whose value should be set to the output of the process.
     *
     * @param outputProperty
     *            the name of the property to be set with the task's output.
     */
    public void setOutputProperty(final String outputProperty) {
        if (outputProperty == null
                || !(outputProperty.equals(this.outputProperty))) {
            synchronized (outMutex) {
                this.outputProperty = outputProperty;
                baos = null;
            }
        }
    }

    /**
     * Whether output should be appended to or overwrite an existing file.
     * Defaults to false.
     *
     * @param append
     *            if true output and error streams are appended to their
     *            respective files, if specified.
     */
    public void setAppend(final boolean append) {
        synchronized (outMutex) {
            appendOut = append;
        }
        synchronized (errMutex) {
            appendErr = append;
        }
    }

    /**
     * If true, (error and non-error) output will be "teed", redirected as
     * specified while being sent to Ant's logging mechanism as if no
     * redirection had taken place. Defaults to false.
     *
     * @param alwaysLog
     *            <code>boolean</code>
     * @since Ant 1.6.3
     */
    public void setAlwaysLog(final boolean alwaysLog) {
        synchronized (outMutex) {
            alwaysLogOut = alwaysLog;
        }
        synchronized (errMutex) {
            alwaysLogErr = alwaysLog;
        }
    }

    /**
     * Whether output and error files should be created even when empty.
     * Defaults to true.
     *
     * @param createEmptyFiles
     *            <code>boolean</code>.
     */
    public void setCreateEmptyFiles(final boolean createEmptyFiles) {
        synchronized (outMutex) {
            createEmptyFilesOut = createEmptyFiles;
        }
        synchronized (outMutex) {
            createEmptyFilesErr = createEmptyFiles;
        }
    }

    /**
     * Property name whose value should be set to the error of the process.
     *
     * @param errorProperty
     *            the name of the property to be set with the error output.
     */
    public void setErrorProperty(final String errorProperty) {
        synchronized (errMutex) {
            if (errorProperty == null
                    || !(errorProperty.equals(this.errorProperty))) {
                this.errorProperty = errorProperty;
                errorBaos = null;
            }
        }
    }

    /**
     * Set the input <code>FilterChain</code>s.
     *
     * @param inputFilterChains
     *            <code>Vector</code> containing <code>FilterChain</code>.
     */
    public void setInputFilterChains(final Vector<FilterChain> inputFilterChains) {
        synchronized (inMutex) {
            this.inputFilterChains = inputFilterChains;
        }
    }

    /**
     * Set the output <code>FilterChain</code>s.
     *
     * @param outputFilterChains
     *            <code>Vector</code> containing <code>FilterChain</code>.
     */
    public void setOutputFilterChains(final Vector<FilterChain> outputFilterChains) {
        synchronized (outMutex) {
            this.outputFilterChains = outputFilterChains;
        }
    }

    /**
     * Set the error <code>FilterChain</code>s.
     *
     * @param errorFilterChains
     *            <code>Vector</code> containing <code>FilterChain</code>.
     */
    public void setErrorFilterChains(final Vector<FilterChain> errorFilterChains) {
        synchronized (errMutex) {
            this.errorFilterChains = errorFilterChains;
        }
    }

    /**
     * Whether to consider the output created by the process binary.
     *
     * <p>Binary output will not be split into lines which may make
     * error and normal output look mixed up when they get written to
     * the same stream.</p>
     * @since 1.9.4
     */
    public void setBinaryOutput(final boolean b) {
        outputIsBinary = b;
    }

    /**
     * Set a property from a ByteArrayOutputStream
     *
     * @param baos
     *            contains the property value.
     * @param propertyName
     *            the property name.
     *
     * @exception IOException
     *                if the value cannot be read form the stream.
     */
    private void setPropertyFromBAOS(final ByteArrayOutputStream baos,
            final String propertyName) throws IOException {

        final BufferedReader in = new BufferedReader(new StringReader(Execute
                .toString(baos)));
        String line = null;
        final StringBuffer val = new StringBuffer();
        while ((line = in.readLine()) != null) {
            if (val.length() != 0) {
                val.append(StringUtils.LINE_SEP);
            }
            val.append(line);
        }
        managingTask.getProject().setNewProperty(propertyName, val.toString());
    }

    /**
     * Create the input, error and output streams based on the configuration
     * options.
     */
    public void createStreams() {

        synchronized (outMutex) {
            outStreams();
            if (alwaysLogOut || outputStream == null) {
                final OutputStream outputLog = new LogOutputStream(managingTask,
                        Project.MSG_INFO);
                outputStream = (outputStream == null) ? outputLog
                        : new TeeOutputStream(outputLog, outputStream);
            }

            if ((outputFilterChains != null && outputFilterChains.size() > 0)
                    || !(outputEncoding.equalsIgnoreCase(inputEncoding))) {
                try {
                    final LeadPipeInputStream snk = new LeadPipeInputStream();
                    snk.setManagingComponent(managingTask);

                    InputStream outPumpIn = snk;

                    Reader reader = new InputStreamReader(outPumpIn,
                            inputEncoding);

                    if (outputFilterChains != null
                            && outputFilterChains.size() > 0) {
                        final ChainReaderHelper helper = new ChainReaderHelper();
                        helper.setProject(managingTask.getProject());
                        helper.setPrimaryReader(reader);
                        helper.setFilterChains(outputFilterChains);
                        reader = helper.getAssembledReader();
                    }
                    outPumpIn = new ReaderInputStream(reader, outputEncoding);

                    final Thread t = new Thread(threadGroup, new StreamPumper(
                            outPumpIn, outputStream, true), "output pumper");
                    t.setPriority(Thread.MAX_PRIORITY);
                    outputStream = new PipedOutputStream(snk);
                    t.start();
                } catch (final IOException eyeOhEx) {
                    throw new BuildException("error setting up output stream",
                            eyeOhEx);
                }
            }
        }

        synchronized (errMutex) {
            errorStreams();
            if (alwaysLogErr || errorStream == null) {
                final OutputStream errorLog = new LogOutputStream(managingTask,
                        Project.MSG_WARN);
                errorStream = (errorStream == null) ? errorLog
                        : new TeeOutputStream(errorLog, errorStream);
            }

            if ((errorFilterChains != null && errorFilterChains.size() > 0)
                    || !(errorEncoding.equalsIgnoreCase(inputEncoding))) {
                try {
                    final LeadPipeInputStream snk = new LeadPipeInputStream();
                    snk.setManagingComponent(managingTask);

                    InputStream errPumpIn = snk;

                    Reader reader = new InputStreamReader(errPumpIn,
                            inputEncoding);

                    if (errorFilterChains != null
                            && errorFilterChains.size() > 0) {
                        final ChainReaderHelper helper = new ChainReaderHelper();
                        helper.setProject(managingTask.getProject());
                        helper.setPrimaryReader(reader);
                        helper.setFilterChains(errorFilterChains);
                        reader = helper.getAssembledReader();
                    }
                    errPumpIn = new ReaderInputStream(reader, errorEncoding);

                    final Thread t = new Thread(threadGroup, new StreamPumper(
                            errPumpIn, errorStream, true), "error pumper");
                    t.setPriority(Thread.MAX_PRIORITY);
                    errorStream = new PipedOutputStream(snk);
                    t.start();
                } catch (final IOException eyeOhEx) {
                    throw new BuildException("error setting up error stream",
                            eyeOhEx);
                }
            }
        }

        synchronized (inMutex) {
            // if input files are specified, inputString and inputStream are
            // ignored;
            // classes that work with redirector attributes can enforce
            // whatever warnings are needed
            if (input != null && input.length > 0) {
                managingTask
                        .log("Redirecting input from file"
                                + ((input.length == 1) ? "" : "s"),
                                Project.MSG_VERBOSE);
                try {
                    inputStream = new ConcatFileInputStream(input);
                } catch (final IOException eyeOhEx) {
                    throw new BuildException(eyeOhEx);
                }
                ((ConcatFileInputStream) inputStream)
                        .setManagingComponent(managingTask);
            } else if (inputString != null) {
                final StringBuffer buf = new StringBuffer("Using input ");
                if (logInputString) {
                    buf.append('"').append(inputString).append('"');
                } else {
                    buf.append("string");
                }
                managingTask.log(buf.toString(), Project.MSG_VERBOSE);
                inputStream = new ByteArrayInputStream(inputString.getBytes());
            }

            if (inputStream != null && inputFilterChains != null
                    && inputFilterChains.size() > 0) {
                final ChainReaderHelper helper = new ChainReaderHelper();
                helper.setProject(managingTask.getProject());
                try {
                    helper.setPrimaryReader(new InputStreamReader(inputStream,
                            inputEncoding));
                } catch (final IOException eyeOhEx) {
                    throw new BuildException("error setting up input stream",
                            eyeOhEx);
                }
                helper.setFilterChains(inputFilterChains);
                inputStream = new ReaderInputStream(
                        helper.getAssembledReader(), inputEncoding);
            }
        }
    }

    /** outStreams */
    private void outStreams() {
        if (out != null && out.length > 0) {
            final String logHead = new StringBuffer("Output ").append(
                    ((appendOut) ? "appended" : "redirected")).append(" to ")
                    .toString();
            outputStream = foldFiles(out, logHead, Project.MSG_VERBOSE,
                    appendOut, createEmptyFilesOut);
        }
        if (outputProperty != null) {
            if (baos == null) {
                baos = new PropertyOutputStream(outputProperty);
                managingTask.log("Output redirected to property: "
                        + outputProperty, Project.MSG_VERBOSE);
            }
            // shield it from being closed by a filtering StreamPumper
            final OutputStream keepAliveOutput = new KeepAliveOutputStream(baos);
            outputStream = (outputStream == null) ? keepAliveOutput
                    : new TeeOutputStream(outputStream, keepAliveOutput);
        } else {
            baos = null;
        }
    }

    private void errorStreams() {
        if (error != null && error.length > 0) {
            final String logHead = new StringBuffer("Error ").append(
                    ((appendErr) ? "appended" : "redirected")).append(" to ")
                    .toString();
            errorStream = foldFiles(error, logHead, Project.MSG_VERBOSE,
                    appendErr, createEmptyFilesErr);
        } else if (!(logError || outputStream == null) && errorProperty == null) {
            final long funnelTimeout = 0L;
            final OutputStreamFunneler funneler = new OutputStreamFunneler(
                    outputStream, funnelTimeout);
            try {
                outputStream = funneler.getFunnelInstance();
                errorStream = funneler.getFunnelInstance();
                if (!outputIsBinary) {
                    outputStream = new LineOrientedOutputStreamRedirector(outputStream);
                    errorStream = new LineOrientedOutputStreamRedirector(errorStream);
                }
            } catch (final IOException eyeOhEx) {
                throw new BuildException(
                        "error splitting output/error streams", eyeOhEx);
            }
        }
        if (errorProperty != null) {
            if (errorBaos == null) {
                errorBaos = new PropertyOutputStream(errorProperty);
                managingTask.log("Error redirected to property: "
                        + errorProperty, Project.MSG_VERBOSE);
            }
            // shield it from being closed by a filtering StreamPumper
            final OutputStream keepAliveError = new KeepAliveOutputStream(errorBaos);
            errorStream = (error == null || error.length == 0) ? keepAliveError
                    : new TeeOutputStream(errorStream, keepAliveError);
        } else {
            errorBaos = null;
        }
    }

    /**
     * Create the StreamHandler to use with our Execute instance.
     *
     * @return the execute stream handler to manage the input, output and error
     *         streams.
     *
     * @throws BuildException
     *             if the execute stream handler cannot be created.
     */
    public ExecuteStreamHandler createHandler() throws BuildException {
        createStreams();
        final boolean nonBlockingRead = input == null && inputString == null;
        return new PumpStreamHandler(getOutputStream(), getErrorStream(),
                getInputStream(), nonBlockingRead);
    }

    /**
     * Pass output sent to System.out to specified output.
     *
     * @param output
     *            the data to be output
     */
    protected void handleOutput(final String output) {
        synchronized (outMutex) {
            if (outPrintStream == null) {
                outPrintStream = new PrintStream(outputStream);
            }
            outPrintStream.print(output);
        }
    }

    /**
     * Handle an input request
     *
     * @param buffer
     *            the buffer into which data is to be read.
     * @param offset
     *            the offset into the buffer at which data is stored.
     * @param length
     *            the amount of data to read
     *
     * @return the number of bytes read
     *
     * @exception IOException
     *                if the data cannot be read
     */
    protected int handleInput(final byte[] buffer, final int offset, final int length)
            throws IOException {
        synchronized (inMutex) {
            if (inputStream == null) {
                return managingTask.getProject().defaultInput(buffer, offset,
                        length);
            }
            return inputStream.read(buffer, offset, length);

        }
    }

    /**
     * Process data due to a flush operation.
     *
     * @param output
     *            the data being flushed.
     */
    protected void handleFlush(final String output) {
        synchronized (outMutex) {
            if (outPrintStream == null) {
                outPrintStream = new PrintStream(outputStream);
            }
            outPrintStream.print(output);
            outPrintStream.flush();
        }
    }

    /**
     * Process error output
     *
     * @param output
     *            the error output data.
     */
    protected void handleErrorOutput(final String output) {
        synchronized (errMutex) {
            if (errorPrintStream == null) {
                errorPrintStream = new PrintStream(errorStream);
            }
            errorPrintStream.print(output);
        }
    }

    /**
     * Handle a flush operation on the error stream
     *
     * @param output
     *            the error information being flushed.
     */
    protected void handleErrorFlush(final String output) {
        synchronized (errMutex) {
            if (errorPrintStream == null) {
                errorPrintStream = new PrintStream(errorStream);
            }
            errorPrintStream.print(output);
            errorPrintStream.flush();
        }
    }

    /**
     * Get the output stream for the redirector
     *
     * @return the redirector's output stream or null if no output has been
     *         configured
     */
    public OutputStream getOutputStream() {
        synchronized (outMutex) {
            return outputStream;
        }
    }

    /**
     * Get the error stream for the redirector
     *
     * @return the redirector's error stream or null if no output has been
     *         configured
     */
    public OutputStream getErrorStream() {
        synchronized (errMutex) {
            return errorStream;
        }
    }

    /**
     * Get the input stream for the redirector
     *
     * @return the redirector's input stream or null if no output has been
     *         configured
     */
    public InputStream getInputStream() {
        synchronized (inMutex) {
            return inputStream;
        }
    }

    /**
     * Complete redirection.
     *
     * This operation will close any streams and create any specified property
     * values.
     *
     * @throws IOException
     *             if the output properties cannot be read from their output
     *             streams.
     */
    public void complete() throws IOException {
        System.out.flush();
        System.err.flush();

        synchronized (inMutex) {
            if (inputStream != null) {
                inputStream.close();
            }
        }

        synchronized (outMutex) {
            outputStream.flush();
            outputStream.close();
        }

        synchronized (errMutex) {
            errorStream.flush();
            errorStream.close();
        }

        // wait for the StreamPumpers to finish
        synchronized (this) {
            while (threadGroup.activeCount() > 0) {
                try {
                    managingTask.log("waiting for " + threadGroup.activeCount()
                            + " Threads:", Project.MSG_DEBUG);
                    final Thread[] thread = new Thread[threadGroup.activeCount()];
                    threadGroup.enumerate(thread);
                    for (int i = 0; i < thread.length && thread[i] != null; i++) {
                        try {
                            managingTask.log(thread[i].toString(),
                                    Project.MSG_DEBUG);
                        } catch (final NullPointerException enPeaEx) {
                            // Ignore exception
                        }
                    }
                    wait(STREAMPUMPER_WAIT_INTERVAL);
                } catch (final InterruptedException eyeEx) {
                    final Thread[] thread = new Thread[threadGroup.activeCount()];
                    threadGroup.enumerate(thread);
                    for (int i = 0; i < thread.length && thread[i] != null; i++) {
                        thread[i].interrupt();
                    }
                }
            }
        }

        setProperties();

        synchronized (inMutex) {
            inputStream = null;
        }
        synchronized (outMutex) {
            outputStream = null;
            outPrintStream = null;
        }
        synchronized (errMutex) {
            errorStream = null;
            errorPrintStream = null;
        }
    }

    /**
     * Notify the <code>Redirector</code> that it is now okay to set any output
     * and/or error properties.
     */
    public void setProperties() {
        synchronized (outMutex) {
            if (baos != null) {
                try {
                    baos.close();
                } catch (final IOException eyeOhEx) {
                    // Ignore exception
                }
            }
        }
        synchronized (errMutex) {
            if (errorBaos != null) {
                try {
                    errorBaos.close();
                } catch (final IOException eyeOhEx) {
                    // Ignore exception
                }
            }
        }
    }

    private OutputStream foldFiles(final File[] file, final String logHead, final int loglevel,
            final boolean append, final boolean createEmptyFiles) {
        final OutputStream result = new LazyFileOutputStream(file[0], append,
                createEmptyFiles);

        managingTask.log(logHead + file[0], loglevel);
        final char[] c = new char[logHead.length()];
        Arrays.fill(c, ' ');
        final String indent = new String(c);

        for (int i = 1; i < file.length; i++) {
            outputStream = new TeeOutputStream(outputStream,
                    new LazyFileOutputStream(file[i], append, createEmptyFiles));
            managingTask.log(indent + file[i], loglevel);
        }
        return result;
    }
}