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 | |
import org.apache.maven.surefire.util.internal.ByteBuffer; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | 0 | public class ConsoleOutputCapture |
32 | |
{ |
33 | |
public static void startCapture( ConsoleOutputReceiver target ) |
34 | |
{ |
35 | 0 | System.setOut( new ForwardingPrintStream( true, target ) ); |
36 | |
|
37 | 0 | System.setErr( new ForwardingPrintStream( false, target ) ); |
38 | 0 | } |
39 | |
|
40 | 0 | private static class ForwardingPrintStream |
41 | |
extends PrintStream |
42 | |
{ |
43 | |
private final boolean isStdout; |
44 | |
|
45 | |
private final ConsoleOutputReceiver target; |
46 | |
|
47 | |
ForwardingPrintStream( boolean stdout, ConsoleOutputReceiver target ) |
48 | |
{ |
49 | 0 | super( new ByteArrayOutputStream() ); |
50 | 0 | isStdout = stdout; |
51 | 0 | this.target = target; |
52 | 0 | } |
53 | |
|
54 | |
public void write( byte[] buf, int off, int len ) |
55 | |
{ |
56 | |
|
57 | |
|
58 | 0 | target.writeTestOutput( buf, off, len, isStdout ); |
59 | 0 | } |
60 | |
|
61 | |
public void write( byte[] b ) |
62 | |
throws IOException |
63 | |
{ |
64 | 0 | target.writeTestOutput( b, 0, b.length, isStdout ); |
65 | 0 | } |
66 | |
|
67 | |
public void write( int b ) |
68 | |
{ |
69 | 0 | byte[] buf = new byte[1]; |
70 | 0 | buf[0] = (byte) b; |
71 | |
try |
72 | |
{ |
73 | 0 | write( buf ); |
74 | |
} |
75 | 0 | catch ( IOException e ) |
76 | |
{ |
77 | 0 | setError(); |
78 | 0 | } |
79 | 0 | } |
80 | |
|
81 | 0 | static final byte[] newline = new byte[]{ (byte) '\n' }; |
82 | |
|
83 | |
public void println( String s ) |
84 | |
{ |
85 | 0 | if ( s == null ) |
86 | |
{ |
87 | 0 | s = "null"; |
88 | |
} |
89 | 0 | final byte[] bytes = s.getBytes(); |
90 | 0 | final byte[] join = ByteBuffer.join( bytes, 0, bytes.length, newline, 0, 1 ); |
91 | 0 | target.writeTestOutput( join, 0, join.length, isStdout ); |
92 | 0 | } |
93 | |
|
94 | |
public void close() |
95 | |
{ |
96 | 0 | } |
97 | |
|
98 | |
public void flush() |
99 | |
{ |
100 | 0 | } |
101 | |
} |
102 | |
|
103 | |
} |