Java Resource Load getResourceBytes(String path)

Here you can find the source of getResourceBytes(String path)

Description

get Resource Bytes

License

Open Source License

Declaration

static public byte[] getResourceBytes(String path) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    static public byte[] getResourceBytes(String path) {
        InputStream s = ClassLoader.getSystemClassLoader().getResourceAsStream(path);
        try {/*from ww  w .j  a  va  2s.  co m*/
            return readAll(s);
        } finally {
            try {
                s.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    static public byte[] readAll(InputStream s) {
        byte[] temp = new byte[1024];
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            while (true) {
                int read = s.read(temp, 0, temp.length);
                if (read <= 0)
                    break;
                os.write(temp, 0, read);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return os.toByteArray();
    }
}

Related

  1. getResourceAsString(String relativePath)
  2. getResourceAsTempFile(Object context, String resourceName)
  3. getResourceBundle(String baseName)
  4. getResourceBundle(String resourceURI, Locale targetLocale)
  5. getResourceBundleForLoc(String language, String fileName)
  6. getResourceByZip(final ZipInputStream zipIN, final String resourcePath)
  7. getResourceDirectory()
  8. getResourceFile(Class clazz, String path)
  9. getResourceFile(Class theClass, String fileName)