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