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  /**
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      public void write( byte[] b, int off, int len )
49          throws IOException
50      {
51          for ( int x = 0; x < len; x++ )
52          {
53              int c = b[off + x];
54              if ( last == '\n' || ( last == '\r' && c != '\n' ) )
55              {
56                  out.write( b, off, x );
57                  bout.write( b, off, x );
58                  out.write( indent );
59                  off += x;
60                  len -= x;
61                  x = 0;
62              }
63              last = c;
64          }
65          out.write( b, off, len );
66          bout.write( b, off, len );
67      }
68  
69      public void write( int b )
70          throws IOException
71      {
72          if ( last == '\n' || ( last == '\r' && b != '\n' ) )
73          {
74              out.write( indent );
75          }
76          out.write( b );
77          bout.write( b );
78          last = b;
79      }
80      
81      public String toString() 
82      {
83          return bout.toString();
84      }
85  
86      public String getContent()
87      {
88          return bout.toString();
89      }
90  
91  }