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      @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  }