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