1 package org.apache.maven.shared.invoker;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.PrintStream;
23
24 /**
25 * Offers an output handler that writes to a print stream like {@link System#out}.
26 *
27 * @version $Id: PrintStreamHandler.java 681956 2008-08-02 11:43:44Z dennisl $
28 * @since 2.0.9
29 */
30 public class PrintStreamHandler
31 implements InvocationOutputHandler
32 {
33
34 /**
35 * The print stream to write to, never <code>null</code>.
36 */
37 private PrintStream out;
38
39 /**
40 * A flag whether the print stream should be flushed after each line.
41 */
42 private boolean alwaysFlush;
43
44 /**
45 * Creates a new output handler that writes to {@link System#out}.
46 */
47 public PrintStreamHandler()
48 {
49 this( System.out, false );
50 }
51
52 /**
53 * Creates a new output handler that writes to the specified print stream.
54 *
55 * @param out The print stream to write to, must not be <code>null</code>.
56 * @param alwaysFlush A flag whether the print stream should be flushed after each line.
57 */
58 public PrintStreamHandler( PrintStream out, boolean alwaysFlush )
59 {
60 if ( out == null )
61 {
62 throw new NullPointerException( "missing output stream" );
63 }
64 this.out = out;
65 this.alwaysFlush = alwaysFlush;
66 }
67
68 public void consumeLine( String line )
69 {
70 if ( line == null )
71 {
72 out.println();
73 }
74 else
75 {
76 out.println( line );
77 }
78
79 if ( alwaysFlush )
80 {
81 out.flush();
82 }
83 }
84
85 }