1 18 19 package org.apache.tools.ant.util; 20 21 import java.io.File ; 22 import java.io.InputStream ; 23 import java.io.BufferedInputStream ; 24 import java.io.IOException ; 25 import java.io.FileInputStream ; 26 27 import org.apache.tools.ant.Project; 28 import org.apache.tools.ant.ProjectComponent; 29 import org.apache.tools.ant.Task; 30 31 35 public class ConcatFileInputStream extends InputStream { 36 37 private static final int EOF = -1; 38 private int currentIndex = -1; 39 private boolean eof = false; 40 private File [] file; 41 private InputStream currentStream; 42 private ProjectComponent managingPc; 43 44 50 public ConcatFileInputStream(File [] file) throws IOException { 51 this.file = file; 52 } 53 54 58 public void close() throws IOException { 59 closeCurrent(); 60 eof = true; 61 } 62 63 68 public int read() throws IOException { 69 int result = readCurrent(); 70 if (result == EOF && !eof) { 71 openFile(++currentIndex); 72 result = readCurrent(); 73 } 74 return result; 75 } 76 77 82 public void setManagingTask(Task task) { 83 setManagingComponent(task); 84 } 85 86 91 public void setManagingComponent(ProjectComponent pc) { 92 this.managingPc = pc; 93 } 94 95 100 public void log(String message, int loglevel) { 101 if (managingPc != null) { 102 managingPc.log(message, loglevel); 103 } else { 104 if (loglevel > Project.MSG_WARN) { 105 System.out.println(message); 106 } else { 107 System.err.println(message); 108 } 109 } 110 } 111 112 private int readCurrent() throws IOException { 113 return (eof || currentStream == null) ? EOF : currentStream.read(); 114 } 115 116 private void openFile(int index) throws IOException { 117 closeCurrent(); 118 if (file != null && index < file.length) { 119 log("Opening " + file[index], Project.MSG_VERBOSE); 120 try { 121 currentStream = new BufferedInputStream ( 122 new FileInputStream (file[index])); 123 } catch (IOException eyeOhEx) { 124 log("Failed to open " + file[index], Project.MSG_ERR); 125 throw eyeOhEx; 126 } 127 } else { 128 eof = true; 129 } 130 } 131 132 private void closeCurrent() { 133 FileUtils.close(currentStream); 134 currentStream = null; 135 } 136 } 137 | Popular Tags |