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   * <br>
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          @Override
56          public void write( byte[] buf, int off, int len )
57          {
58              // Note: At this point the supplied "buf" instance is reused, which means
59              // data must be copied out of the buffer
60              target.writeTestOutput( buf, off, len, isStdout );
61          }
62  
63          @Override
64          public void write( byte[] b )
65              throws IOException
66          {
67              target.writeTestOutput( b, 0, b.length, isStdout );
68          }
69  
70          @Override
71          public void write( int b )
72          {
73              byte[] buf = new byte[1];
74              buf[0] = (byte) b;
75              try
76              {
77                  write( buf );
78              }
79              catch ( IOException e )
80              {
81                  setError();
82              }
83          }
84  
85          @Override
86          public void println( String s )
87          {
88              if ( s == null )
89              {
90                  s = "null"; // Shamelessly taken from super.print
91              }
92              final byte[] bytes = ( s + NL ).getBytes();
93              target.writeTestOutput( bytes, 0, bytes.length, isStdout );
94          }
95  
96          @Override
97          public void close()
98          {
99          }
100 
101         @Override
102         public void flush()
103         {
104         }
105     }
106 
107 }