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:info.magnolia.cms.util.ClasspathResourcesUtil.java

/**
 * Checks last modified and returns the new content if changed and the cache flag is not set to true.
 *
 * @param name/*from   w w  w.j  ava  2s  .co m*/
 * @return the input stream
 * @throws IOException
 */
public static InputStream getStream(String name, boolean cache) throws IOException {
    if (cache) {
        return ClasspathResourcesUtil.class.getResourceAsStream(name);
    } else {
        // TODO use the last modified attribute
        URL url = ClasspathResourcesUtil.class.getResource(name);
        if (url != null) {
            return url.openStream();
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("Can't find {}", name);
    }
    return null;
}

From source file:uk.dsxt.voting.common.utils.PropertiesHelper.java

private static Properties loadPropertiesFromPath(String moduleName, URL propertiesURL) {
    Properties properties = new Properties();
    if (propertiesURL != null) {
        try (InputStream resourceStream = propertiesURL.openStream()) {
            properties.load(resourceStream);
            log.info("Loading {} properties from file: {}", moduleName.toUpperCase(), propertiesURL);
        } catch (Exception e) {
            log.error(String.format("Couldn't load %s properties from file: %s", moduleName.toUpperCase(),
                    propertiesURL), e);/*from  ww w .j a va2 s  .c om*/
        }
    } else {
        log.info("Couldn't find {} properties file", moduleName.toUpperCase());
    }
    return properties;
}

From source file:com.intuit.autumn.utils.PropertyFactory.java

private static Properties readZip(final Class base, final URL jar, final String property,
        final Properties properties) {
    try (ZipInputStream zip = new ZipInputStream(jar.openStream())) {
        for (ZipEntry ze = zip.getNextEntry(); ze != null; ze = zip.getNextEntry()) {
            if (ze.getName().equals(property)) {
                properties.load(zip);//from w w  w  .  j  a  va  2 s. co m

                break;
            }
        }
    } catch (IOException e) {
        LOGGER.warn("unable to read jar: {}, property: {}, class: {}, cause: {}",
                new Object[] { jar, property, base.getSimpleName(), e.getMessage() }, e);
    }

    return properties;
}

From source file:com.tonbeller.wcf.utils.ObjectFactory.java

public static Object instance(URL rulesXml, URL configXml) throws SAXException, IOException {

    Digester digester = DigesterLoader.createDigester(rulesXml);
    digester.setValidating(false);//from  w w w  .  j  ava  2  s .co m

    ObjectHolder root = new ObjectHolder();
    digester.push(root);

    digester.parse(configXml.openStream());
    return root.getObject();
}

From source file:com.indexoutofbounds.util.ClassPathInputStreamUtils.java

/**
 * Creates an InputStream from the path which can be either something on
 * the file system, a remote system, or a classpath entry.
 * @param path//from www . ja v a  2  s  . c o m
 * @return
 * @throws Exception
 */
public final static InputStream getInputStream(final String path) throws IOException {

    if (StringUtils.isEmpty(path)) {
        throw new IOException("Unable to locate empty config file parameter");
    }

    final URL url = locateURL(path);
    if (url == null) {
        throw new IOException("Unable to locate config file: " + path);
    }

    try {

        return new BufferedInputStream(url.openStream());
    } catch (IOException e) {
        throw new IOException("Unable to open config file: " + path + ", " + e.getLocalizedMessage());
    }
}

From source file:com.meltmedia.rodimus.RodimusCli.java

public static StreamSource createStreamSource(URL url) throws IOException {
    StreamSource source = new StreamSource();
    source.setSystemId(url.toExternalForm());
    source.setInputStream(url.openStream());
    return source;
}

From source file:com.siphyc.utils.Utilities.java

public static byte[] getFileBytes(String url) {

    InputStream stream = null;//from  w w  w.  j  a  va 2 s  .  co  m
    try {
        URL resource = getResource(url);
        stream = resource.openStream();
        return IOUtils.toByteArray(stream);

    } catch (IOException ex) {
        logger.error("<>shit hit the fan !" + ex);
    } finally {
        try {
            stream.close();
        } catch (NullPointerException | IOException ex) {

            logger.error("<>the stream might have been null ..." + ex);
        }
    }
    return null;
}

From source file:com.htmlhifive.tools.codeassist.core.proposal.wrapper.ProposalWrapperUtils.java

/**
 * ?CSS??./*w  w w.j a v a2s  .c o  m*/
 * 
 * @return CSS.
 */
public static String getCSSStyles() {

    Bundle bundle = Platform.getBundle(JavaScriptPlugin.getPluginId());
    URL url = bundle.getEntry("/JavadocHoverStyleSheet.css"); //$NON-NLS-1$
    if (url != null) {
        BufferedReader reader = null;
        try {
            url = FileLocator.toFileURL(url);
            return IOUtils.toString(url.openStream());
        } catch (IOException ex) {
            JavaScriptPlugin.log(ex);
        } finally {
            IOUtils.closeQuietly(reader);
        }
    }
    return null;
}

From source file:com.xerox.amazonws.ec2.EC2Utils.java

/**
 * This method makes a best effort to fetch a piece of instance metadata.
 *
 * @param key the name of the metadata to fetch
 * @return value of the metadata item//  w  w w  . ja v a  2s .  c o m
 */
public static String getInstanceUserdata() throws IOException {
    int retries = 0;
    while (true) {
        try {
            URL url = new URL("http://169.254.169.254/latest/user-data/");
            InputStreamReader rdr = new InputStreamReader(url.openStream());
            StringWriter wtr = new StringWriter();
            char[] buf = new char[1024];
            int bytes;
            while ((bytes = rdr.read(buf)) > -1) {
                if (bytes > 0) {
                    wtr.write(buf, 0, bytes);
                }
            }
            rdr.close();
            return wtr.toString();
        } catch (IOException ex) {
            if (retries == 5) {
                logger.debug("Problem getting user data, retries exhausted...");
                return null;
            } else {
                logger.debug("Problem getting user data, retrying...");
                try {
                    Thread.sleep((int) Math.pow(2.0, retries) * 1000);
                } catch (InterruptedException e) {
                }
            }
        }
    }
}

From source file:com.xerox.amazonws.ec2.EC2Utils.java

/**
 * This method makes a best effort to fetch a piece of instance metadata.
 *
 * @param key the name of the metadata to fetch
 * @return value of the metadata item/*from  ww w  .  ja  v  a 2  s.  co  m*/
 */
public static String getInstanceMetadata(String key) throws IOException {
    int retries = 0;
    String value = null;
    while (true) {
        try {
            URL url = new URL("http://169.254.169.254/latest/meta-data/" + key);
            value = new BufferedReader(new InputStreamReader(url.openStream())).readLine();
            return value;
        } catch (IOException ex) {
            if (retries == 5) {
                logger.debug("Problem getting instance data, retries exhausted...");
                logger.debug("value = " + value);
                return null;
            } else {
                logger.debug("Problem getting instance data, retrying...");
                try {
                    Thread.sleep((int) Math.pow(2.0, retries) * 1000);
                } catch (InterruptedException e) {
                }
            }
        }
    }
}