View Javadoc
1   package org.apache.maven.surefire.report;
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.ByteArrayOutputStream;
23  import java.io.IOException;
24  import java.io.PrintStream;
25  
26  import static java.lang.System.setErr;
27  import static java.lang.System.setOut;
28  import static org.apache.maven.surefire.util.internal.StringUtils.NL;
29  
30  /**
31   * Deals with system.out/err.
32   * <p/>
33   */
34  public class ConsoleOutputCapture
35  {
36      public static void startCapture( ConsoleOutputReceiver target )
37      {
38          setOut( new ForwardingPrintStream( true, target ) );
39          setErr( new ForwardingPrintStream( false, target ) );
40      }
41  
42      private static class ForwardingPrintStream
43          extends PrintStream
44      {
45          private final boolean isStdout;
46          private final ConsoleOutputReceiver target;
47  
48          ForwardingPrintStream( boolean stdout, ConsoleOutputReceiver target )
49          {
50              super( new ByteArrayOutputStream() );
51              isStdout = stdout;
52              this.target = target;
53          }
54  
55          public void write( byte[] buf, int off, int len )
56          {
57              // Note: At this point the supplied "buf" instance is reused, which means
58              // data must be copied out of the buffer
59              target.writeTestOutput( buf, off, len, isStdout );
60          }
61  
62          public void write( byte[] b )
63              throws IOException
64          {
65              target.writeTestOutput( b, 0, b.length, isStdout );
66          }
67  
68          public void write( int b )
69          {
70              byte[] buf = new byte[1];
71              buf[0] = (byte) b;
72              try
73              {
74                  write( buf );
75              }
76              catch ( IOException e )
77              {
78                  setError();
79              }
80          }
81  
82          public void println( String s )
83          {
84              if ( s == null )
85              {
86                  s = "null"; // Shamelessly taken from super.print
87              }
88              final byte[] bytes = ( s + NL ).getBytes();
89              target.writeTestOutput( bytes, 0, bytes.length, isStdout );
90          }
91  
92          public void close()
93          {
94          }
95  
96          public void flush()
97          {
98          }
99      }
100 
101 }