View Javadoc
1   package org.apache.maven.shared.release.exec;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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      private ByteArrayOutputStream bout = new ByteArrayOutputStream( 1024 * 8 );
31      private byte indent[];
32      private int last = '\n';
33  
34      public TeeOutputStream( OutputStream out )
35      {
36          this( out, "    " );
37      }
38      
39      public TeeOutputStream( OutputStream out, String i )
40      {
41          super( out );
42          indent = i.getBytes();
43      }
44  
45      public void write( byte[] b, int off, int len )
46          throws IOException
47      {
48          for ( int x = 0; x < len; x++ )
49          {
50              int c = b[off + x];
51              if ( last == '\n' || ( last == '\r' && c != '\n' ) )
52              {
53                  out.write( b, off, x );
54                  bout.write( b, off, x );
55                  out.write( indent );
56                  off += x;
57                  len -= x;
58                  x = 0;
59              }
60              last = c;
61          }
62          out.write( b, off, len );
63          bout.write( b, off, len );
64      }
65  
66      public void write( int b )
67          throws IOException
68      {
69          if ( last == '\n' || ( last == '\r' && b != '\n' ) )
70          {
71              out.write( indent );
72          }
73          out.write( b );
74          bout.write( b );
75          last = b;
76      }
77      
78      public String toString() 
79      {
80          return bout.toString();
81      }
82  
83      public String getContent()
84      {
85          return bout.toString();
86      }
87  
88  }