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