001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.util;
020:
021: import java.io.File;
022: import java.io.InputStream;
023: import java.io.BufferedInputStream;
024: import java.io.IOException;
025: import java.io.FileInputStream;
026:
027: import org.apache.tools.ant.Project;
028: import org.apache.tools.ant.ProjectComponent;
029: import org.apache.tools.ant.Task;
030:
031: /**
032: * Special <code>InputStream</code> that will
033: * concatenate the contents of an array of files.
034: */
035: public class ConcatFileInputStream extends InputStream {
036:
037: private static final int EOF = -1;
038: private int currentIndex = -1;
039: private boolean eof = false;
040: private File[] file;
041: private InputStream currentStream;
042: private ProjectComponent managingPc;
043:
044: /**
045: * Construct a new <code>ConcatFileInputStream</code>
046: * with the specified <code>File[]</code>.
047: * @param file <code>File[]</code>.
048: * @throws IOException if I/O errors occur.
049: */
050: public ConcatFileInputStream(File[] file) throws IOException {
051: this .file = file;
052: }
053:
054: /**
055: * Close the stream.
056: * @throws IOException if there is an error.
057: */
058: public void close() throws IOException {
059: closeCurrent();
060: eof = true;
061: }
062:
063: /**
064: * Read a byte.
065: * @return the byte (0 - 255) or -1 if this is the end of the stream.
066: * @throws IOException if there is an error.
067: */
068: public int read() throws IOException {
069: int result = readCurrent();
070: if (result == EOF && !eof) {
071: openFile(++currentIndex);
072: result = readCurrent();
073: }
074: return result;
075: }
076:
077: /**
078: * Set a managing <code>Task</code> for
079: * this <code>ConcatFileInputStream</code>.
080: * @param task the managing <code>Task</code>.
081: */
082: public void setManagingTask(Task task) {
083: setManagingComponent(task);
084: }
085:
086: /**
087: * Set a managing <code>Task</code> for
088: * this <code>ConcatFileInputStream</code>.
089: * @param pc the managing <code>Task</code>.
090: */
091: public void setManagingComponent(ProjectComponent pc) {
092: this .managingPc = pc;
093: }
094:
095: /**
096: * Log a message with the specified logging level.
097: * @param message the <code>String</code> message.
098: * @param loglevel the <code>int</code> logging level.
099: */
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
114: .read();
115: }
116:
117: private void openFile(int index) throws IOException {
118: closeCurrent();
119: if (file != null && index < file.length) {
120: log("Opening " + file[index], Project.MSG_VERBOSE);
121: try {
122: currentStream = new BufferedInputStream(
123: new FileInputStream(file[index]));
124: } catch (IOException eyeOhEx) {
125: log("Failed to open " + file[index], Project.MSG_ERR);
126: throw eyeOhEx;
127: }
128: } else {
129: eof = true;
130: }
131: }
132:
133: private void closeCurrent() {
134: FileUtils.close(currentStream);
135: currentStream = null;
136: }
137: }
|