source: release-kits/lirk3/resources/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/LogOutputStream.java@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 4.9 KB
Line 
1/*
2 * Copyright 2000-2005 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;
19
20import java.io.ByteArrayOutputStream;
21import java.io.IOException;
22import java.io.OutputStream;
23import org.apache.tools.ant.Project;
24import org.apache.tools.ant.ProjectComponent;
25import org.apache.tools.ant.Task;
26
27
28/**
29 * Logs each line written to this stream to the log system of ant.
30 *
31 * Tries to be smart about line separators.<br>
32 * TODO: This class can be split to implement other line based processing
33 * of data written to the stream.
34 *
35 * @since Ant 1.2
36 */
37public class LogOutputStream extends OutputStream {
38
39 /** Initial buffer size. */
40 private static final int INTIAL_SIZE = 132;
41
42 /** Carriage return */
43 private static final int CR = 0x0d;
44
45 /** Linefeed */
46 private static final int LF = 0x0a;
47
48 private ByteArrayOutputStream buffer
49 = new ByteArrayOutputStream(INTIAL_SIZE);
50 private boolean skip = false;
51
52 private ProjectComponent pc;
53 private int level = Project.MSG_INFO;
54
55 /**
56 * Creates a new instance of this class.
57 *
58 * @param task the task for whom to log
59 * @param level loglevel used to log data written to this stream.
60 */
61 public LogOutputStream(Task task, int level) {
62 this((ProjectComponent) task, level);
63 }
64
65 /**
66 * Creates a new instance of this class.
67 *
68 * @param task the task for whom to log
69 * @param level loglevel used to log data written to this stream.
70 * @since Ant 1.6.3
71 */
72 public LogOutputStream(ProjectComponent pc, int level) {
73 this.pc = pc;
74 this.level = level;
75 }
76
77
78 /**
79 * Write the data to the buffer and flush the buffer, if a line
80 * separator is detected.
81 *
82 * @param cc data to log (byte).
83 */
84 public void write(int cc) throws IOException {
85 final byte c = (byte) cc;
86 if ((c == '\n') || (c == '\r')) {
87 if (!skip) {
88 processBuffer();
89 }
90 } else {
91 buffer.write(cc);
92 }
93 skip = (c == '\r');
94 }
95
96 /**
97 * Flush this log stream
98 */
99 public void flush() {
100 if (buffer.size() > 0) {
101 processBuffer();
102 }
103 }
104
105
106 /**
107 * Converts the buffer to a string and sends it to <code>processLine</code>
108 */
109 protected void processBuffer() {
110 processLine(buffer.toString());
111 buffer.reset();
112 }
113
114 /**
115 * Logs a line to the log system of ant.
116 *
117 * @param line the line to log.
118 */
119 protected void processLine(String line) {
120 processLine(line, level);
121 }
122
123 /**
124 * Logs a line to the log system of ant.
125 *
126 * @param line the line to log.
127 */
128 protected void processLine(String line, int level) {
129 pc.log(line, level);
130 }
131
132
133 /**
134 * Writes all remaining
135 */
136 public void close() throws IOException {
137 if (buffer.size() > 0) {
138 processBuffer();
139 }
140 super.close();
141 }
142
143 public int getMessageLevel() {
144 return level;
145 }
146
147 /**
148 * Write a block of characters to the output stream
149 *
150 * @param b the array containing the data
151 * @param off the offset into the array where data starts
152 * @param len the length of block
153 *
154 * @throws IOException if the data cannot be written into the stream.
155 */
156 public void write(byte[] b, int off, int len) throws IOException {
157 // find the line breaks and pass other chars through in blocks
158 int offset = off;
159 int blockStartOffset = offset;
160 int remaining = len;
161 while (remaining > 0) {
162 while (remaining > 0 && b[offset] != LF && b[offset] != CR) {
163 offset++;
164 remaining--;
165 }
166 // either end of buffer or a line separator char
167 int blockLength = offset - blockStartOffset;
168 if (blockLength > 0) {
169 buffer.write(b, blockStartOffset, blockLength);
170 }
171 while (remaining > 0 && (b[offset] == LF || b[offset] == CR)) {
172 write(b[offset]);
173 offset++;
174 remaining--;
175 }
176 blockStartOffset = offset;
177 }
178 }
179
180}
Note: See TracBrowser for help on using the repository browser.