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 d164d792017d81880fee21e4a7db68dec10082fa..aa35b0f1c83bbd2bcffe9724988b3e51d8492753 100644 --- a/common/source/java/ch/systemsx/cisd/common/utilities/FileUtilities.java +++ b/common/source/java/ch/systemsx/cisd/common/utilities/FileUtilities.java @@ -19,10 +19,12 @@ package ch.systemsx.cisd.common.utilities; import java.io.BufferedReader; import java.io.File; import java.io.FileFilter; +import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; @@ -477,8 +479,8 @@ public final class FileUtilities * * @param defaultFileName the default name for the new file if the digit pattern could not be found (probably the * starting file). - * @param regex pattern to find out the counter. If <code>null</code> then <code>"(\\d+)"</code> will be - * taken. The given <var>regex</var> must contain <code>(\\d+)</code> or <code>([0-9]+)</code>. + * @param regex pattern to find out the counter. If <code>null</code> then <code>"(\\d+)"</code> will be taken. + * The given <var>regex</var> must contain <code>(\\d+)</code> or <code>([0-9]+)</code>. */ public final static File createNextNumberedFile(File path, Pattern regex, String defaultFileName) { @@ -548,4 +550,62 @@ public final class FileUtilities return null; } } + + /** + * Copies the resource with the given name to a temporary file. + * + * @param resource The name of the resource to copy. + * @param prefix The prefix to use for the temporary name. + * @param postfix The postfix to use for the temporary name. + * @return The name of the temporary file. + * @throws IllegalArgumentException If the resource cannot be found in the class path. + * @throws CheckedExceptionTunnel If an {@link IOException} occurs. + */ + public final static String copyResourceToTempFile(String resource, String prefix, String postfix) + { + final InputStream resourceStream = FileUtilities.class.getResourceAsStream(resource); + if (resourceStream == null) + { + throw new IllegalArgumentException("Resource '" + resource + "' not found."); + } + try + { + final File tempFile = File.createTempFile(prefix, postfix); + tempFile.deleteOnExit(); + OutputStream fileStream = new FileOutputStream(tempFile); + try + { + IOUtils.copy(resourceStream, fileStream); + } finally + { + IOUtils.closeQuietly(fileStream); + } + return tempFile.getAbsolutePath(); + } catch (IOException ex) + { + throw new CheckedExceptionTunnel(ex); + } finally + { + IOUtils.closeQuietly(resourceStream); + } + } + + /** + * Tries to copy the resource with the given name to a temporary file. + * + * @param resource The name of the resource to copy. + * @param prefix The prefix to use for the temporary name. + * @param postfix The postfix to use for the temporary name. + * @return The name of the temporary file, or <code>null</code>, if the resource could not be copied. + */ + public final static String tryCopyResourceToTempFile(String resource, String prefix, String postfix) + { + try + { + return copyResourceToTempFile(resource, prefix, postfix); + } catch (Exception ex) + { + return null; + } + } } 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 57c82262047b5b77b5c3dec1fa3505ff9b69e032..e297a444bccf81d83c086103bf7cbe9baded2a08 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/FileUtilitiesTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/FileUtilitiesTest.java @@ -18,17 +18,22 @@ package ch.systemsx.cisd.common.utilities; import static org.testng.AssertJUnit.assertEquals; import static org.testng.AssertJUnit.assertFalse; +import static org.testng.AssertJUnit.assertNotNull; import static org.testng.AssertJUnit.assertNull; +import static org.testng.AssertJUnit.assertTrue; import static org.testng.AssertJUnit.fail; import java.io.File; +import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; +import java.io.InputStream; import java.util.Arrays; import java.util.List; import java.util.regex.Pattern; import org.apache.commons.io.FilenameUtils; +import org.apache.commons.io.IOUtils; import org.testng.annotations.BeforeSuite; import org.testng.annotations.Test; @@ -235,4 +240,62 @@ public class FileUtilitiesTest assertFalse(relativeFile.isAbsolute()); assertEquals("hello", relativeFile.getPath()); } + + @Test(expectedExceptions = IllegalArgumentException.class) + public final void testCopyResourceToTempFileIllegalResource() + { + FileUtilities.copyResourceToTempFile("nonexistent", "pre", "post"); + } + + @Test + public final void testCopyResourceToTempFile() + { + final String resourceName = "/ch/systemsx/cisd/common/utilities/FileUtilities.class"; + final String absoluteTempFileName = FileUtilities.copyResourceToTempFile(resourceName, "pre", "post"); + assertNotNull(absoluteTempFileName); + final File tempFile = new File(absoluteTempFileName); + final String tempFileName = tempFile.getName(); + assertTrue(tempFile.exists()); + assertTrue(tempFile.length() > 0); + assertTrue(tempFileName.startsWith("pre")); + assertTrue(tempFileName.endsWith("post")); + assertTrue(Arrays.equals(resourceToByteArray(resourceName), fileToByteArray(absoluteTempFileName))); + + } + + private byte[] resourceToByteArray(String resourcename) + { + final InputStream is = FileUtilitiesTest.class.getResourceAsStream(resourcename); + if (is == null) + { + return null; + } + try + { + return IOUtils.toByteArray(is); + } catch (IOException ex) + { + return null; + } finally + { + IOUtils.closeQuietly(is); + } + } + + private byte[] fileToByteArray(String filename) + { + InputStream is = null; + try + { + is = new FileInputStream(filename); + return IOUtils.toByteArray(is); + } catch (IOException ex) + { + return null; + } finally + { + IOUtils.closeQuietly(is); + } + + } }