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:Main.java

public static void parse(final XMLReader xmlReader, final URL url) throws IOException, SAXException {
    final InputStream inputStream = url.openStream();
    try {//w w  w.j  av  a  2s . com
        final InputSource inputSource = new InputSource(inputStream);
        xmlReader.parse(inputSource);
    } finally {
        inputStream.close();
    }

}

From source file:cz.autoclient.github.json.GitHubJson.java

public static JSONObject fromURL(URL url) {
    Scanner s;/*from w  ww . ja  va  2 s.c  o  m*/
    try {
        s = new Scanner(url.openStream(), "UTF-8");
    } catch (IOException ex) {
        throw new IllegalArgumentException("JSON file not found at " + url);
    }
    s.useDelimiter("\\A");
    if (s.hasNext()) {
        String out = s.next();
        try {
            return new JSONObject(out);
        } catch (JSONException ex) {
            throw new IllegalArgumentException(
                    "Invalid JSON contents at " + url + " - \n         JSON error:" + ex);
            //Logger.getLogger(DataLoader.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else
        throw new IllegalArgumentException("JSON file not found at " + url);
}

From source file:Main.java

/**
 * Reads the XML document and returns it as a {@link String}.
 * //from   w  ww .  j  av a 2  s . co m
 * @param url the URL of the XML document
 * 
 * @return the XML document, as a {@link String}
 * 
 * @throws IOException
 */
public static String readXmlDocument(URL url) throws IOException {
    Reader in = new InputStreamReader(url.openStream());
    CharArrayWriter out = new CharArrayWriter();
    FileCopyUtils.copy(in, out);
    return out.toString();
}

From source file:de.dennishoersch.web.css.images.resolver.HttpPathResolver.java

private static Path downloadToTmp(String url) throws IOException {
    try {/*from   ww  w .  j  a v  a 2  s. c  o m*/
        URL weburl = new URL(url);
        byte[] bytes = IOUtils.toByteArray(weburl.openStream());

        Path tempFile = Files.createTempFile("__" + HttpPathResolver.class.getSimpleName() + "_", "");
        Files.write(tempFile, bytes);

        return tempFile;
    } catch (IllegalArgumentException e) {
        logger.log(Level.WARNING, "url of wrong format: '" + url + "'!", e);
        throw e;
    }
}

From source file:Main.java

public static Object unmarshall(String cntxtPkg, URL url) throws JAXBException, SAXException, IOException {
    InputStream is = url.openStream();
    return unmarshall(cntxtPkg, new BufferedInputStream(is));
}

From source file:Main.java

public static Document createDocument(String uri) throws Exception {
    // handler.clear();
    URL url = new URL(uri);
    synchronized (lock) {
        return builder.parse(url.openStream());
    }//  w  w  w  .ja  v  a 2 s . co m
}

From source file:Main.java

private static void getDocument(String xslName) {
    try {//from   w ww  .  j a v  a  2  s .  c  o m
        URL xslUri = new URL(xslName);
        InputStream xslIs = xslUri.openStream();
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        xsl = dBuilder.parse(xslIs);
        xsl.getDocumentElement().normalize();
    } catch (ParserConfigurationException ex) {
        throw new RuntimeException(ex);
    } catch (SAXException ex) {
        throw new RuntimeException(ex);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }

}

From source file:Main.java

public static String createShortUrl(String urlStr) throws Exception {
    URL url = new URL("http://is.gd/create.php?format=simple&url=" + urlStr);
    return createBuffReader(url.openStream()).readLine();
}

From source file:Main.java

/**
 * @param url//from   ww w .  jav  a  2s .co m
 * @return the xml string at that url
 */
public static String getXMLString(URL url) {
    try {
        InputStream in = url.openStream();

        StringBuilder xml = new StringBuilder();
        byte[] buffer = new byte[512];
        int readen = 0;
        while ((readen = in.read(buffer)) != -1) {
            xml.append(new String(buffer, 0, readen));
        }

        String doc = xml.toString();

        if (doc.indexOf(buggyChar) != -1) {
            doc = doc.replace(buggyChar, ' ');
        }

        return doc;
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:br.usp.poli.lta.cereda.macro.util.CommonUtils.java

/**
 * Obtm o contedo a partir do caminho informado.
 * @param path Caminho a ser pesquisado.
 * @return Contedo do caminho informado.
 * @throws TextRetrievalException Um erro ocorreu ao obter o arquivo.
 *//*from ww w  .j a v  a  2s  .co m*/
public static String get(String path) throws TextRetrievalException {

    try {

        // cria uma nova URL e obtm o fluxo de bytes
        URL url = new URL(path);
        InputStream stream = url.openStream();

        // cria um arquivo temporrio, copia o fluxo de bytes para ele, l
        // o contedo e remove o arquivo temporrio
        File temp = new File(
                System.getProperty("user.home").concat(java.io.File.separator).concat("get-tmp.txt"));
        FileUtils.copyInputStreamToFile(stream, temp);
        String output = FileUtils.readFileToString(temp, Charset.forName("UTF-8"));
        temp.delete();

        // retorna o contedo
        return output;

    } catch (MalformedURLException mue) {

        // a URL  invlida, lanar exceo
        throw new TextRetrievalException("A URL informada  invlida.");
    } catch (IOException ioe) {

        // o documento no foi encontrado, lanar exceo
        throw new TextRetrievalException("O documento informado na URL no foi encontrado.");
    }

}