1 package org.apache.maven.surefire.report;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.io.PrintStream;
25
26
27
28
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
56
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";
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 }