Methods Summary |
---|
public static java.io.File | copyFile(java.io.File root, java.lang.String folder, java.lang.String file)
File f;
if (folder != null) {
f = new File(root.toString() + "/" + folder);
if (!f.exists()) {
f.mkdirs();
f.deleteOnExit();
}
} else {
f = root;
}
File dest = new File(f.toString() + "/" + file);
InputStream in = Support_Resources.getStream(folder == null ? file
: folder + "/" + file);
try {
copyLocalFileto(dest, in);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return dest;
|
public static void | copyLocalFileto(java.io.File dest, java.io.InputStream in)
if (!dest.exists()) {
FileOutputStream out = new FileOutputStream(dest);
int result;
byte[] buf = new byte[4096];
while ((result = in.read(buf)) != -1) {
out.write(buf, 0, result);
}
in.close();
out.close();
dest.deleteOnExit();
}
|
public static java.io.File | createTempFile(java.lang.String suffix)
return File.createTempFile("hyts_", suffix, null);
|
public static java.io.File | createTempFolder()
File folder = null;
try {
folder = File.createTempFile("hyts_resources", "", null);
folder.delete();
folder.mkdirs();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
folder.deleteOnExit();
return folder;
|
public static java.lang.String | getAbsoluteResourcePath(java.lang.String name)Util method to get absolute path to resource file
URL url = ClassLoader.getSystemClassLoader().getResource(name);
if (url == null) {
throw new RuntimeException("Failed to load resource: " + name);
}
try {
return new File(url.toURI()).getAbsolutePath();
} catch (URISyntaxException e) {
throw new RuntimeException("Failed to load resource: " + name);
}
|
public static java.io.File | getExternalLocalFile(java.lang.String url)
File resources = createTempFolder();
InputStream in = new URL(url).openStream();
File temp = new File(resources.toString() + "/local.tmp");
copyLocalFileto(temp, in);
return temp;
|
public static java.io.InputStream | getResourceStream(java.lang.String name)Util method to load resource files
//ATTENTION:
// Against class.getResourceStream(name) the name can start with a "/".
// Against classLoader.getResourceStream NOT!
InputStream is;
// is = Support_Resources.class.getClassLoader().getResourceAsStream(name); This would work without leading "/"
is = Support_Resources.class.getResourceAsStream(name);
// is = ClassLoader.getSystemClassLoader().getResourceAsStream(name); This would work without leading "/"
if (is == null) {
name = "/tests/resources/" + name;
is = Support_Resources.class.getResourceAsStream(name);
if (is == null) {
throw new RuntimeException("Failed to load resource: " + name);
}
}
return is;
|
public static java.lang.String | getResourceURL(java.lang.String resource)
return "http://" + Support_Configuration.TestResources + resource;
|
public static java.io.InputStream | getStream(java.lang.String name)
return Support_Resources.class.getResourceAsStream(RESOURCE_PACKAGE
+ name);
|
public static java.lang.String | getURL(java.lang.String name)
String folder = null;
String fileName = name;
File resources = createTempFolder();
int index = name.lastIndexOf("/");
if (index != -1) {
folder = name.substring(0, index);
name = name.substring(index + 1);
}
copyFile(resources, folder, name);
URL url = null;
String resPath = resources.toString();
if (resPath.charAt(0) == '/" || resPath.charAt(0) == '\\") {
resPath = resPath.substring(1);
}
try {
url = new URL("file:/" + resPath + "/" + fileName);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return url.toString();
|
public static int | writeResourceToStream(java.lang.String name, java.io.OutputStream out)Util method to write resource files directly to an OutputStream.
InputStream input = getResourceStream(name);
byte[] buffer = new byte[512];
int total = 0;
int count;
try {
count = input.read(buffer);
while (count != -1) {
out.write(buffer, 0, count);
total = total + count;
count = input.read(buffer);
}
return total;
} catch (IOException e) {
throw new RuntimeException("Failed to write to passed stream.", e);
}
|