001 /*
002 * JBoss DNA (http://www.jboss.org/dna)
003 * See the COPYRIGHT.txt file distributed with this work for information
004 * regarding copyright ownership. Some portions may be licensed
005 * to Red Hat, Inc. under one or more contributor license agreements.
006 * See the AUTHORS.txt file in the distribution for a full listing of
007 * individual contributors.
008 *
009 * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
010 * is licensed to you under the terms of the GNU Lesser General Public License as
011 * published by the Free Software Foundation; either version 2.1 of
012 * the License, or (at your option) any later version.
013 *
014 * JBoss DNA is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017 * Lesser General Public License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this software; if not, write to the Free
021 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
023 */
024
025 package org.jboss.dna.common.util;
026
027 import java.io.BufferedInputStream;
028 import java.io.BufferedOutputStream;
029 import java.io.File;
030 import java.io.FileInputStream;
031 import java.io.FileOutputStream;
032 import java.io.IOException;
033 import java.io.InputStream;
034 import java.io.OutputStream;
035 import java.net.MalformedURLException;
036 import java.net.URL;
037
038 public class FileUtil {
039
040 /**
041 * Delete the file or directory at the supplied path. This method works on a directory that is not empty, unlike the
042 * {@link File#delete()} method.
043 *
044 * @param path the path to the file or directory that is to be deleted
045 * @return true if the file or directory at the supplied path existed and was successfully deleted, or false otherwise
046 */
047 public static boolean delete( String path ) {
048 if (path == null || path.trim().length() == 0) return false;
049 return delete(new File(path));
050 }
051
052 /**
053 * Delete the file or directory given by the supplied reference. This method works on a directory that is not empty, unlike
054 * the {@link File#delete()} method.
055 *
056 * @param fileOrDirectory the reference to the Java File object that is to be deleted
057 * @return true if the supplied file or directory existed and was successfully deleted, or false otherwise
058 */
059 public static boolean delete( File fileOrDirectory ) {
060 if (fileOrDirectory == null) return false;
061 if (!fileOrDirectory.exists()) return false;
062
063 // The file/directory exists, so if a directory delete all of the contents ...
064 if (fileOrDirectory.isDirectory()) {
065 for (File childFile : fileOrDirectory.listFiles()) {
066 delete(childFile); // recursive call (good enough for now until we need something better)
067 }
068 // Now an empty directory ...
069 }
070 // Whether this is a file or empty directory, just delete it ...
071 return fileOrDirectory.delete();
072 }
073
074 /**
075 * Copy the source file system structure into the supplied target location. If the source is a file, the destiniation will be
076 * created as a file; if the source is a directory, the destination will be created as a directory.
077 *
078 * @param sourceFileOrDirectory the file or directory whose contents are to be copied into the target location
079 * @param destinationFileOrDirectory the location where the copy is to be placed; does not need to exist, but if it does its
080 * type must match that of <code>src</code>
081 * @return the number of files (not directories) that were copied
082 * @throws IllegalArgumentException if the <code>src</code> or <code>dest</code> references are null
083 * @throws IOException
084 */
085 public static int copy( File sourceFileOrDirectory,
086 File destinationFileOrDirectory ) throws IOException {
087 int numberOfFilesCopied = 0;
088 if (sourceFileOrDirectory.isDirectory()) {
089 destinationFileOrDirectory.mkdirs();
090 String list[] = sourceFileOrDirectory.list();
091
092 for (int i = 0; i < list.length; i++) {
093 String dest1 = destinationFileOrDirectory.getPath() + File.separator + list[i];
094 String src1 = sourceFileOrDirectory.getPath() + File.separator + list[i];
095 numberOfFilesCopied += copy(new File(src1), new File(dest1));
096 }
097 } else {
098 InputStream fin = new FileInputStream(sourceFileOrDirectory);
099 fin = new BufferedInputStream(fin);
100 try {
101 OutputStream fout = new FileOutputStream(destinationFileOrDirectory);
102 fout = new BufferedOutputStream(fout);
103 try {
104 int c;
105 while ((c = fin.read()) >= 0) {
106 fout.write(c);
107 }
108 } finally {
109 fout.close();
110 }
111 } finally {
112 fin.close();
113 }
114 numberOfFilesCopied++;
115 }
116 return numberOfFilesCopied;
117 }
118
119 /**
120 * Utility to convert {@link File} to {@link URL}.
121 *
122 * @param filePath the path of the file
123 * @return the {@link URL} representation of the file.
124 * @throws MalformedURLException
125 * @throws IllegalArgumentException if the file path is null, empty or blank
126 */
127 public static URL convertFileToURL( String filePath ) throws MalformedURLException {
128 CheckArg.isNotEmpty(filePath, "filePath");
129 File file = new File(filePath.trim());
130 return file.toURI().toURL();
131 }
132
133 }