KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > resources > CompressedResource


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 package org.apache.tools.ant.types.resources;
19
20 import java.io.InputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23
24 import org.apache.tools.ant.BuildException;
25 import org.apache.tools.ant.types.Resource;
26 import org.apache.tools.ant.types.Reference;
27 import org.apache.tools.ant.types.ResourceCollection;
28 import org.apache.tools.ant.util.FileUtils;
29
30 /**
31  * A compressed resource.
32  *
33  * <p>Wraps around another resource, delegates all queries (except
34  * getSize) to that other resource but uncompresses/compresses streams
35  * on the fly.</p>
36  *
37  * @since Ant 1.7
38  */

39 public abstract class CompressedResource extends Resource {
40
41     private Resource resource;
42
43     /** no arg constructor */
44     public CompressedResource() {
45     }
46
47     /**
48      * Constructor with another resource to wrap.
49      * @param other the resource to wrap.
50      */

51     public CompressedResource(ResourceCollection other) {
52         addConfigured(other);
53     }
54
55     /**
56      * Sets the resource to wrap using a single-element collection.
57      * @param a the resource to wrap as a single element Resource collection.
58      */

59     public void addConfigured(ResourceCollection a) {
60         checkChildrenAllowed();
61         if (resource != null) {
62             throw new BuildException("you must not specify more than one"
63                                      + " resource");
64         }
65         if (a.size() != 1) {
66             throw new BuildException("only single argument resource collections"
67                                      + " are supported");
68         }
69         resource = (Resource) a.iterator().next();
70     }
71
72     /**
73      * Get the name of the resource.
74      * @return the name of the wrapped resource.
75      */

76     public String JavaDoc getName() {
77         return getResource().getName();
78     }
79
80
81     /**
82      * Overridden, not allowed to set the name of the resource.
83      * @param name not used.
84      * @throws BuildException always.
85      */

86     public void setName(String JavaDoc name) throws BuildException {
87         throw new BuildException("you can't change the name of a compressed"
88                                  + " resource");
89     }
90
91     /**
92      * The exists attribute tells whether a file exists.
93      * @return true if this resource exists.
94      */

95     public boolean isExists() {
96         return getResource().isExists();
97     }
98
99     /**
100      * Set the exists attribute.
101      * @param exists if true, this resource exists.
102      */

103     public void setExists(boolean exists) {
104         throw new BuildException("you can't change the exists state of a "
105                                  + " compressed resource");
106     }
107
108     /**
109      * Tells the modification time in milliseconds since 01.01.1970 .
110      *
111      * @return 0 if the resource does not exist to mirror the behavior
112      * of {@link java.io.File File}.
113      */

114     public long getLastModified() {
115         return getResource().getLastModified();
116     }
117
118     /**
119      * Override setLastModified.
120      * @param lastmodified not used.
121      * @throws BuildException always.
122      */

123     public void setLastModified(long lastmodified) throws BuildException {
124         throw new BuildException("you can't change the timestamp of a "
125                                  + " compressed resource");
126     }
127
128     /**
129      * Tells if the resource is a directory.
130      * @return boolean flag indicating if the resource is a directory.
131      */

132     public boolean isDirectory() {
133         return getResource().isDirectory();
134     }
135
136     /**
137      * Override setDirectory.
138      * @param directory not used.
139      * @throws BuildException always.
140      */

141     public void setDirectory(boolean directory) throws BuildException {
142         throw new BuildException("you can't change the directory state of a "
143                                  + " compressed resource");
144     }
145
146     /**
147      * Get the size of this Resource.
148      * @return the size, as a long, 0 if the Resource does not exist (for
149      * compatibility with java.io.File), or UNKNOWN_SIZE if not known.
150      */

151     public long getSize() {
152         if (isExists()) {
153             InputStream JavaDoc in = null;
154             try {
155                 in = getInputStream();
156                 byte[] buf = new byte[8192];
157                 int size = 0;
158                 int readNow;
159                 while ((readNow = in.read(buf, 0, buf.length)) > 0) {
160                     size += readNow;
161                 }
162                 return size;
163             } catch (IOException JavaDoc ex) {
164                 throw new BuildException("caught exception while reading "
165                                          + getName(), ex);
166             } finally {
167                 FileUtils.close(in);
168             }
169         } else {
170             return 0;
171         }
172     }
173
174     /**
175      * Override setSize.
176      * @param size not used.
177      * @throws BuildException always.
178      */

179     public void setSize(long size) throws BuildException {
180         throw new BuildException("you can't change the size of a "
181                                  + " compressed resource");
182     }
183
184     /**
185      * Delegates to a comparison of names.
186      * @param other the object to compare to.
187      * @return a negative integer, zero, or a positive integer as this Resource
188      * is less than, equal to, or greater than the specified Resource.
189      */

190     public int compareTo(Object JavaDoc other) {
191         if (other == this) {
192             return 0;
193         }
194         if (other instanceof CompressedResource) {
195             return getResource().compareTo(
196                 ((CompressedResource) other).getResource());
197         }
198         return getResource().compareTo(other);
199     }
200
201     /**
202      * Get the hash code for this Resource.
203      * @return hash code as int.
204      */

205     public int hashCode() {
206         return getResource().hashCode();
207     }
208
209     /**
210      * Get an InputStream for the Resource.
211      * @return an InputStream containing this Resource's content.
212      * @throws IOException if unable to provide the content of this
213      * Resource as a stream.
214      * @throws UnsupportedOperationException if InputStreams are not
215      * supported for this Resource type.
216      */

217     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
218         InputStream JavaDoc in = getResource().getInputStream();
219         if (in != null) {
220             in = wrapStream(in);
221         }
222         return in;
223     }
224
225     /**
226      * Get an OutputStream for the Resource.
227      * @return an OutputStream to which content can be written.
228      * @throws IOException if unable to provide the content of this
229      * Resource as a stream.
230      * @throws UnsupportedOperationException if OutputStreams are not
231      * supported for this Resource type.
232      */

233     public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
234         OutputStream JavaDoc out = getResource().getOutputStream();
235         if (out != null) {
236             out = wrapStream(out);
237         }
238         return out;
239     }
240
241     /**
242      * Fulfill the ResourceCollection contract.
243      * @return whether this Resource is a FileResource.
244      */

245     public boolean isFilesystemOnly() {
246         return false;
247     }
248
249     /**
250      * Get the string representation of this Resource.
251      * @return this Resource formatted as a String.
252      * @since Ant 1.7
253      */

254     public String JavaDoc toString() {
255         return getCompressionName() + " compressed "
256             + getResource().toString();
257     }
258
259     /**
260      * Overrides the base version.
261      * @param r the Reference to set.
262      */

263     public void setRefid(Reference r) {
264         if (resource != null) {
265             throw noChildrenAllowed();
266         }
267         super.setRefid(r);
268     }
269
270     /**
271      * Is supposed to wrap the stream to allow decompression on the fly.
272      *
273      * @param in InputStream to wrap, will never be null.
274      * @return a compressed inputstream.
275      * @throws IOException if there is a problem.
276      */

277     protected abstract InputStream JavaDoc wrapStream(InputStream JavaDoc in)
278         throws IOException JavaDoc;
279
280     /**
281      * Is supposed to wrap the stream to allow compression on the fly.
282      *
283      * @param out OutputStream to wrap, will never be null.
284      * @return a compressed outputstream.
285      * @throws IOException if there is a problem.
286      */

287     protected abstract OutputStream JavaDoc wrapStream(OutputStream JavaDoc out)
288         throws IOException JavaDoc;
289
290     /**
291      * @return the name of the compression method.
292      */

293     protected abstract String JavaDoc getCompressionName();
294
295     private Resource getResource() {
296         if (isReference()) {
297             return (Resource) getCheckedRef();
298         } else if (resource == null) {
299             throw new BuildException("no resource specified");
300         }
301         return resource;
302     }
303
304 }
305
Popular Tags