Java Utililty Methods Resource Load

List of utility methods to do Resource Load

Description

The list of methods to do Resource Load are organized into topic(s).

Method

byte[]getResourceAsByteArray(String url)
Retrieves a resource as a byte array.
return toByteArray(url.getClass().getResourceAsStream(url));
byte[]getResourceAsBytes(Class cl, String resname)
get Resource As Bytes
DataInputStream is = new DataInputStream(getResourceAsStream(cl, resname));
byte[] buf = new byte[512];
int bytesread = 0;
byte[] result = new byte[bytesread];
int i = 0;
int n = 0;
while ((n = is.read(buf, 0, buf.length)) > 0) {
    byte[] newres = new byte[n + bytesread];
...
byte[]getResourceAsBytes(Class testClass, String resourceName)
Load a file into memory as a byte array.
return Resources.toByteArray(testClass.getResource(resourceName));
byte[]getResourceAsBytes(ClassLoader classLoader, String resourcePath)
Return resource as byte array.
ByteArrayOutputStream bos = new ByteArrayOutputStream();
InputStream is = null;
try {
    is = classLoader.getResourceAsStream(resourcePath);
    if (is == null)
        throw new IOException("Resource " + resourcePath + " not found.");
    byte[] buf = new byte[1000];
    int len = 0;
...
byte[]getResourceAsBytes(String path, ClassLoader loader)
get Resource As Bytes
if (loader == null)
    loader = Thread.currentThread().getContextClassLoader();
InputStream is = loader.getResourceAsStream(path);
if (is == null)
    throw new FileNotFoundException("Resource not found: " + path);
ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
try {
    int b = -1;
...
byte[]getResourceAsBytes(String url)
get Resource As Bytes
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream(url);
ByteArrayOutputStream os = new ByteArrayOutputStream();
BufferedInputStream bis = new BufferedInputStream(is);
byte buf[] = new byte[8192];
int len;
while ((len = bis.read(buf)) > 0) {
    os.write(buf, 0, len);
bis.close();
is.close();
os.close();
return os.toByteArray();
FilegetResourceAsFile(String path)
get Resource As File
File file = new File(".\\src\\main\\resources\\" + path);
if (file.exists())
    return file;
else
    return null;
FilegetResourceAsFile(String resourceName)
get Resource As File
return new File(Thread.currentThread().getContextClassLoader().getResource(stripLeadingSlash(resourceName))
        .getFile().replaceAll("%20", " "));
FilegetResourceAsFile(String resourcePath)
get Resource As File
try {
    InputStream in = ClassLoader.getSystemClassLoader().getResourceAsStream(resourcePath);
    if (in == null) {
        return null;
    File tempFile = File.createTempFile(String.valueOf(in.hashCode()), ".tmp");
    tempFile.deleteOnExit();
    try (FileOutputStream out = new FileOutputStream(tempFile)) {
...
FilegetResourceAsFile(T testSuite, String resourceName)
get Resource As File
return new File(testSuite.getClass().getResource(resourceName).getPath());