1. /*
  2. * Copyright 2001-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. package org.apache.tools.ant;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import java.util.Hashtable;
  22. /**
  23. * Logs content written by a thread and forwards the buffers onto the
  24. * project object which will forward the content to the appropriate
  25. * task.
  26. *
  27. * @since 1.4
  28. */
  29. public class DemuxOutputStream extends OutputStream {
  30. /**
  31. * A data class to store information about a buffer. Such information
  32. * is stored on a per-thread basis.
  33. */
  34. private static class BufferInfo {
  35. /**
  36. * The per-thread output stream.
  37. */
  38. private ByteArrayOutputStream buffer;
  39. /**
  40. * Indicates we have just seen a carriage return. It may be part of
  41. * a crlf pair or a single cr invoking processBuffer twice.
  42. */
  43. private boolean crSeen = false;
  44. }
  45. /** Maximum buffer size. */
  46. private static final int MAX_SIZE = 1024;
  47. /** Initial buffer size. */
  48. private static final int INTIAL_SIZE = 132;
  49. /** Carriage return */
  50. private static final int CR = 0x0d;
  51. /** Linefeed */
  52. private static final int LF = 0x0a;
  53. /** Mapping from thread to buffer (Thread to BufferInfo). */
  54. private Hashtable buffers = new Hashtable();
  55. /**
  56. * The project to send output to.
  57. */
  58. private Project project;
  59. /**
  60. * Whether or not this stream represents an error stream.
  61. */
  62. private boolean isErrorStream;
  63. /**
  64. * Creates a new instance of this class.
  65. *
  66. * @param project The project instance for which output is being
  67. * demultiplexed. Must not be <code>null</code>.
  68. * @param isErrorStream <code>true</code> if this is the error string,
  69. * otherwise a normal output stream. This is
  70. * passed to the project so it knows
  71. * which stream it is receiving.
  72. */
  73. public DemuxOutputStream(Project project, boolean isErrorStream) {
  74. this.project = project;
  75. this.isErrorStream = isErrorStream;
  76. }
  77. /**
  78. * Returns the buffer associated with the current thread.
  79. *
  80. * @return a BufferInfo for the current thread to write data to
  81. */
  82. private BufferInfo getBufferInfo() {
  83. Thread current = Thread.currentThread();
  84. BufferInfo bufferInfo = (BufferInfo) buffers.get(current);
  85. if (bufferInfo == null) {
  86. bufferInfo = new BufferInfo();
  87. bufferInfo.buffer = new ByteArrayOutputStream(INTIAL_SIZE);
  88. bufferInfo.crSeen = false;
  89. buffers.put(current, bufferInfo);
  90. }
  91. return bufferInfo;
  92. }
  93. /**
  94. * Resets the buffer for the current thread.
  95. */
  96. private void resetBufferInfo() {
  97. Thread current = Thread.currentThread();
  98. BufferInfo bufferInfo = (BufferInfo) buffers.get(current);
  99. try {
  100. bufferInfo.buffer.close();
  101. } catch (IOException e) {
  102. // Shouldn't happen
  103. }
  104. bufferInfo.buffer = new ByteArrayOutputStream();
  105. bufferInfo.crSeen = false;
  106. }
  107. /**
  108. * Removes the buffer for the current thread.
  109. */
  110. private void removeBuffer() {
  111. Thread current = Thread.currentThread();
  112. buffers.remove (current);
  113. }
  114. /**
  115. * Writes the data to the buffer and flushes the buffer if a line
  116. * separator is detected or if the buffer has reached its maximum size.
  117. *
  118. * @param cc data to log (byte).
  119. * @exception IOException if the data cannot be written to the stream
  120. */
  121. public void write(int cc) throws IOException {
  122. final byte c = (byte) cc;
  123. BufferInfo bufferInfo = getBufferInfo();
  124. if (c == '\n') {
  125. // LF is always end of line (i.e. CRLF or single LF)
  126. bufferInfo.buffer.write(cc);
  127. processBuffer(bufferInfo.buffer);
  128. } else {
  129. if (bufferInfo.crSeen) {
  130. // CR without LF - send buffer then add char
  131. processBuffer(bufferInfo.buffer);
  132. }
  133. // add into buffer
  134. bufferInfo.buffer.write(cc);
  135. }
  136. bufferInfo.crSeen = (c == '\r');
  137. if (!bufferInfo.crSeen && bufferInfo.buffer.size() > MAX_SIZE) {
  138. processBuffer(bufferInfo.buffer);
  139. }
  140. }
  141. /**
  142. * Converts the buffer to a string and sends it to the project.
  143. *
  144. * @param buffer the ByteArrayOutputStream used to collect the output
  145. * until a line separator is seen.
  146. *
  147. * @see Project#demuxOutput(String,boolean)
  148. */
  149. protected void processBuffer(ByteArrayOutputStream buffer) {
  150. String output = buffer.toString();
  151. project.demuxOutput(output, isErrorStream);
  152. resetBufferInfo();
  153. }
  154. /**
  155. * Converts the buffer to a string and sends it to the project.
  156. *
  157. * @param buffer the ByteArrayOutputStream used to collect the output
  158. * until a line separator is seen.
  159. *
  160. * @see Project#demuxOutput(String,boolean)
  161. */
  162. protected void processFlush(ByteArrayOutputStream buffer) {
  163. String output = buffer.toString();
  164. project.demuxFlush(output, isErrorStream);
  165. resetBufferInfo();
  166. }
  167. /**
  168. * Equivalent to flushing the stream.
  169. *
  170. * @exception IOException if there is a problem closing the stream.
  171. *
  172. * @see #flush
  173. */
  174. public void close() throws IOException {
  175. flush();
  176. removeBuffer();
  177. }
  178. /**
  179. * Writes all remaining data in the buffer associated
  180. * with the current thread to the project.
  181. *
  182. * @exception IOException if there is a problem flushing the stream.
  183. */
  184. public void flush() throws IOException {
  185. BufferInfo bufferInfo = getBufferInfo();
  186. if (bufferInfo.buffer.size() > 0) {
  187. processFlush(bufferInfo.buffer);
  188. }
  189. }
  190. /**
  191. * Write a block of characters to the output stream
  192. *
  193. * @param b the array containing the data
  194. * @param off the offset into the array where data starts
  195. * @param len the length of block
  196. *
  197. * @throws IOException if the data cannot be written into the stream.
  198. */
  199. public void write(byte[] b, int off, int len) throws IOException {
  200. // find the line breaks and pass other chars through in blocks
  201. int offset = off;
  202. int blockStartOffset = offset;
  203. int remaining = len;
  204. BufferInfo bufferInfo = getBufferInfo();
  205. while (remaining > 0) {
  206. while (remaining > 0 && b[offset] != LF && b[offset] != CR) {
  207. offset++;
  208. remaining--;
  209. }
  210. // either end of buffer or a line separator char
  211. int blockLength = offset - blockStartOffset;
  212. if (blockLength > 0) {
  213. bufferInfo.buffer.write(b, blockStartOffset, blockLength);
  214. }
  215. while (remaining > 0 && (b[offset] == LF || b[offset] == CR)) {
  216. write(b[offset]);
  217. offset++;
  218. remaining--;
  219. }
  220. blockStartOffset = offset;
  221. }
  222. }
  223. }