Example usage for java.lang Class getResourceAsStream

List of usage examples for java.lang Class getResourceAsStream

Introduction

In this page you can find the example usage for java.lang Class getResourceAsStream.

Prototype

@CallerSensitive
public InputStream getResourceAsStream(String name) 

Source Link

Document

Finds a resource with a given name.

Usage

From source file:org.apache.solr.analytics.AbstractAnalyticsStatsTest.java

public static String[] fileToStringArr(Class<?> clazz, String fileName) throws FileNotFoundException {
    InputStream in = clazz.getResourceAsStream(fileName);
    if (in == null)
        throw new FileNotFoundException("Resource not found: " + fileName);
    Scanner file = new Scanner(in, "UTF-8");
    try {//from   w  ww.  j  av a 2  s .  c  o m
        ArrayList<String> strList = new ArrayList<>();
        while (file.hasNextLine()) {
            String line = file.nextLine();
            line = line.trim();
            if (StringUtils.isBlank(line) || line.startsWith("#")) {
                continue;
            }
            String[] param = line.split("=");
            strList.add(param[0]);
            strList.add(param[1]);
        }
        return strList.toArray(new String[0]);
    } finally {
        IOUtils.closeWhileHandlingException(file, in);
    }
}

From source file:com.uber.jenkins.phabricator.utils.TestUtils.java

public static JSONObject getJSONFromFile(Class klass, String filename) throws IOException {
    InputStream in = klass.getResourceAsStream(String.format("%s.json", filename));
    return slurpFromInputStream(in);
}

From source file:net.solarnetwork.util.ClassUtils.java

/**
 * Load a classpath SQL resource into a String.
 * /* w w  w. j a  va  2  s  .co m*/
 * @param resourceName the SQL resource to load
 * @param clazz the Class to load the resource from
 * @return the String
 */
public static String getResourceAsString(String resourceName, Class<?> clazz) {
    InputStream in = clazz.getResourceAsStream(resourceName);
    if (in == null) {
        throw new RuntimeException("Fesource [" + resourceName + "] not found");
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        byte[] buffer = new byte[4096];
        int bytesRead = -1;
        while ((bytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
        }
        out.flush();
        return out.toString();
    } catch (IOException e) {
        throw new RuntimeException("Error reading resource [" + resourceName + ']', e);
    } finally {
        try {
            in.close();
        } catch (IOException ex) {
            LOG.warn("Could not close InputStream", ex);
        }
        try {
            out.close();
        } catch (IOException ex) {
            LOG.warn("Could not close OutputStream", ex);
        }
    }
}

From source file:org.apache.solr.analytics.legacy.LegacyAbstractAnalyticsTest.java

public static String[] fileToStringArr(Class<?> clazz, String fileName) throws FileNotFoundException {
    InputStream in = clazz.getResourceAsStream("/solr/analytics/legacy/" + fileName);
    if (in == null)
        throw new FileNotFoundException("Resource not found: " + fileName);
    Scanner file = new Scanner(in, "UTF-8");
    try {/*from w ww  .  j av a 2  s .c  o m*/
        ArrayList<String> strList = new ArrayList<>();
        while (file.hasNextLine()) {
            String line = file.nextLine();
            line = line.trim();
            if (StringUtils.isBlank(line) || line.startsWith("#")) {
                continue;
            }
            String[] param = line.split("=");
            strList.add(param[0]);
            strList.add(param[1]);
        }
        return strList.toArray(new String[0]);
    } finally {
        IOUtils.closeWhileHandlingException(file, in);
    }
}

From source file:com.igormaznitsa.j2z80.utils.Utils.java

/**
 * Read a text (UTF-8 encoded) resource in a class path of a class 
 * @param thisClass the class to be used to get a class path, must not be null
 * @param resource the resource name, must not be null
 * @return the loaded text as a solid string
 * @throws IOException it will be thrown if there is any transport error
 * @throws FileNotFoundException it will be thrown if the resource is not found
 *///from  w  w w . j a v a2 s  . c o  m
public static String readTextResource(final Class<?> thisClass, final String resource) throws IOException {
    final InputStream file = thisClass.getResourceAsStream(resource);
    if (file == null) {
        throw new FileNotFoundException("Can't find resource " + resource);
    }
    final StringBuilder builder = new StringBuilder();
    final BufferedReader reader = new BufferedReader(new InputStreamReader(file, "UTF-8"));
    try {
        while (true) {
            final String line = reader.readLine();
            if (line == null) {
                break;
            }
            builder.append(line).append(NEXT_LINE);
        }
        return builder.toString();
    } finally {
        silentlyClose(reader);
    }

}

From source file:io.milton.common.FileUtils.java

public static String readResource(Class cl, String res) throws IOException {
    InputStream in = cl.getResourceAsStream(res);
    if (in == null) {
        throw new IOException(
                "Failed to read resource: " + res + " relative to class: " + cl.getCanonicalName());
    }/*from  w  ww . ja  va  2  s  . c  om*/
    ByteArrayOutputStream out = readIn(in);
    return out.toString();
}

From source file:com.attilax.zip.FileUtil.java

/**
 * ???//from ww  w .  ja va  2s .  co  m
 * 
 * @param clazz
 * @param name
 * @return
 */
@SuppressWarnings("unchecked")
public static InputStream getResourceAsStream(Class clazz, String name) {
    try {
        InputStream inputStream = clazz.getResourceAsStream(name);
        if (inputStream == null)
            inputStream = clazz.getClassLoader().getResourceAsStream(name);
        return inputStream;
    } catch (Exception e) {
        if (log.isWarnEnabled())
            log.warn("??", e);
        return null;
    }
}

From source file:com.carrotgarden.eclipse.fileinstall.util.ProjectUtil.java

/**
 * Extract class path resource to the file system.
 *///w  w  w . j av a 2 s .c  o m
public static void copyFromClasspathIntoProject(
        //
        final Class<?> klaz, //
        final String sourcePath, //
        final IProject project, //
        final String targetPath //
) throws Exception {

    final InputStream input = klaz.getResourceAsStream(sourcePath);

    final File target = file(project, targetPath);

    if (!target.exists()) {
        target.getParentFile().mkdirs();
    }

    final OutputStream output = new FileOutputStream(target);

    IOUtils.copy(input, output);

    input.close();
    output.close();

}

From source file:org.codemucker.jtest.TestHelper.java

private static Properties loadResourceProperties(Class<?> relativeToClass, String fileName) {
    Properties props = new Properties();
    InputStream is = null;//from   w  w  w .  j ava 2s .c om
    try {
        is = relativeToClass.getResourceAsStream(fileName);
        if (is != null) {
            props.load(is);
        }
    } catch (IOException e) {
        // ignore, fail silently
    } finally {
        IOUtils.closeQuietly(is);
    }
    return props;
}

From source file:org.apache.solr.analytics.facet.AbstractAnalyticsFacetCloudTest.java

public static ModifiableSolrParams fileToParams(Class<?> clazz, String fileName) throws FileNotFoundException {
    InputStream in = clazz.getResourceAsStream(fileName);
    if (in == null)
        throw new FileNotFoundException("Resource not found: " + fileName);
    Scanner file = new Scanner(in, "UTF-8");
    try {//from   w  w  w .j a  v a  2s  .  c  o m
        ModifiableSolrParams params = new ModifiableSolrParams();
        params.set("q", "*:*");
        params.set("indent", "true");
        params.set("olap", "true");
        params.set("rows", "0");
        while (file.hasNextLine()) {
            String line = file.nextLine();
            line = line.trim();
            if (StringUtils.isBlank(line) || line.startsWith("#")) {
                continue;
            }
            String[] param = line.split("=");
            params.set(param[0], param[1]);
        }
        return params;
    } finally {
        IOUtils.closeWhileHandlingException(file, in);
    }
}