KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > ConcatFileInputStream


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.util;
20
21 import java.io.File JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.BufferedInputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26
27 import org.apache.tools.ant.Project;
28 import org.apache.tools.ant.ProjectComponent;
29 import org.apache.tools.ant.Task;
30
31 /**
32  * Special <code>InputStream</code> that will
33  * concatenate the contents of an array of files.
34  */

35 public class ConcatFileInputStream extends InputStream JavaDoc {
36
37     private static final int EOF = -1;
38     private int currentIndex = -1;
39     private boolean eof = false;
40     private File JavaDoc[] file;
41     private InputStream JavaDoc currentStream;
42     private ProjectComponent managingPc;
43
44   /**
45    * Construct a new <code>ConcatFileInputStream</code>
46    * with the specified <code>File[]</code>.
47    * @param file <code>File[]</code>.
48    * @throws IOException if I/O errors occur.
49    */

50     public ConcatFileInputStream(File JavaDoc[] file) throws IOException JavaDoc {
51         this.file = file;
52     }
53
54     /**
55      * Close the stream.
56      * @throws IOException if there is an error.
57      */

58     public void close() throws IOException JavaDoc {
59         closeCurrent();
60         eof = true;
61     }
62
63     /**
64      * Read a byte.
65      * @return the byte (0 - 255) or -1 if this is the end of the stream.
66      * @throws IOException if there is an error.
67      */

68     public int read() throws IOException JavaDoc {
69         int result = readCurrent();
70         if (result == EOF && !eof) {
71             openFile(++currentIndex);
72             result = readCurrent();
73         }
74         return result;
75     }
76
77     /**
78      * Set a managing <code>Task</code> for
79      * this <code>ConcatFileInputStream</code>.
80      * @param task the managing <code>Task</code>.
81      */

82     public void setManagingTask(Task task) {
83         setManagingComponent(task);
84     }
85
86     /**
87      * Set a managing <code>Task</code> for
88      * this <code>ConcatFileInputStream</code>.
89      * @param pc the managing <code>Task</code>.
90      */

91     public void setManagingComponent(ProjectComponent pc) {
92         this.managingPc = pc;
93     }
94
95     /**
96      * Log a message with the specified logging level.
97      * @param message the <code>String</code> message.
98      * @param loglevel the <code>int</code> logging level.
99      */

100     public void log(String JavaDoc 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 JavaDoc {
113         return (eof || currentStream == null) ? EOF : currentStream.read();
114     }
115
116     private void openFile(int index) throws IOException JavaDoc {
117         closeCurrent();
118         if (file != null && index < file.length) {
119             log("Opening " + file[index], Project.MSG_VERBOSE);
120             try {
121                 currentStream = new BufferedInputStream JavaDoc(
122                     new FileInputStream JavaDoc(file[index]));
123             } catch (IOException JavaDoc 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