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:dk.clanie.io.IOUtil.java

/**
 * Calculates SHA1 for the contents referred to by the supplied URL.
 * // w  w w  . j a v a2s .co  m
 * @param url URL
 * @return String with SHA1 calculated from the contents referred to by <code>url</code>.
 * 
 * @throws RuntimeIOException
 */
public static String sha1(URL url) {
    InputStream is = null;
    try {
        is = url.openStream();
        return sha1(is);
    } catch (IOException ioe) {
        log.warn("Error occourred while reading from URL: " + url, ioe);
        throw new RuntimeIOException(ioe);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException ioe) {
                log.warn("Failed to close stream for URL: " + url, ioe);
                throw new RuntimeIOException(ioe);
            }
        }
    }
}

From source file:eu.europa.ec.markt.dss.validation.crl.OnlineCRLSource.java

private static X509CRL downloadCRLFromURL(String crlURL) throws DSSException {

    InputStream crlStream = null;
    try {//w  w w . ja va2  s .c  o  m

        final URL url = new URL(crlURL);
        crlStream = url.openStream();
        return DSSUtils.loadCRL(crlStream);
    } catch (Exception e) {

        LOG.warning(e.getMessage());
    } finally {
        IOUtils.closeQuietly(crlStream);
    }
    return null;
}

From source file:common.DBHelper.java

private static String[] readSqlStatements(URL url) {
    try {//  www  . ja v  a  2  s  . co  m
        char buffer[] = new char[256];
        StringBuilder result = new StringBuilder();
        InputStreamReader reader = new InputStreamReader(url.openStream(), "UTF-8");
        while (true) {
            int count = reader.read(buffer);
            if (count < 0) {
                break;
            }
            result.append(buffer, 0, count);
        }
        return result.toString().split(";");
    } catch (IOException ex) {
        throw new RuntimeException("Cannot read " + url, ex);
    }
}

From source file:hm.binkley.util.ServiceBinder.java

private static <T> BufferedReader config(final Class<T> service, final URL config) {
    try {//from ww  w .  java2 s .com
        return new BufferedReader(new InputStreamReader(config.openStream(), UTF8));
    } catch (final IOException e) {
        return fail(service, config, "Cannot read service configuration", e);
    }
}

From source file:com.codename1.tools.javadoc.sourceembed.javadocsourceembed.Main.java

public static void processFile(File javaSourceFile, File javaDestFile) throws Exception {
    System.out.println("JavaSource Processing: " + javaSourceFile.getName());
    List<String> lines = Files.readAllLines(Paths.get(javaSourceFile.toURI()), CHARSET);
    for (int iter = 0; iter < lines.size(); iter++) {
        String l = lines.get(iter);
        int position = l.indexOf("<script src=\"https://gist.github.com/");
        if (position > -1) {
            String id = l.substring(position + 39);
            id = id.split("/")[1];
            id = id.substring(0, id.indexOf('.'));
            String fileContent = gistCache.get(id);
            if (fileContent != null) {
                lines.add(iter + 1, fileContent);
                iter++;/*from www . j  av a  2 s.c om*/
                continue;
            }

            URL u = new URL("https://api.github.com/gists/" + id + "?client_id=" + CLIENT_ID + "&client_secret="
                    + CLIENT_SECRET);
            try (BufferedReader br = new BufferedReader(new InputStreamReader(u.openStream(), CHARSET))) {
                String jsonText = br.lines().collect(Collectors.joining("\n"));
                JSONObject json = new JSONObject(jsonText);
                JSONObject filesObj = json.getJSONObject("files");
                String str = "";
                for (String k : filesObj.keySet()) {
                    JSONObject jsonFileEntry = filesObj.getJSONObject(k);
                    // breaking line to fix the problem with a blank space on the first line
                    String current = "\n" + jsonFileEntry.getString("content");
                    str += current;
                }
                int commentStartPos = str.indexOf("/*");
                while (commentStartPos > -1) {
                    // we just remove the comment as its pretty hard to escape it properly
                    int commentEndPos = str.indexOf("*/");
                    str = str.substring(commentStartPos, commentEndPos + 1);
                    commentStartPos = str.indexOf("/*");
                }

                // allows things like the @Override annotation
                str = "<noscript><pre>{@code " + str.replace("@", "{@literal @}") + "}</pre></noscript>";
                gistCache.put(id, str);
                lines.add(iter + 1, str);
                iter++;
            }
        }
    }
    try (BufferedWriter fw = new BufferedWriter(new FileWriter(javaDestFile))) {
        for (String l : lines) {
            fw.write(l);
            fw.write('\n');
        }
    }
}

From source file:com.comcast.magicwand.spells.web.chrome.ChromePhoenixDriver.java

/**
 * Gets the latest known version of chromedriver.
 * /*w  w  w. j  av  a2  s  .  c om*/
 * @return Latest known version
 */
public static String getLatestVersion() {
    String latest = LATEST_KNOWN_VERSION;

    try {
        URL url = new URL(LATEST_RELEASE_URL);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        IOUtils.copy(url.openStream(), baos);
        latest = baos.toString();
    } catch (IOException e) {
        LOG.error("Error retrieving url[{}]: {}", LATEST_RELEASE_URL, e);
    }
    return latest;
}

From source file:com.vmware.qe.framework.datadriven.impl.supplier.XMLDataParser.java

private static void validateAgainstSchema(URL dataFileURL) throws Exception {
    URL xsdURL = XMLDataParser.class.getResource(XSD_FILE_NAME);
    if (xsdURL == null) {
        log.error("Schema file not found!");
    } else {/*from  w w  w.  j a  va2s .c o m*/
        XMLUtil.validateXML(dataFileURL.openStream(), xsdURL.openStream());
    }
}

From source file:de.Keyle.MyPet.util.Util.java

public static String readUrlContent(String address) throws IOException {
    StringBuilder contents = new StringBuilder(2048);
    BufferedReader br = null;/*  w w w.  j  ava 2s  .  c o  m*/

    try {
        URL url = new URL(address);
        br = new BufferedReader(new InputStreamReader(url.openStream()));
        String line;
        while ((line = br.readLine()) != null) {
            contents.append(line);
        }
    } finally {
        try {
            if (br != null) {
                br.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            DebugLogger.printThrowable(e);
        }
    }
    return contents.toString();
}

From source file:eionet.gdem.conversion.datadict.DataDictUtil.java

public static Map<String, DDElement> importDDElementSchemaDefs(Map<String, DDElement> elemDefs,
        String schemaUrl) {/* ww  w. ja  v  a  2  s.  co  m*/
    InputStream inputStream = null;
    if (elemDefs == null) {
        elemDefs = new HashMap<String, DDElement>();
    }

    try {
        IXmlCtx ctx = new XmlContext();
        URL url = new URL(schemaUrl);
        inputStream = url.openStream();
        ctx.checkFromInputStream(inputStream);

        IXQuery xQuery = ctx.getQueryManager();
        List<String> elemNames = xQuery.getSchemaElements();
        for (int i = 0; i < elemNames.size(); i++) {
            String elemName = elemNames.get(i);
            DDElement element = elemDefs.containsKey(elemName) ? elemDefs.get(elemName)
                    : new DDElement(elemName);
            element.setSchemaDataType(xQuery.getSchemaElementType(elemName));
            elemDefs.put(elemName, element);
        }
    } catch (Exception ex) {
        LOGGER.error("Error reading schema file ", ex);
    } finally {
        try {
            inputStream.close();
        } catch (Exception e) {
        }
    }
    return elemDefs;

}

From source file:com.moadbus.banking.iso.core.protocol.ConfigParser.java

/** Creates a message factory from the file located at the specified URL. */
public static MessageFactory createFromUrl(URL url) throws IOException {
    if (msgFactory == null) {
        msgFactory = new MessageFactory();
        InputStream stream = url.openStream();
        try {/*from  w w w. java 2 s  .  c  o  m*/
            parse(msgFactory, stream);
        } finally {
            stream.close();
        }
    }
    return msgFactory;
}