created a new getTempFile() to FileUtils that does not create the file, it only generates the file name

Former-commit-id: 04e275caade0deba97b3b03cf41fa48f962c0172
This commit is contained in:
Jeremy Long
2014-03-10 21:25:53 -04:00
parent ac5a23ef29
commit a966f263a2
2 changed files with 22 additions and 4 deletions

View File

@@ -112,10 +112,7 @@ public abstract class AbstractSuppressionAnalyzer extends AbstractAnalyzer {
final Pattern uriRx = Pattern.compile("^(https?|file)\\:.*", Pattern.CASE_INSENSITIVE);
if (uriRx.matcher(suppressionFilePath).matches()) {
deleteTempFile = true;
file = File.createTempFile("suppression", ".xml", Settings.getTempDirectory());
if (file.exists()) {
file.delete();
}
file = FileUtils.getTempFile("suppression", "xml");
final URL url = new URL(suppressionFilePath);
try {
Downloader.fetchFile(url, file, false);

View File

@@ -26,6 +26,7 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
@@ -86,6 +87,26 @@ public final class FileUtils {
return success;
}
/**
* Generates a new temporary file name that is guaranteed to be unique.
*
* @param prefix the prefix for the file name to generate
* @param extension the extension of the generated file name
* @return a temporary File
*/
public static File getTempFile(String prefix, String extension) {
final File dir = Settings.getTempDirectory();
if (!dir.exists()) {
dir.mkdirs();
}
final String tempFileName = String.format("%s%s.%s", prefix, UUID.randomUUID().toString(), extension);
final File tempFile = new File(dir, tempFileName);
if (tempFile.exists()) {
return getTempFile(prefix, extension);
}
return tempFile;
}
/**
* Returns the data directory. If a path was specified in dependencycheck.properties or was specified using the
* Settings object, and the path exists, that path will be returned as a File object. If it does not exist, then a