Example usage for java.net URL openStream

List of usage examples for java.net URL openStream

Introduction

In this page you can find the example usage for java.net URL openStream.

Prototype

public final InputStream openStream() throws java.io.IOException 

Source Link

Document

Opens a connection to this URL and returns an InputStream for reading from that connection.

Usage

From source file:net.sourceforge.jaulp.lang.PropertiesUtils.java

/**
 * Gives a Properties-object from the given packagepath.
 * /*from  w w w  .  j  ava 2  s  . c o  m*/
 * @param packagePath
 *            The package-path and the name from the resource as a String.
 * @return The Properties-object from the given packagepath.
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static Properties loadProperties(final String packagePath) throws IOException {
    Properties properties = null;
    final URL url = ClassUtils.getResource(packagePath);
    if (url != null) {
        properties = new Properties();
        properties.load(url.openStream());
    } else {
        InputStream is = ClassUtils.getResourceAsStream(packagePath);
        if (is != null) {
            properties = new Properties();
            properties.load(is);
        }
    }
    return properties;
}

From source file:org.apache.commons.httpclient.contrib.ssl.AuthSSLProtocolSocketFactory.java

private static KeyStore createKeyStore(final URL url, final String password)
        throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
    if (url == null) {
        throw new IllegalArgumentException("Keystore url may not be null");
    }// ww w . j  a va  2 s  . c  om
    LOG.debug("Initializing key store");
    KeyStore keystore = KeyStore.getInstance("jks");
    keystore.load(url.openStream(), password != null ? password.toCharArray() : null);
    return keystore;
}

From source file:net.sf.jabref.exporter.FileActions.java

/**
 * This method attempts to get a Reader for the file path given, either by
 * loading it as a resource (from within jar), or as a normal file. If
 * unsuccessful (e.g. file not found), an IOException is thrown.
 *///from   w  ww. ja v  a 2 s  .  c  o  m
public static Reader getReader(String name) throws IOException {
    Reader reader;
    // Try loading as a resource first. This works for files inside the jar:
    URL reso = Globals.class.getResource(name);

    // If that didn't work, try loading as a normal file URL:
    if (reso != null) {
        try {
            reader = new InputStreamReader(reso.openStream());
        } catch (FileNotFoundException ex) {
            throw new IOException("Cannot find layout file: '" + name + "'.");
        }
    } else {
        File f = new File(name);
        try {
            reader = new FileReader(f);
        } catch (FileNotFoundException ex) {
            throw new IOException("Cannot find layout file: '" + name + "'.");
        }
    }

    return reader;
}

From source file:ar.com.fdvs.dj.core.DynamicJasperHelper.java

protected static DynamicJasperDesign generateJasperDesign(DynamicReport dr) throws CoreException {
    DynamicJasperDesign jd = null;/*ww w . jav a  2 s .c  o m*/
    try {
        if (dr.getTemplateFileName() != null) {
            log.info("about to load template file: " + dr.getTemplateFileName()
                    + ", Attemping to find the file directly in the file system.");
            File file = new File(dr.getTemplateFileName());
            if (file.exists()) {
                JasperDesign jdesign = JRXmlLoader.load(file);
                jd = DJJRDesignHelper.downCast(jdesign, dr);
            } else {
                log.info("Not found: Attemping to find the file in the classpath...");
                URL url = DynamicJasperHelper.class.getClassLoader().getResource(dr.getTemplateFileName());
                JasperDesign jdesign = JRXmlLoader.load(url.openStream());
                jd = DJJRDesignHelper.downCast(jdesign, dr);
            }
            DJJRDesignHelper.populateReportOptionsFromDesign(jd, dr);

        } else {
            //Create new JasperDesign from the scratch
            jd = DJJRDesignHelper.getNewDesign(dr);
        }
        jd.setScriptletClass(DJDefaultScriptlet.class.getName()); //Set up scripttlet so that custom expressions can do their magic
        registerParameters(jd, dr);
    } catch (JRException e) {
        throw new CoreException(e.getMessage(), e);
    } catch (IOException e) {
        throw new CoreException(e.getMessage(), e);
    }
    return jd;
}

From source file:de.alpharogroup.lang.PropertiesUtils.java

/**
 * Gives a Properties-object from the given packagepath.
 * /*w  ww.j  a v a  2 s  .com*/
 * @param packagePath
 *            The package-path and the name from the resource as a String.
 * @return The Properties-object from the given packagepath.
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static Properties loadProperties(final String packagePath) throws IOException {
    Properties properties = null;
    final URL url = ClassExtensions.getResource(packagePath);
    if (url != null) {
        properties = new Properties();
        properties.load(url.openStream());
    } else {
        final InputStream is = ClassExtensions.getResourceAsStream(packagePath);
        if (is != null) {
            properties = new Properties();
            properties.load(is);
        }
    }
    return properties;
}

From source file:ch.docbox.elexis.UserDocboxPreferences.java

/**
 * if loginID is prefix with TEST_ we use the tesystem
 * //  ww w .  jav  a 2s. co  m
 * @param loginID
 * @return
 */
public static String getSha1DocboxSecretKey() {
    String docboxSha1SecretKey = "";
    showSha1SecretKey = false;
    if (isDocboxTest()) {
        return CDACHServicesClient.getSHA1("docboxtest");
    }
    URL baseUrl = ch.docbox.ws.cdachservices.CDACHServices_Service.class.getResource("");
    try {
        URL url = new URL(baseUrl + "/product.key");
        InputStream in = url.openStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
        docboxSha1SecretKey = bufferedReader.readLine();
    } catch (Exception e) {
        docboxSha1SecretKey = CDACHServicesClient.getSHA1(CoreHub.mandantCfg.get(USR_SECRETKEY, ""));
        showSha1SecretKey = true;
    }
    return docboxSha1SecretKey;
}

From source file:URLUtil.java

/**
 * Method that tries to get a stream (ideally, optimal one) to read from the
 * specified URL. Currently it just means creating a simple file input stream
 * if the URL points to a (local) file, and otherwise relying on URL classes
 * input stream creation method.//from ww w  .j a va2 s.com
 */
public static InputStream inputStreamFromURL(URL url) throws IOException {
    if ("file".equals(url.getProtocol())) {
        /*
         * As per [WSTX-82], can not do this if the path refers to a network drive
         * on windows. This fixes the problem; might not be needed on all
         * platforms (NFS?), but should not matter a lot: performance penalty of
         * extra wrapping is more relevant when accessing local file system.
         */
        String host = url.getHost();
        if (host == null || host.length() == 0) {
            return new FileInputStream(url.getPath());
        }
    }
    return url.openStream();
}

From source file:edu.cornell.med.icb.R.script.RScript.java

/**
 * Read the script from the specified file. Note: this will first check to see if this
 * script was previously read and stored in the FILENAME_TO_SCRIPT_MAP so the file only
 * has to be read one time. This method runs synchronized so multiple objects
 * can share the FILENAME_TO_SCRIPT_MAP map.
 * NOTE: this will look for the script at scriptFilename then at data/scriptFilename
 * before giving up/*  w w  w  .  j  av  a  2 s. c  o  m*/
 * @param scriptFilename the file to read the R script from
 * @return the String value of the R script (content of file)
 * @throws java.io.IOException error reading the file
 */
private static synchronized String readScript(final String scriptFilename) throws IOException {
    StringBuilder script = FILENAME_TO_SCRIPT_MAP.get(scriptFilename);
    if (script == null) {
        final URL scriptUrl = resourceFinder.findResource(scriptFilename);
        if (scriptUrl == null) {
            throw new IOException("Could not locate R script for filename " + scriptFilename);
        }
        script = new StringBuilder();
        int i = 0;
        for (final String rawLine : new TextFileLineIterator(scriptUrl.openStream())) {
            final String line = rawLine.trim();
            if (StringUtils.isBlank(line) || line.startsWith("#")) {
                continue;
            }
            if (i++ > 0) {
                script.append('\n');
            }
            script.append(line);
        }
        FILENAME_TO_SCRIPT_MAP.put(scriptFilename, script);
    }
    return script.toString();
}

From source file:it.greenvulcano.util.bin.BinaryUtils.java

/**
 * Loads the full content of a file.<br>
 * The file must be reachable via the <i>parent classloader</i>, or at least
 * via the <i>system classloader</i>.<br>
 * The full content of a file is then stored into a <code>byte</code> array.<br>
 * /*from  w ww  .  j a v  a2  s  . c  om*/
 * @param filename
 *        the name of the file to be read
 * @return the full content of the file, stored into a <code>byte</code>
 *         array
 * @throws IOException
 *         if any I/O error occurs
 */
public static byte[] readFileAsBytesFromCP(String filename) throws IOException {
    filename = TextUtils.adjustPath(filename);
    URL url = ClassLoader.getSystemResource(filename);
    if (url == null) {
        throw new IOException("File " + filename + " not found in classpath");
    }

    InputStream in = null;
    try {
        in = url.openStream();
        return inputStreamToBytes(in);
    } finally {
        if (in != null) {
            in.close();
        }
    }
}

From source file:IOUtilities.java

/**
 * Returns an <code>InpuStream</code> for reading the data at the specified
 * URL. If <code>allowCache</code> is <code>true</code>, and the URL is cached,
 * a <code>ByteArrayInpuStream</code> with the cached data is returned.
 *//*  w w w.  java2 s  .co m*/

public static InputStream inputStreamForURL(URL url, boolean allowCache) throws IOException {
    byte[] cached = null;
    if (allowCache)
        cached = (byte[]) urlCache.get(url);
    return cached == null ? url.openStream() : new ByteArrayInputStream(cached);
}