[14627] | 1 | /*
|
---|
| 2 | * Copyright 2004-2005 The Apache Software Foundation
|
---|
| 3 | *
|
---|
| 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
| 5 | * you may not use this file except in compliance with the License.
|
---|
| 6 | * You may obtain a copy of the License at
|
---|
| 7 | *
|
---|
| 8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 9 | *
|
---|
| 10 | * Unless required by applicable law or agreed to in writing, software
|
---|
| 11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
| 13 | * See the License for the specific language governing permissions and
|
---|
| 14 | * limitations under the License.
|
---|
| 15 | *
|
---|
| 16 | */
|
---|
| 17 |
|
---|
| 18 | package org.apache.tools.ant.util;
|
---|
| 19 |
|
---|
| 20 | import java.io.File;
|
---|
| 21 | import java.io.InputStream;
|
---|
| 22 | import java.io.BufferedInputStream;
|
---|
| 23 | import java.io.IOException;
|
---|
| 24 | import java.io.FileInputStream;
|
---|
| 25 |
|
---|
| 26 | import org.apache.tools.ant.Project;
|
---|
| 27 | import org.apache.tools.ant.ProjectComponent;
|
---|
| 28 | import org.apache.tools.ant.Task;
|
---|
| 29 |
|
---|
| 30 | /**
|
---|
| 31 | * Special <code>InputStream</code> that will
|
---|
| 32 | * concatenate the contents of an array of files.
|
---|
| 33 | */
|
---|
| 34 | public class ConcatFileInputStream extends InputStream {
|
---|
| 35 |
|
---|
| 36 | private static final int EOF = -1;
|
---|
| 37 | private int currentIndex = -1;
|
---|
| 38 | private boolean eof = false;
|
---|
| 39 | private File[] file;
|
---|
| 40 | private InputStream currentStream;
|
---|
| 41 | private ProjectComponent managingPc;
|
---|
| 42 |
|
---|
| 43 | /**
|
---|
| 44 | * Construct a new <code>ConcatFileInputStream</code>
|
---|
| 45 | * with the specified <code>File[]</code>.
|
---|
| 46 | * @param file <code>File[]</code>.
|
---|
| 47 | * @throws IOException if I/O errors occur.
|
---|
| 48 | */
|
---|
| 49 | public ConcatFileInputStream(File[] file) throws IOException {
|
---|
| 50 | this.file = file;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | // inherit doc
|
---|
| 54 | public void close() throws IOException {
|
---|
| 55 | closeCurrent();
|
---|
| 56 | eof = true;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | // inherit doc
|
---|
| 60 | public int read() throws IOException {
|
---|
| 61 | int result = readCurrent();
|
---|
| 62 | if (result == EOF && !eof) {
|
---|
| 63 | openFile(++currentIndex);
|
---|
| 64 | result = readCurrent();
|
---|
| 65 | }
|
---|
| 66 | return result;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | /**
|
---|
| 70 | * Set a managing <code>Task</code> for
|
---|
| 71 | * this <code>ConcatFileInputStream</code>.
|
---|
| 72 | * @param task the managing <code>Task</code>.
|
---|
| 73 | */
|
---|
| 74 | public void setManagingTask(Task task) {
|
---|
| 75 | setManagingComponent(task);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | /**
|
---|
| 79 | * Set a managing <code>Task</code> for
|
---|
| 80 | * this <code>ConcatFileInputStream</code>.
|
---|
| 81 | * @param task the managing <code>Task</code>.
|
---|
| 82 | */
|
---|
| 83 | public void setManagingComponent(ProjectComponent pc) {
|
---|
| 84 | this.managingPc = pc;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | /**
|
---|
| 88 | * Log a message with the specified logging level.
|
---|
| 89 | * @param message the <code>String</code> message.
|
---|
| 90 | * @param loglevel the <code>int</code> logging level.
|
---|
| 91 | */
|
---|
| 92 | public void log(String message, int loglevel) {
|
---|
| 93 | if (managingPc != null) {
|
---|
| 94 | managingPc.log(message, loglevel);
|
---|
| 95 | } else {
|
---|
| 96 | if (loglevel > Project.MSG_WARN) {
|
---|
| 97 | System.out.println(message);
|
---|
| 98 | } else {
|
---|
| 99 | System.err.println(message);
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | private int readCurrent() throws IOException {
|
---|
| 105 | return (eof || currentStream == null) ? EOF : currentStream.read();
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | private void openFile(int index) throws IOException {
|
---|
| 109 | closeCurrent();
|
---|
| 110 | if (file != null && index < file.length) {
|
---|
| 111 | log("Opening " + file[index], Project.MSG_VERBOSE);
|
---|
| 112 | try {
|
---|
| 113 | currentStream = new BufferedInputStream(
|
---|
| 114 | new FileInputStream(file[index]));
|
---|
| 115 | } catch (IOException eyeOhEx) {
|
---|
| 116 | log("Failed to open " + file[index], Project.MSG_ERR);
|
---|
| 117 | throw eyeOhEx;
|
---|
| 118 | }
|
---|
| 119 | } else {
|
---|
| 120 | eof = true;
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | private void closeCurrent() {
|
---|
| 125 | FileUtils.close(currentStream);
|
---|
| 126 | currentStream = null;
|
---|
| 127 | }
|
---|
| 128 | }
|
---|