Title: [903] trunk/core/src/main/java/org/servicemix/jbi/util/FileUtil.java: Fix test in deleteFile that was causing a directory not to be deleted.
Revision
903
Author
gnt
Date
2005-11-22 17:01:41 -0500 (Tue, 22 Nov 2005)

Log Message

Fix test in deleteFile that was causing a directory not to be deleted.
Replace indentations tabs by spaces.

Modified Paths

Diff

Modified: trunk/core/src/main/java/org/servicemix/jbi/util/FileUtil.java (902 => 903)

--- trunk/core/src/main/java/org/servicemix/jbi/util/FileUtil.java	2005-11-22 21:52:58 UTC (rev 902)
+++ trunk/core/src/main/java/org/servicemix/jbi/util/FileUtil.java	2005-11-22 22:01:41 UTC (rev 903)
@@ -39,220 +39,217 @@
  * @version $Revision$
  */
 public class FileUtil {
-	private static final int DEFAULT_BUFFER_SIZE = 4096;
+    
+    /**
+     * Buffer size used when copying the content of an input stream to
+     * an output stream. 
+     */
+    private static final int DEFAULT_BUFFER_SIZE = 4096;
 
-	/**
-	 * Move a File
-	 * 
-	 * @param src
-	 * @param targetDirectory
-	 */
-	public static void moveFile(File src, File targetDirectory) {
-		src.renameTo(new File(targetDirectory, src.getName()));
-	}
+    /**
+     * Move a File
+     * 
+     * @param src
+     * @param targetDirectory
+     */
+    public static void moveFile(File src, File targetDirectory) {
+        src.renameTo(new File(targetDirectory, src.getName()));
+    }
 
-	/**
-	 * Build a path- but do not create it
-	 * 
-	 * @param parent
-	 * @param subDirectory
-	 * @return a File representing the path
-	 */
-	public static File getDirectoryPath(File parent, String subDirectory) {
-		File result = new File(parent.getAbsolutePath() + File.separator
-				+ subDirectory);
-		return result;
-	}
+    /**
+     * Build a path- but do not create it
+     * 
+     * @param parent
+     * @param subDirectory
+     * @return a File representing the path
+     */
+    public static File getDirectoryPath(File parent, String subDirectory) {
+        File result = new File(parent.getAbsolutePath() + File.separator + subDirectory);
+        return result;
+    }
 
-	/**
-	 * Build a directory path - creating directories if neccesary
-	 * 
-	 * @param file
-	 * @return true if the directory exists, or making it was successful
-	 */
-	public static boolean buildDirectory(File file) {
-		return file.exists() || file.mkdirs();
-	}
+    /**
+     * Build a directory path - creating directories if neccesary
+     * 
+     * @param file
+     * @return true if the directory exists, or making it was successful
+     */
+    public static boolean buildDirectory(File file) {
+        return file.exists() || file.mkdirs();
+    }
 
-	/**
-	 * Copy in stream to an out stream
-	 * 
-	 * @param in
-	 * @param out
-	 * @throws IOException
-	 */
-	public static void copyInputStream(InputStream in, OutputStream out)
-			throws IOException {
-		byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
-		int len;
-		while ((len = in.read(buffer)) >= 0)
-			out.write(buffer, 0, len);
-		in.close();
-		out.close();
-	}
+    /**
+     * Copy in stream to an out stream
+     * 
+     * @param in
+     * @param out
+     * @throws IOException
+     */
+    public static void copyInputStream(InputStream in, OutputStream out) throws IOException {
+        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
+        int len;
+        while ((len = in.read(buffer)) >= 0) {
+            out.write(buffer, 0, len);
+        }
+        in.close();
+        out.close();
+    }
 
-	/**
-	 * Unpack a zip file
-	 * 
-	 * @param theFile
-	 * @param targetDir
-	 * @return the file
-	 * @throws IOException
-	 */
-	public static File unpackArchive(File theFile, File targetDir)
-			throws IOException {
-		if (!theFile.exists()) {
-			throw new IOException(theFile.getAbsolutePath()
-					+ " Does Not Exist!");
-		}
-		if (!targetDir.exists()) {
-			targetDir.mkdirs();
-		}
-		ZipFile zipFile;
-		zipFile = new ZipFile(theFile);
-		for (Enumeration entries = zipFile.entries(); entries.hasMoreElements();) {
-			ZipEntry entry = (ZipEntry) entries.nextElement();
-			File file = new File(targetDir, File.separator + entry.getName());
-			// Take the sledgehammer approach to creating directories
-			// to work around ZIP's that incorrectly miss directories
-			file.mkdirs();
-			file.delete();
-			if (!entry.isDirectory())
-				copyInputStream(zipFile.getInputStream(entry),
-						new BufferedOutputStream(new FileOutputStream(file)));
-		}
-		zipFile.close();
-		return theFile;
-	}
+    /**
+     * Unpack a zip file
+     * 
+     * @param theFile
+     * @param targetDir
+     * @return the file
+     * @throws IOException
+     */
+    public static File unpackArchive(File theFile, File targetDir) throws IOException {
+        if (!theFile.exists()) {
+            throw new IOException(theFile.getAbsolutePath() + " does Not Exist!");
+        }
+        if (!targetDir.exists()) {
+            targetDir.mkdirs();
+        }
+        ZipFile zipFile;
+        zipFile = new ZipFile(theFile);
+        for (Enumeration entries = zipFile.entries(); entries.hasMoreElements();) {
+            ZipEntry entry = (ZipEntry) entries.nextElement();
+            File file = new File(targetDir, File.separator + entry.getName());
+            // Take the sledgehammer approach to creating directories
+            // to work around ZIP's that incorrectly miss directories
+            file.mkdirs();
+            file.delete();
+            if (!entry.isDirectory())
+                copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(file)));
+        }
+        zipFile.close();
+        return theFile;
+    }
 
-	/**
-	 * Unpack an archive from a URL
-	 * 
-	 * @param url
-	 * @param targetDir
-	 * @return the file to the url
-	 * @throws IOException
-	 */
-	public static File unpackArchive(URL url, File targetDir)
-			throws IOException {
-		if (!targetDir.exists()) {
-			targetDir.mkdirs();
-		}
-		InputStream in = new BufferedInputStream(url.openStream(),
-				DEFAULT_BUFFER_SIZE);
-		String fileName = url.getFile();
-		// make sure we get the actual file
-		int index = fileName.lastIndexOf("/");
-		if (index > 0) {
-			fileName = fileName.substring(index + 1);
-		}
-		File zip = new File(targetDir, fileName);
-		OutputStream out = new BufferedOutputStream(new FileOutputStream(zip));
-		copyInputStream(in, out);
-		out.close();
-		return unpackArchive(zip, targetDir);
-	}
+    /**
+     * Unpack an archive from a URL
+     * 
+     * @param url
+     * @param targetDir
+     * @return the file to the url
+     * @throws IOException
+     */
+    public static File unpackArchive(URL url, File targetDir) throws IOException {
+        if (!targetDir.exists()) {
+            targetDir.mkdirs();
+        }
+        InputStream in = new BufferedInputStream(url.openStream(), DEFAULT_BUFFER_SIZE);
+        String fileName = url.getFile();
+        // make sure we get the actual file
+        int index = fileName.lastIndexOf("/");
+        if (index > 0) {
+            fileName = fileName.substring(index + 1);
+        }
+        File zip = new File(targetDir, fileName);
+        OutputStream out = new BufferedOutputStream(new FileOutputStream(zip));
+        copyInputStream(in, out);
+        out.close();
+        return unpackArchive(zip, targetDir);
+    }
 
-	/**
-	 * Validate an arcive contains a named entry
-	 * 
-	 * @param theFile
-	 * @param name
-	 * @return true if the entry exists
-	 * @throws IOException
-	 */
-	public static boolean archiveContainsEntry(File theFile, String name)
-			throws IOException {
-		boolean result = false;
-		ZipFile zipFile;
-		zipFile = new ZipFile(theFile);
-		for (Enumeration entries = zipFile.entries(); entries.hasMoreElements();) {
-			ZipEntry entry = (ZipEntry) entries.nextElement();
-			if (entry.getName().equals(name)) {
-				result = true;
-				break;
-			}
-		}
-		zipFile.close();
-		return result;
-	}
+    /**
+     * Validate that an archive contains a named entry
+     * 
+     * @param theFile
+     * @param name
+     * @return true if the entry exists
+     * @throws IOException
+     */
+    public static boolean archiveContainsEntry(File theFile, String name) throws IOException {
+        boolean result = false;
+        ZipFile zipFile;
+        zipFile = new ZipFile(theFile);
+        for (Enumeration entries = zipFile.entries(); entries.hasMoreElements();) {
+            ZipEntry entry = (ZipEntry) entries.nextElement();
+            if (entry.getName().equals(name)) {
+                result = true;
+                break;
+            }
+        }
+        zipFile.close();
+        return result;
+    }
 
-	/**
-	 * Create a unique directory within a directory 'root'
-	 * 
-	 * @param rootDir
-	 * @param seed
-	 * @return unique directory
-	 * @throws IOException
-	 */
-	public synchronized static File createUniqueDirectory(File rootDir,
-			String seed) throws IOException {
-		int index = seed.lastIndexOf('.');
-		if (index > 0) {
-			seed = seed.substring(0, index);
-		}
-		File result = null;
-		int count = 0;
-		while (result == null) {
-			String name = seed + "." + count + ".tmp";
-			File file = new File(rootDir, name);
-			if (!file.exists()) {
-				file.mkdirs();
-				result = file;
-			}
-			count++;
-		}
-		return result;
-	}
+    /**
+     * Create a unique directory within a directory 'root'
+     * 
+     * @param rootDir
+     * @param seed
+     * @return unique directory
+     * @throws IOException
+     */
+    public synchronized static File createUniqueDirectory(File rootDir, String seed) throws IOException {
+        int index = seed.lastIndexOf('.');
+        if (index > 0) {
+            seed = seed.substring(0, index);
+        }
+        File result = null;
+        int count = 0;
+        while (result == null) {
+            String name = seed + "." + count + ".tmp";
+            File file = new File(rootDir, name);
+            if (!file.exists()) {
+                file.mkdirs();
+                result = file;
+            }
+            count++;
+        }
+        return result;
+    }
 
-	/**
-	 * Delete a file
-	 * 
-	 * @param fileToDelete
-	 * @return true if the File is deleted
-	 */
-	public static boolean deleteFile(File fileToDelete) {
-		boolean result = true;
-		if (fileToDelete != null && fileToDelete.exists()) {
-			if (fileToDelete.isDirectory()) {
-				File[] files = fileToDelete.listFiles();
-                if (files != null) {
+    /**
+     * Delete a file
+     * 
+     * @param fileToDelete
+     * @return true if the File is deleted
+     */
+    public static boolean deleteFile(File fileToDelete) {
+        boolean result = true;
+        if (fileToDelete != null && fileToDelete.exists()) {
+            if (fileToDelete.isDirectory()) {
+                File[] files = fileToDelete.listFiles();
+                if (files == null) {
                     result = false;
                 } else {
-    				for (int i = 0; i < files.length; i++) {
-    					File file = files[i];
-    					if (!file.getName().equals(".") && !file.getName().equals("..")) {
-    						if (file.isDirectory()) {
-    							result &= deleteFile(file);
-    						}
-    						result &= file.delete();
-    					}
-    				}
+                    for (int i = 0; i < files.length; i++) {
+                        File file = files[i];
+                        if (!file.getName().equals(".") && !file.getName().equals("..")) {
+                            if (file.isDirectory()) {
+                                result &= deleteFile(file);
+                            }
+                            result &= file.delete();
+                        }
+                    }
                 }
-			}
-			result &= fileToDelete.delete();
-			if (fileToDelete.exists()) {
-				fileToDelete.delete();
-			}
-		}
-		return result;
-	}
+            }
+            result &= fileToDelete.delete();
+            if (fileToDelete.exists()) {
+                fileToDelete.delete();
+            }
+        }
+        return result;
+    }
 
     /**
      * Zip up a directory
+     * 
      * @param directory
      * @param zipName
      * @throws IOException
      */
-	public static void zipDir(String directory, String zipName)
-			throws IOException {
-		// create a ZipOutputStream to zip the data to
-		ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipName));
-		String path = "";
-		zipDir(directory, zos, path);
-		// close the stream
-		zos.close();
-	}
+    public static void zipDir(String directory, String zipName) throws IOException {
+        // create a ZipOutputStream to zip the data to
+        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipName));
+        String path = "";
+        zipDir(directory, zos, path);
+        // close the stream
+        zos.close();
+    }
 
     /**
      * Zip up a directory path
@@ -261,28 +258,27 @@
      * @param path
      * @throws IOException
      */
-	public static void zipDir(String directory, ZipOutputStream zos, String path)
-			throws IOException {
-		File zipDir = new File(directory);
-		// get a listing of the directory content
-		String[] dirList = zipDir.list();
-		byte[] readBuffer = new byte[2156];
-		int bytesIn = 0;
-		// loop through dirList, and zip the files
-		for (int i = 0; i < dirList.length; i++) {
-			File f = new File(zipDir, dirList[i]);
-			if (f.isDirectory()) {
-				String filePath = f.getPath();				
-				zipDir(filePath, zos,path+f.getName()+"/");
-				continue;
-			}
-			FileInputStream fis = new FileInputStream(f);
-			ZipEntry anEntry = new ZipEntry(path+f.getName());
-			zos.putNextEntry(anEntry);
-			while ((bytesIn = fis.read(readBuffer)) != -1) {
-				zos.write(readBuffer, 0, bytesIn);
-			}
-			fis.close();
-		}
-	}
+    public static void zipDir(String directory, ZipOutputStream zos, String path) throws IOException {
+        File zipDir = new File(directory);
+        // get a listing of the directory content
+        String[] dirList = zipDir.list();
+        byte[] readBuffer = new byte[2156];
+        int bytesIn = 0;
+        // loop through dirList, and zip the files
+        for (int i = 0; i < dirList.length; i++) {
+            File f = new File(zipDir, dirList[i]);
+            if (f.isDirectory()) {
+                String filePath = f.getPath();
+                zipDir(filePath, zos, path + f.getName() + "/");
+                continue;
+            }
+            FileInputStream fis = new FileInputStream(f);
+            ZipEntry anEntry = new ZipEntry(path + f.getName());
+            zos.putNextEntry(anEntry);
+            while ((bytesIn = fis.read(readBuffer)) != -1) {
+                zos.write(readBuffer, 0, bytesIn);
+            }
+            fis.close();
+        }
+    }
 }
\ No newline at end of file

Reply via email to