You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

DemuxOutputStream.java 8.0 kB

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