source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/optional/perforce/P4OutputStream.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 2.5 KB
Line 
1/*
2 * Copyright 2003-2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.apache.tools.ant.taskdefs.optional.perforce;
19
20import java.io.OutputStream;
21import java.io.ByteArrayOutputStream;
22import java.io.IOException;
23
24/**
25 * heavily inspired from LogOutputStream
26 * this stream class calls back the P4Handler on each line of stdout or stderr read
27 */
28public class P4OutputStream extends OutputStream {
29 private P4Handler handler;
30 private ByteArrayOutputStream buffer = new ByteArrayOutputStream();
31 private boolean skip = false;
32
33 /**
34 * creates a new P4OutputStream for a P4Handler
35 * @param handler the handler which will process the streams
36 */
37 public P4OutputStream(P4Handler handler) {
38 this.handler = handler;
39 }
40
41 /**
42 * Write the data to the buffer and flush the buffer, if a line
43 * separator is detected.
44 *
45 * @param cc data to log (byte).
46 * @throws IOException IOException if an I/O error occurs. In particular,
47 * an <code>IOException</code> may be thrown if the
48 * output stream has been closed.
49 */
50 public void write(int cc) throws IOException {
51 final byte c = (byte) cc;
52 if ((c == '\n') || (c == '\r')) {
53 if (!skip) {
54 processBuffer();
55 }
56 } else {
57 buffer.write(cc);
58 }
59 skip = (c == '\r');
60 }
61
62
63 /**
64 * Converts the buffer to a string and sends it to <code>processLine</code>
65 */
66 protected void processBuffer() {
67 handler.process(buffer.toString());
68 buffer.reset();
69 }
70
71 /**
72 * Writes all remaining
73 * @throws IOException if an I/O error occurs.
74 */
75 public void close() throws IOException {
76 if (buffer.size() > 0) {
77 processBuffer();
78 }
79 super.close();
80 }
81
82}
83
84
Note: See TracBrowser for help on using the repository browser.