1 18 19 package org.apache.tools.ant.util; 20 21 import java.io.IOException ; 22 import java.io.PipedInputStream ; 23 import java.io.PipedOutputStream ; 24 25 import org.apache.tools.ant.ProjectComponent; 26 import org.apache.tools.ant.Task; 27 import org.apache.tools.ant.Project; 28 29 34 public class LeadPipeInputStream extends PipedInputStream { 35 private ProjectComponent managingPc; 36 37 40 public LeadPipeInputStream() { 41 super(); 42 } 43 44 49 public LeadPipeInputStream(int size) { 50 super(); 51 setBufferSize(size); 52 } 53 54 60 public LeadPipeInputStream(PipedOutputStream src) throws IOException { 61 super(src); 62 } 63 64 72 public LeadPipeInputStream(PipedOutputStream src, int size) throws IOException { 73 super(src); 74 setBufferSize(size); 75 } 76 77 83 public synchronized int read() throws IOException { 84 int result = -1; 85 try { 86 result = super.read(); 87 } catch (IOException eyeOhEx) { 88 if ("write end dead".equalsIgnoreCase(eyeOhEx.getMessage())) { 89 if (super.in > 0 && super.out < super.buffer.length 90 && super.out > super.in) { 91 result = super.buffer[super.out++] & 0xFF; 92 } 93 } else { 94 log("error at LeadPipeInputStream.read(): " 95 + eyeOhEx.getMessage(), Project.MSG_INFO); 96 } 97 } 98 return result; 99 } 100 101 105 public synchronized void setBufferSize(int size) { 106 if (size > buffer.length) { 107 byte[] newBuffer = new byte[size]; 108 if (in >= 0) { 109 if (in > out) { 110 System.arraycopy(buffer, out, newBuffer, out, in - out); 111 } else { 112 int outlen = buffer.length - out; 113 System.arraycopy(buffer, out, newBuffer, 0, outlen); 114 System.arraycopy(buffer, 0, newBuffer, outlen, in); 115 in += outlen; 116 out = 0; 117 } 118 } 119 buffer = newBuffer; 120 } 121 } 122 123 128 public void setManagingTask(Task task) { 129 setManagingComponent(task); 130 } 131 132 137 public void setManagingComponent(ProjectComponent pc) { 138 this.managingPc = pc; 139 } 140 141 146 public void log(String message, int loglevel) { 147 if (managingPc != null) { 148 managingPc.log(message, loglevel); 149 } else { 150 if (loglevel > Project.MSG_WARN) { 151 System.out.println(message); 152 } else { 153 System.err.println(message); 154 } 155 } 156 } 157 } 158 159 | Popular Tags |