Skip to content
Snippets Groups Projects
Commit f2d25ad1 authored by brinn's avatar brinn
Browse files

add methods copyResourceToTempFile() and tryCopyResourceToTempFile()

SVN: 1491
parent c7bc5eb2
No related branches found
No related tags found
No related merge requests found
...@@ -19,10 +19,12 @@ package ch.systemsx.cisd.common.utilities; ...@@ -19,10 +19,12 @@ package ch.systemsx.cisd.common.utilities;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileFilter; import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
...@@ -477,8 +479,8 @@ public final class FileUtilities ...@@ -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 * @param defaultFileName the default name for the new file if the digit pattern could not be found (probably the
* starting file). * starting file).
* @param regex pattern to find out the counter. If <code>null</code> then <code>"(\\d+)"</code> will be * @param regex pattern to find out the counter. If <code>null</code> then <code>"(\\d+)"</code> will be taken.
* taken. The given <var>regex</var> must contain <code>(\\d+)</code> or <code>([0-9]+)</code>. * 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) public final static File createNextNumberedFile(File path, Pattern regex, String defaultFileName)
{ {
...@@ -548,4 +550,62 @@ public final class FileUtilities ...@@ -548,4 +550,62 @@ public final class FileUtilities
return null; 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;
}
}
} }
...@@ -18,17 +18,22 @@ package ch.systemsx.cisd.common.utilities; ...@@ -18,17 +18,22 @@ package ch.systemsx.cisd.common.utilities;
import static org.testng.AssertJUnit.assertEquals; import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertFalse; import static org.testng.AssertJUnit.assertFalse;
import static org.testng.AssertJUnit.assertNotNull;
import static org.testng.AssertJUnit.assertNull; import static org.testng.AssertJUnit.assertNull;
import static org.testng.AssertJUnit.assertTrue;
import static org.testng.AssertJUnit.fail; import static org.testng.AssertJUnit.fail;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.testng.annotations.BeforeSuite; import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test; import org.testng.annotations.Test;
...@@ -235,4 +240,62 @@ public class FileUtilitiesTest ...@@ -235,4 +240,62 @@ public class FileUtilitiesTest
assertFalse(relativeFile.isAbsolute()); assertFalse(relativeFile.isAbsolute());
assertEquals("hello", relativeFile.getPath()); 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);
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment