KickJava   Java API By Example, From Geeks To Geeks.

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


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.InputStream JavaDoc;
22 import java.io.BufferedInputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Iterator JavaDoc;
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 /**
32  * Special <code>InputStream</code> that will
33  * concatenate the contents of Resources from a single ResourceCollection.
34  * @since Ant 1.7
35  */

36 public class ConcatResourceInputStream extends InputStream JavaDoc {
37
38     private static final int EOF = -1;
39     private boolean eof = false;
40     private Iterator JavaDoc iter;
41     private InputStream JavaDoc currentStream;
42     private ProjectComponent managingPc;
43     private boolean ignoreErrors = false;
44
45   /**
46    * Construct a new ConcatResourceInputStream
47    * for the specified ResourceCollection.
48    * @param rc the ResourceCollection to combine.
49    */

50     public ConcatResourceInputStream(ResourceCollection rc) {
51         iter = rc.iterator();
52     }
53
54     /**
55      * Set whether this ConcatResourceInputStream ignores errors.
56      * @param b whether to ignore errors.
57      */

58     public void setIgnoreErrors(boolean b) {
59         ignoreErrors = b;
60     }
61
62     /**
63      * Find out whether this ConcatResourceInputStream ignores errors.
64      * @return boolean ignore-errors flag.
65      */

66     public boolean isIgnoreErrors() {
67         return ignoreErrors;
68     }
69
70     /**
71      * Close the stream.
72      * @throws IOException if there is an error.
73      */

74      public void close() throws IOException JavaDoc {
75         closeCurrent();
76         eof = true;
77     }
78
79     /**
80      * Read a byte.
81      * @return the byte (0 - 255) or -1 if this is the end of the stream.
82      * @throws IOException if there is an error.
83      */

84     public int read() throws IOException JavaDoc {
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     /**
97      * Set a managing <code>ProjectComponent</code> for
98      * this <code>ConcatResourceInputStream</code>.
99      * @param pc the managing <code>ProjectComponent</code>.
100      */

101     public void setManagingComponent(ProjectComponent pc) {
102         this.managingPc = pc;
103     }
104
105     /**
106      * Log a message with the specified logging level.
107      * @param message the <code>String</code> message.
108      * @param loglevel the <code>int</code> logging level.
109      */

110     public void log(String JavaDoc 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 JavaDoc {
119         return eof || currentStream == null ? EOF : currentStream.read();
120     }
121
122     private void nextResource() throws IOException JavaDoc {
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 JavaDoc(r.getInputStream());
132                 return;
133             } catch (IOException JavaDoc 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