KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > BUnzip2


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.taskdefs;
20
21
22 import java.io.BufferedInputStream JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.util.FileUtils;
28 import org.apache.tools.bzip2.CBZip2InputStream;
29
30 /**
31  * Expands a file that has been compressed with the BZIP2
32  * algorithm. Normally used to compress non-compressed archives such
33  * as TAR files.
34  *
35  * @since Ant 1.5
36  *
37  * @ant.task category="packaging"
38  */

39
40 public class BUnzip2 extends Unpack {
41
42     private static final String JavaDoc DEFAULT_EXTENSION = ".bz2";
43
44     /**
45      * Get the default extension.
46      * @return the string ".bz2"
47      */

48     protected String JavaDoc getDefaultExtension() {
49         return DEFAULT_EXTENSION;
50     }
51
52     /**
53      * Do the unbzipping.
54      */

55     protected void extract() {
56         if (source.lastModified() > dest.lastModified()) {
57             log("Expanding " + source.getAbsolutePath() + " to "
58                 + dest.getAbsolutePath());
59
60             FileOutputStream JavaDoc out = null;
61             CBZip2InputStream zIn = null;
62             InputStream JavaDoc fis = null;
63             BufferedInputStream JavaDoc bis = null;
64             try {
65                 out = new FileOutputStream JavaDoc(dest);
66                 fis = srcResource.getInputStream();
67                 bis = new BufferedInputStream JavaDoc(fis);
68                 int b = bis.read();
69                 if (b != 'B') {
70                     throw new BuildException("Invalid bz2 file.", getLocation());
71                 }
72                 b = bis.read();
73                 if (b != 'Z') {
74                     throw new BuildException("Invalid bz2 file.", getLocation());
75                 }
76                 zIn = new CBZip2InputStream(bis);
77                 byte[] buffer = new byte[8 * 1024];
78                 int count = 0;
79                 do {
80                     out.write(buffer, 0, count);
81                     count = zIn.read(buffer, 0, buffer.length);
82                 } while (count != -1);
83             } catch (IOException JavaDoc ioe) {
84                 String JavaDoc msg = "Problem expanding bzip2 " + ioe.getMessage();
85                 throw new BuildException(msg, ioe, getLocation());
86             } finally {
87                 FileUtils.close(bis);
88                 FileUtils.close(fis);
89                 FileUtils.close(out);
90                 FileUtils.close(zIn);
91             }
92         }
93     }
94
95     /**
96      * Whether this task can deal with non-file resources.
97      *
98      * <p>This implementation returns true only if this task is
99      * &lt;gunzip&gt;. Any subclass of this class that also wants to
100      * support non-file resources needs to override this method. We
101      * need to do so for backwards compatibility reasons since we
102      * can't expect subclasses to support resources.</p>
103      * @return true if this class supports non file resources.
104      * @since Ant 1.7
105      */

106     protected boolean supportsNonFileResources() {
107         return getClass().equals(BUnzip2.class);
108     }
109 }
110
Popular Tags