1 18 19 package org.apache.tools.ant; 20 21 import java.io.ByteArrayOutputStream ; 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 import java.util.WeakHashMap ; 25 26 33 public class DemuxOutputStream extends OutputStream { 34 35 39 private static class BufferInfo { 40 43 private ByteArrayOutputStream buffer; 44 45 49 private boolean crSeen = false; 50 } 51 52 53 private static final int MAX_SIZE = 1024; 54 55 56 private static final int INTIAL_SIZE = 132; 57 58 59 private static final int CR = 0x0d; 60 61 62 private static final int LF = 0x0a; 63 64 65 private WeakHashMap buffers = new WeakHashMap (); 66 67 70 private Project project; 71 72 75 private boolean isErrorStream; 76 77 87 public DemuxOutputStream(Project project, boolean isErrorStream) { 88 this.project = project; 89 this.isErrorStream = isErrorStream; 90 } 91 92 97 private BufferInfo getBufferInfo() { 98 Thread current = Thread.currentThread(); 99 BufferInfo bufferInfo = (BufferInfo) buffers.get(current); 100 if (bufferInfo == null) { 101 bufferInfo = new BufferInfo(); 102 bufferInfo.buffer = new ByteArrayOutputStream (INTIAL_SIZE); 103 bufferInfo.crSeen = false; 104 buffers.put(current, bufferInfo); 105 } 106 return bufferInfo; 107 } 108 109 112 private void resetBufferInfo() { 113 Thread current = Thread.currentThread(); 114 BufferInfo bufferInfo = (BufferInfo) buffers.get(current); 115 try { 116 bufferInfo.buffer.close(); 117 } catch (IOException e) { 118 } 120 bufferInfo.buffer = new ByteArrayOutputStream (); 121 bufferInfo.crSeen = false; 122 } 123 124 127 private void removeBuffer() { 128 Thread current = Thread.currentThread(); 129 buffers.remove (current); 130 } 131 132 139 public void write(int cc) throws IOException { 140 final byte c = (byte) cc; 141 142 BufferInfo bufferInfo = getBufferInfo(); 143 144 if (c == '\n') { 145 bufferInfo.buffer.write(cc); 147 processBuffer(bufferInfo.buffer); 148 } else { 149 if (bufferInfo.crSeen) { 150 processBuffer(bufferInfo.buffer); 152 } 153 bufferInfo.buffer.write(cc); 155 } 156 bufferInfo.crSeen = (c == '\r'); 157 if (!bufferInfo.crSeen && bufferInfo.buffer.size() > MAX_SIZE) { 158 processBuffer(bufferInfo.buffer); 159 } 160 } 161 162 170 protected void processBuffer(ByteArrayOutputStream buffer) { 171 String output = buffer.toString(); 172 project.demuxOutput(output, isErrorStream); 173 resetBufferInfo(); 174 } 175 176 184 protected void processFlush(ByteArrayOutputStream buffer) { 185 String output = buffer.toString(); 186 project.demuxFlush(output, isErrorStream); 187 resetBufferInfo(); 188 } 189 190 197 public void close() throws IOException { 198 flush(); 199 removeBuffer(); 200 } 201 202 208 public void flush() throws IOException { 209 BufferInfo bufferInfo = getBufferInfo(); 210 if (bufferInfo.buffer.size() > 0) { 211 processFlush(bufferInfo.buffer); 212 } 213 } 214 215 224 public void write(byte[] b, int off, int len) throws IOException { 225 int offset = off; 227 int blockStartOffset = offset; 228 int remaining = len; 229 BufferInfo bufferInfo = getBufferInfo(); 230 while (remaining > 0) { 231 while (remaining > 0 && b[offset] != LF && b[offset] != CR) { 232 offset++; 233 remaining--; 234 } 235 int blockLength = offset - blockStartOffset; 237 if (blockLength > 0) { 238 bufferInfo.buffer.write(b, blockStartOffset, blockLength); 239 } 240 while (remaining > 0 && (b[offset] == LF || b[offset] == CR)) { 241 write(b[offset]); 242 offset++; 243 remaining--; 244 } 245 blockStartOffset = offset; 246 } 247 } 248 } 249 | Popular Tags |