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 byte[] getDigestFromURL(URL u) throws Exception {
    MessageDigest md5 = MessageDigest.getInstance("MD5");
    InputStream in = u.openStream();
    byte[] data = new byte[1024];
    int bytesRead = -1;
    while ((bytesRead = in.read(data)) >= 0) {
        md5.update(data, 0, bytesRead);/*from w w w. j  ava2s . c  o m*/
    }
    return md5.digest();
}

From source file:Main.java

public static Bitmap getBitmap(String s) {
    Bitmap bitmap = null;//from  ww w  . jav  a2 s . com
    try {
        URL url = new URL(s);
        bitmap = BitmapFactory.decodeStream(url.openStream());
    } catch (Exception e) {
        // TODO Auto-generated catch block   
        e.printStackTrace();
    }

    return bitmap;
}

From source file:WBSTest.java

private static void initTorque() {
    try {//from   w w  w .  j  ava 2  s. c  o  m
        PropertiesConfiguration tcfg = new PropertiesConfiguration();
        System.out.println(WBSTest.class.getResource("/Torque.properties"));
        URL torqueURL = WBSTest.class.getResource("/Torque.properties");
        InputStream in = torqueURL.openStream();
        tcfg.load(in);
        in.close();
        Torque.init(tcfg); // Now really do it: initialize Torque

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static Bitmap getBitmapFromUrl(String imageUrl) {
    Bitmap bmp = null;//from   ww w  . j a  va2 s  .  co m
    try {
        if (!TextUtils.isEmpty(imageUrl) && imageUrl.startsWith("http")) {
            URL url = new URL(imageUrl);
            bmp = BitmapFactory.decodeStream(url.openStream());
        } else {
            bmp = BitmapFactory.decodeFile(imageUrl);
        }
    } catch (Throwable ex) {

    }
    return bmp;
}

From source file:net.landora.justintv.JustinTVAPI.java

private static InputStream openURL(String url) throws IOException {
    URL urlObject = new URL(url);
    return urlObject.openStream();
}

From source file:com.google.infrastructuredmap.MapAndMarkdownExtractorMain.java

private static InputStream openStream(String path) throws IOException {
    if (path.startsWith("http:") || path.startsWith("https:")) {
        URL url = new URL(path);
        return new BufferedInputStream(url.openStream());
    }//from   w  ww.java2 s  .  co m
    return new BufferedInputStream(new FileInputStream(path));
}

From source file:Main.java

public static Document downloadXML(URL url) throws IOException, SAXException {
    return getXML(url.openStream());
}

From source file:Main.java

/**
 * Generate an input stream reading from an android URI
 * /*from  w  w  w.  j  a  va2 s. c  om*/
 * @param uri
 * @return
 * @throws IOException
 */
public static InputStream getFromURI(Context context, Uri uri) throws IOException {

    if (uri.getScheme().equals("content"))
        return context.getContentResolver().openInputStream(uri);
    else if (uri.getScheme().equals("file")) {
        URL url = new URL(uri.toString());

        return url.openStream();
    } else {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(uri.toString());
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        if (entity != null)
            return entity.getContent();
        else
            throw new IOException("No HTTP response");
        // Use the regular java stuff
        // URL url = new URL(uri.toString());

        // return url.openStream();
    }
}

From source file:Main.java

/**
 * StAX parse XML resource into XMLStreamReader
 * @return XMLStreamReader//from w w w .  j av  a  2  s  . c  om
 * @throws IOException
 * @throws XMLStreamException
 */
public static XMLStreamReader parse(URL url) throws IOException, XMLStreamException {
    InputStream input = url.openStream();
    XMLInputFactory xif = XMLInputFactory.newInstance();
    XMLStreamReader xmlStreamReader = xif.createXMLStreamReader(input);

    return xmlStreamReader;
}

From source file:Util.java

public static void download(URL url, File to) throws IOException {
    BufferedInputStream in = new BufferedInputStream(url.openStream());
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(to));
    int bit = -1;
    while ((bit = in.read()) != -1) {
        out.write(bit);// w  w w.j  a  v  a  2 s . c  om
    }
    in.close();
    out.close();
}