1 18 19 package org.apache.tools.ant.util; 20 21 import java.io.InputStream ; 22 import java.io.BufferedInputStream ; 23 import java.io.IOException ; 24 import java.util.Iterator ; 25 26 import org.apache.tools.ant.Project; 27 import org.apache.tools.ant.ProjectComponent; 28 import org.apache.tools.ant.types.Resource; 29 import org.apache.tools.ant.types.ResourceCollection; 30 31 36 public class ConcatResourceInputStream extends InputStream { 37 38 private static final int EOF = -1; 39 private boolean eof = false; 40 private Iterator iter; 41 private InputStream currentStream; 42 private ProjectComponent managingPc; 43 private boolean ignoreErrors = false; 44 45 50 public ConcatResourceInputStream(ResourceCollection rc) { 51 iter = rc.iterator(); 52 } 53 54 58 public void setIgnoreErrors(boolean b) { 59 ignoreErrors = b; 60 } 61 62 66 public boolean isIgnoreErrors() { 67 return ignoreErrors; 68 } 69 70 74 public void close() throws IOException { 75 closeCurrent(); 76 eof = true; 77 } 78 79 84 public int read() throws IOException { 85 if (eof) { 86 return EOF; 87 } 88 int result = readCurrent(); 89 if (result == EOF) { 90 nextResource(); 91 result = readCurrent(); 92 } 93 return result; 94 } 95 96 101 public void setManagingComponent(ProjectComponent pc) { 102 this.managingPc = pc; 103 } 104 105 110 public void log(String message, int loglevel) { 111 if (managingPc != null) { 112 managingPc.log(message, loglevel); 113 } else { 114 (loglevel > Project.MSG_WARN ? System.out : System.err).println(message); 115 } 116 } 117 118 private int readCurrent() throws IOException { 119 return eof || currentStream == null ? EOF : currentStream.read(); 120 } 121 122 private void nextResource() throws IOException { 123 closeCurrent(); 124 while (iter.hasNext()) { 125 Resource r = (Resource) iter.next(); 126 if (!r.isExists()) { 127 continue; 128 } 129 log("Concating " + r.toLongString(), Project.MSG_VERBOSE); 130 try { 131 currentStream = new BufferedInputStream (r.getInputStream()); 132 return; 133 } catch (IOException eyeOhEx) { 134 if (!ignoreErrors) { 135 log("Failed to get input stream for " + r, Project.MSG_ERR); 136 throw eyeOhEx; 137 } 138 } 139 } 140 eof = true; 141 } 142 143 private void closeCurrent() { 144 FileUtils.close(currentStream); 145 currentStream = null; 146 } 147 } 148 | Popular Tags |