package atg.tools.ant.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.jar.Manifest;

/**
 * Utility class for dealing with JAR manifest files.
 *
 * @author msicker
 * @version 2.0
 */
public final class ManifestUtils {

    public static final String META_INF = "META-INF";
    public static final String MANIFEST_MF = "MANIFEST.MF";

    private ManifestUtils() {
    }

    /**
     * Gets a File reference to the {@code META-INF/MANIFEST.MF} file within the given directory.
     *
     * @param baseDirectory directory to locate manifest file within.
     *
     * @return manifest file object (which may or may not exist).
     *
     * @throws java.lang.IllegalArgumentException if {@code baseDirectory} is not an existing directory, or if
     *                                            {@code baseDirectory} contains a file called {@code META-INF} which is
     *                                            not a directory.
     */
    public static File getManifestIn(final File baseDirectory) {
        if (!baseDirectory.isDirectory()) {
            throw new IllegalArgumentException("Provided file `" + baseDirectory + "' is not a valid directory.");
        }
        final File metaInf = new File(baseDirectory, META_INF);
        if (metaInf.exists() && !metaInf.isDirectory()) {
            throw new IllegalArgumentException("The location `" + metaInf + "' already exists, but is not a directory.");
        }
        return new File(metaInf, MANIFEST_MF);
    }

    /**
     * Loads a given manifest file into a Manifest.
     *
     * @param file file containing manifest metadata.
     *
     * @return said file parsed into a Manifest object.
     *
     * @throws IOException if the given file does not exist or is an invalid manifest file.
     */
    public static Manifest load(final File file)
            throws IOException {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            return new Manifest(fis);
        } finally {
            FileUtils.closeSilently(fis);
        }
    }

}

@@ -22,16 +22,97 @@
 .util;%0A%0A
+import org.apache.tools.ant.BuildException;%0A%0Aimport java.io.BufferedInputStream;%0A
 import j
@@ -1151,154 +1151,407 @@
     
-if (!baseDirectory.isDirectory()) %7B%0A            throw new IllegalArgumentException(%22Provided file %60%22 + baseDirectory + %22' is not a valid d
+verifyDirectoryExists(baseDirectory);%0A        final File metaInf = new File(baseDirectory, META_INF);%0A        if (metaInf.exists() && !metaInf.isDirectory()) %7B%0A            throw new BuildException(%22The location %60%22 + metaInf + %22' already exists, but is not a directory.%22);%0A        %7D%0A        return new File(metaInf, MANIFEST_MF);%0A    %7D%0A%0A    public static File touchManifestIn(final File baseD
 irectory
 .%22);
@@ -1538,36 +1538,35 @@
 le baseDirectory
-.%22);
+) %7B
 %0A        %7D%0A     
@@ -1550,33 +1550,69 @@
 tory) %7B%0A        
-%7D
+verifyDirectoryExists(baseDirectory);
 %0A        final F
@@ -1663,32 +1663,34 @@
 NF);%0A        if 
+(!
 (metaInf.exists(
@@ -1687,36 +1687,35 @@
 nf.exists() 
-&& !
+%7C%7C 
 metaInf.
 isDirectory(
@@ -1706,123 +1706,852 @@
 Inf.
-isDirectory()) %7B%0A            throw new IllegalArgumentException(%22The location %60%22 + metaInf + %22' already exists, but
+mkdir())) %7B%0A            throw new BuildException(%22Could not create directory %60%22 + metaInf + %22'.%22);%0A        %7D%0A        final File manifest = new File(metaInf, MANIFEST_MF);%0A        if (manifest.exists()) %7B%0A            verify(%22Couldn't touch file %60%22 + manifest + %22'.%22, manifest.setLastModified(System.currentTimeMillis()));%0A        %7D else %7B%0A            try %7B%0A                verify(%22Couldn't create file %60%22 + manifest + %22'.%22, manifest.createNewFile());%0A            %7D catch (final IOException e) %7B%0A                throw new BuildException(%22Could not create file: %22 + manifest, e);%0A            %7D%0A        %7D%0A        return manifest;%0A    %7D%0A%0A    private static void verifyDirectoryExists(final File baseDirectory) %7B%0A        if (!baseDirectory.isDirectory()) %7B%0A            throw new BuildException(%22Provided file %60%22 + baseDirectory.getAbsolutePath() + %22'
  is 
@@ -2548,32 +2548,38 @@
 ) + %22' is not a 
+valid 
 directory.%22);%0A  
@@ -2594,50 +2594,157 @@
     
+%7D%0A%0A
     
-return new File(metaInf, MANIFEST_MF);
+private static void verify(final String message, final boolean b) %7B%0A        if (!b) %7B%0A            throw new BuildException(message);%0A        %7D
 %0A   
@@ -3128,20 +3128,24 @@
         
-File
+Buffered
 InputStr
@@ -3148,17 +3148,17 @@
 tStream 
-f
+b
 is = nul
@@ -3186,17 +3186,17 @@
         
-f
+b
 is = new
@@ -3192,24 +3192,48 @@
   bis = new 
+BufferedInputStream(new 
 FileInputStr
@@ -3241,16 +3241,17 @@
 am(file)
+)
 ;%0A      
@@ -3276,17 +3276,17 @@
 anifest(
-f
+b
 is);%0A   
@@ -3338,17 +3338,17 @@
 ilently(
-f
+b
 is);%0A   

Add touch manifest file method.