1 | |
package org.apache.maven.shared.release.exec; |
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.FilterOutputStream; |
24 | |
import java.io.IOException; |
25 | |
import java.io.OutputStream; |
26 | |
|
27 | |
public class TeeOutputStream |
28 | |
extends FilterOutputStream |
29 | |
{ |
30 | 22 | private ByteArrayOutputStream bout = new ByteArrayOutputStream( 1024 * 8 ); |
31 | |
private byte indent[]; |
32 | 22 | private int last = '\n'; |
33 | |
|
34 | |
public TeeOutputStream( OutputStream out ) |
35 | |
{ |
36 | 20 | this( out, " " ); |
37 | 20 | } |
38 | |
|
39 | |
public TeeOutputStream( OutputStream out, String i ) |
40 | |
{ |
41 | 22 | super( out ); |
42 | 22 | indent = i.getBytes(); |
43 | 22 | } |
44 | |
|
45 | |
public void write( byte[] b, int off, int len ) |
46 | |
throws IOException |
47 | |
{ |
48 | 198 | for ( int x = 0; x < len; x++ ) |
49 | |
{ |
50 | 52 | int c = b[off + x]; |
51 | 52 | if ( last == '\n' || ( last == '\r' && c != '\n' ) ) |
52 | |
{ |
53 | 6 | out.write( b, off, x ); |
54 | 6 | bout.write( b, off, x ); |
55 | 6 | out.write( indent ); |
56 | 6 | off += x; |
57 | 6 | len -= x; |
58 | 6 | x = 0; |
59 | |
} |
60 | 52 | last = c; |
61 | |
} |
62 | 146 | out.write( b, off, len ); |
63 | 146 | bout.write( b, off, len ); |
64 | 146 | } |
65 | |
|
66 | |
public void write( int b ) |
67 | |
throws IOException |
68 | |
{ |
69 | 0 | if ( last == '\n' || ( last == '\r' && b != '\n' ) ) |
70 | |
{ |
71 | 0 | out.write( indent ); |
72 | |
} |
73 | 0 | out.write( b ); |
74 | 0 | bout.write( b ); |
75 | 0 | last = b; |
76 | 0 | } |
77 | |
|
78 | |
public String toString() |
79 | |
{ |
80 | 20 | return bout.toString(); |
81 | |
} |
82 | |
|
83 | |
public String getContent() |
84 | |
{ |
85 | 2 | return bout.toString(); |
86 | |
} |
87 | |
|
88 | |
} |