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