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