View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.release.exec;
20  
21  import java.io.PrintStream;
22  
23  import org.codehaus.plexus.util.cli.StreamConsumer;
24  
25  /**
26   * Consumer that both funnels to System.out/err, and stores in an internal buffer.
27   *
28   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
29   */
30  public class TeeConsumer implements StreamConsumer {
31      private final PrintStream stream;
32  
33      /**
34       * @noinspection StringBufferField
35       */
36      private final StringBuffer content = new StringBuffer();
37  
38      private static final String LS = System.getProperty("line.separator");
39  
40      private final String indent;
41  
42      /**
43       * <p>Constructor for TeeConsumer.</p>
44       *
45       * @param stream a {@link java.io.PrintStream} object
46       */
47      public TeeConsumer(PrintStream stream) {
48          this(stream, "    ");
49      }
50  
51      /**
52       * <p>Constructor for TeeConsumer.</p>
53       *
54       * @param stream a {@link java.io.PrintStream} object
55       * @param indent a {@link java.lang.String} object
56       */
57      public TeeConsumer(PrintStream stream, String indent) {
58          this.stream = stream;
59          this.indent = indent;
60      }
61  
62      @Override
63      public void consumeLine(String line) {
64          stream.println(indent + line);
65  
66          content.append(line);
67          content.append(LS);
68      }
69  
70      /**
71       * <p>Getter for the field <code>content</code>.</p>
72       *
73       * @return a {@link java.lang.String} object
74       */
75      public String getContent() {
76          return content.toString();
77      }
78  
79      @Override
80      public String toString() {
81          return getContent();
82      }
83  }