001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs;
020:
021: import java.io.BufferedInputStream;
022: import java.io.FileOutputStream;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import org.apache.tools.ant.BuildException;
026: import org.apache.tools.ant.util.FileUtils;
027: import org.apache.tools.bzip2.CBZip2InputStream;
028:
029: /**
030: * Expands a file that has been compressed with the BZIP2
031: * algorithm. Normally used to compress non-compressed archives such
032: * as TAR files.
033: *
034: * @since Ant 1.5
035: *
036: * @ant.task category="packaging"
037: */
038:
039: public class BUnzip2 extends Unpack {
040:
041: private static final String DEFAULT_EXTENSION = ".bz2";
042:
043: /**
044: * Get the default extension.
045: * @return the string ".bz2"
046: */
047: protected String getDefaultExtension() {
048: return DEFAULT_EXTENSION;
049: }
050:
051: /**
052: * Do the unbzipping.
053: */
054: protected void extract() {
055: if (source.lastModified() > dest.lastModified()) {
056: log("Expanding " + source.getAbsolutePath() + " to "
057: + dest.getAbsolutePath());
058:
059: FileOutputStream out = null;
060: CBZip2InputStream zIn = null;
061: InputStream fis = null;
062: BufferedInputStream bis = null;
063: try {
064: out = new FileOutputStream(dest);
065: fis = srcResource.getInputStream();
066: bis = new BufferedInputStream(fis);
067: int b = bis.read();
068: if (b != 'B') {
069: throw new BuildException("Invalid bz2 file.",
070: getLocation());
071: }
072: b = bis.read();
073: if (b != 'Z') {
074: throw new BuildException("Invalid bz2 file.",
075: getLocation());
076: }
077: zIn = new CBZip2InputStream(bis);
078: byte[] buffer = new byte[8 * 1024];
079: int count = 0;
080: do {
081: out.write(buffer, 0, count);
082: count = zIn.read(buffer, 0, buffer.length);
083: } while (count != -1);
084: } catch (IOException ioe) {
085: String msg = "Problem expanding bzip2 "
086: + ioe.getMessage();
087: throw new BuildException(msg, ioe, getLocation());
088: } finally {
089: FileUtils.close(bis);
090: FileUtils.close(fis);
091: FileUtils.close(out);
092: FileUtils.close(zIn);
093: }
094: }
095: }
096:
097: /**
098: * Whether this task can deal with non-file resources.
099: *
100: * <p>This implementation returns true only if this task is
101: * <gunzip>. Any subclass of this class that also wants to
102: * support non-file resources needs to override this method. We
103: * need to do so for backwards compatibility reasons since we
104: * can't expect subclasses to support resources.</p>
105: * @return true if this class supports non file resources.
106: * @since Ant 1.7
107: */
108: protected boolean supportsNonFileResources() {
109: return getClass().equals(BUnzip2.class);
110: }
111: }
|