From f44dced9e541de77939e0164a7cdfc73ba358dd9 Mon Sep 17 00:00:00 2001 From: ribeaudc <ribeaudc> Date: Mon, 28 Apr 2008 10:53:20 +0000 Subject: [PATCH] [LMS-400] add: - 'FileUtilities.createTempFile' (tested). SVN: 5828 --- .../cisd/common/utilities/FileUtilities.java | 32 +++++++++++++++++++ .../common/utilities/FileUtilitiesTest.java | 24 ++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/FileUtilities.java b/common/source/java/ch/systemsx/cisd/common/utilities/FileUtilities.java index c79f06f39b6..f19dcfea456 100644 --- a/common/source/java/ch/systemsx/cisd/common/utilities/FileUtilities.java +++ b/common/source/java/ch/systemsx/cisd/common/utilities/FileUtilities.java @@ -42,6 +42,7 @@ import java.util.regex.Pattern; import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.CharUtils; +import org.apache.commons.lang.RandomStringUtils; import org.apache.commons.lang.StringUtils; import ch.systemsx.cisd.common.exceptions.CheckedExceptionTunnel; @@ -1054,4 +1055,35 @@ public final class FileUtilities } } + /** + * Create a temporary file in a given directory. + * <p> + * The file denoted by the returned abstract pathname did not exist before this method was + * invoked, any subsequent invocation of this method will yield a different file name. + * </p> + * <p> + * The filename is prefixNNNNsuffix where NNNN is a random number. + * </p> + * <p> + * This method is different from File.createTempFile() of JDK 1.2 as it doesn't create the file + * itself. It uses the location pointed to by java.io.tmpdir when the parentDir attribute is + * null. + * </p> + */ + public final static File createTempFile(final String prefixOrNull, final String suffixOrNull, + final File parentDirOrNull) + { + File result = null; + final String parent = + (parentDirOrNull == null) ? System.getProperty("java.io.tmpdir") : parentDirOrNull + .getPath(); + do + { + result = + new File(parent, StringUtils.defaultString(prefixOrNull) + + RandomStringUtils.randomNumeric(4) + + StringUtils.defaultString(suffixOrNull)); + } while (result.exists()); + return result; + } } diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/FileUtilitiesTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/FileUtilitiesTest.java index 5334caee52d..1003a9b47b7 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/FileUtilitiesTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/FileUtilitiesTest.java @@ -36,6 +36,7 @@ import java.util.regex.Pattern; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; +import org.apache.commons.lang.math.NumberUtils; import org.testng.annotations.Test; import ch.systemsx.cisd.common.Constants; @@ -500,4 +501,27 @@ public final class FileUtilitiesTest extends AbstractFileSystemTestCase assertEquals(canonicalPath, FileUtilities.normalizeFile(file).getAbsolutePath()); } + @Test + public final void testCreateTempFile() + { + File file = FileUtilities.createTempFile(null, null, null); + assertNotNull(file); + assertFalse(file.exists()); + String name = file.getName(); + assertTrue(name.length() == 4); + NumberUtils.isNumber(name); + String prefix = System.getProperty("java.io.tmpdir"); + assertTrue(file.getAbsolutePath().startsWith(prefix)); + // With a prefix, suffix and a parent + final String filePrefix = "prefix"; + final String fileSuffix = ".tmp"; + file = FileUtilities.createTempFile(filePrefix, fileSuffix, workingDirectory); + assertNotNull(file); + assertFalse(file.exists()); + assertTrue(file.getAbsolutePath().startsWith(workingDirectory.getAbsolutePath())); + name = file.getName(); + assertTrue(name.startsWith(filePrefix)); + assertTrue(name.endsWith(fileSuffix)); + assertEquals(filePrefix.length() + fileSuffix.length() + 4, name.length()); + } } -- GitLab