Example usage for java.net URLConnection getInputStream

List of usage examples for java.net URLConnection getInputStream

Introduction

In this page you can find the example usage for java.net URLConnection getInputStream.

Prototype

public InputStream getInputStream() throws IOException 

Source Link

Document

Returns an input stream that reads from this open connection.

Usage

From source file:com.pwned.utils.VersionCheck.java

private static void _versionCheck() throws IOException {
    URL myURL = new URL(Constants.VERSIONCHECK_URL);
    URLConnection ucon = myURL.openConnection();
    InputStream is = ucon.getInputStream();
    BufferedInputStream bis = new BufferedInputStream(is);
    ByteArrayBuffer baf = new ByteArrayBuffer(50);
    int current = 0;
    while ((current = bis.read()) != -1) {
        baf.append((byte) current);
    }//from  ww  w .ja v  a 2 s .  co  m
    Logger.log("start version check", new String(baf.toByteArray()));
    try {
        currentVersionName = Integer.parseInt(new String(baf.toByteArray()));
    } catch (Exception e) {
        currentVersionName = 1;
    }
    ((Activity) context).runOnUiThread(returnRes);
}

From source file:UpdateMySqlClobServlet.java

public static String getClobsContentAsString(String urlAsString) throws Exception {
    InputStream content = null;/*from   ww w .ja v  a  2s  .  co  m*/
    try {
        java.net.URL url = new java.net.URL(urlAsString);
        java.net.URLConnection urlConn = url.openConnection();
        urlConn.connect();
        content = urlConn.getInputStream();

        int BUFFER_SIZE = 1024;
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        int length;
        byte[] buffer = new byte[BUFFER_SIZE];

        while ((length = content.read(buffer)) != -1) {
            output.write(buffer, 0, length);
        }
        return new String(output.toByteArray());

    } finally {
        content.close();
    }
}

From source file:PropertiesUtil.java

public static Properties load(Properties properties, URL url) throws IOException {
    if (url == null) {
        throw new IllegalArgumentException("Url is not set.");
    } else {//from w ww . j a v a 2 s.  co m
        URLConnection connection = url.openConnection();
        return load(properties, connection.getInputStream());
    }
}

From source file:gdv.xport.util.URLReader.java

private static String read(final URLConnection connection) throws IOException {
    connection.connect();/* ww w . ja va 2 s  .com*/
    InputStream istream = connection.getInputStream();
    BufferedReader in = new BufferedReader(new InputStreamReader(istream));
    try {
        StringBuilder sbuf = new StringBuilder();
        while (in.ready()) {
            String line = in.readLine();
            LOG.debug(line);
            sbuf.append(line);
            sbuf.append("\n");
        }
        return sbuf.toString().trim();
    } finally {
        in.close();
        istream.close();
    }
}

From source file:com.baidu.rigel.biplatform.parser.util.PropertiesUtil.java

public static Properties loadPropertiesFromFile(String file) throws IOException {
    String location = file;/*w w w . j  a v  a  2  s .  c o m*/
    if (StringUtils.isBlank(location)) {
        throw new IllegalArgumentException("file can not be blank.");
    }
    Properties properties = new Properties();

    ClassLoader classLoaderToUse = Thread.currentThread().getContextClassLoader();

    URL url = classLoaderToUse != null ? classLoaderToUse.getResource(location)
            : ClassLoader.getSystemResource(location);

    URLConnection con = url.openConnection();
    InputStream is = con.getInputStream();
    try {
        if (location != null && location.endsWith(".xml")) {
            properties.loadFromXML(is);
        } else {
            properties.load(is);
        }
    } finally {
        IOUtils.close(con);
        IOUtils.closeQuietly(is);
    }

    return properties;
}

From source file:com.baidu.rigel.biplatform.parser.util.PropertiesUtil.java

/** 
 * loadPropertiesFromCurrent/*w  w  w .  j  a v a  2s  . c o  m*/
 * @param resource
 * @return
 * @throws IOException
 */
public static Properties loadPropertiesFromPath(String resource) throws IOException {
    String location = resource;
    if (StringUtils.isBlank(location)) {
        location = "conf/default.properties";
    }
    Properties properties = new Properties();

    ClassLoader classLoaderToUse = Thread.currentThread().getContextClassLoader();

    URL url = classLoaderToUse != null ? classLoaderToUse.getResource(location)
            : ClassLoader.getSystemResource(location);

    URLConnection con = url.openConnection();
    InputStream is = con.getInputStream();
    try {
        if (location != null && location.endsWith(".xml")) {
            properties.loadFromXML(is);
        } else {
            properties.load(is);
        }
    } finally {
        IOUtils.close(con);
        IOUtils.closeQuietly(is);
    }

    return properties;
}

From source file:de.ifgi.mosia.wpswfs.Util.java

public static Set<String> readConfigFilePerLine(String resourcePath) throws IOException {
    URL resURL = Util.class.getResource(resourcePath);
    URLConnection resConn = resURL.openConnection();
    resConn.setUseCaches(false);//from  w w w.  ja va2  s . c o  m
    InputStream contents = resConn.getInputStream();

    Scanner sc = new Scanner(contents);
    Set<String> result = new HashSet<String>();
    String line;
    while (sc.hasNext()) {
        line = sc.nextLine();
        if (line != null && !line.isEmpty() && !line.startsWith("#")) {
            result.add(line.trim());
        }
    }
    sc.close();

    return result;
}

From source file:net.daboross.bukkitdev.skywars.gist.GistReport.java

public static String readConnection(URLConnection connection) throws IOException {
    try (InputStream inputStream = connection.getInputStream()) {
        try (Reader reader = new InputStreamReader(inputStream, "UTF-8")) {
            StringBuilder result = new StringBuilder();
            char[] buffer = new char[128];
            int length;
            while ((length = reader.read(buffer)) > 0) {
                result.append(buffer, 0, length);
            }//from  w ww.  j  ava2 s .co  m
            return result.toString();
        }
    }
}

From source file:Main.java

public static Bitmap tryToGetBitmapFromInternet(String bitmapUrl, Context context, int imageSize) {
    if (null != bitmapUrl) {
        InputStream inputStream = null;
        try {//w w  w . java 2 s.  co m
            URL url = new URL(bitmapUrl);

            URLConnection connection = url.openConnection();

            connection.connect();

            inputStream = connection.getInputStream();
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int read;

            while ((read = inputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, read);
            }
            inputStream.close();
            byteArrayOutputStream.close();

            buffer = byteArrayOutputStream.toByteArray();

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;

            BitmapFactory.decodeByteArray(buffer, 0, buffer.length, options);

            int maxSize = Math.max(options.outWidth, options.outHeight);
            float newImageScale = 1f;
            if (-1 != imageSize) {
                newImageScale = maxSize / (imageSize * context.getResources().getDisplayMetrics().density);
            }

            options.inJustDecodeBounds = false;
            options.inSampleSize = Math.round(newImageScale);

            return BitmapFactory.decodeByteArray(buffer, 0, buffer.length, options);
        } catch (Throwable e) {
            // pass
        } finally {
            if (null != inputStream) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    // pass
                }
                inputStream = null;
            }
            System.gc();
        }
    }

    return null;
}

From source file:subhan.portal.config.Util.java

public static void downloadFromUrl(String downloadUrl, String fileName) throws IOException {
    File root = android.os.Environment.getExternalStorageDirectory(); // path ke sdcard

    File dir = new File(root.getAbsolutePath() + "/youread"); // path ke folder

    if (dir.exists() == false) { // cek folder eksistensi
        dir.mkdirs(); // kalau belum ada, dibuat
    }//from w  w  w .j  a v a2s  .  c o m

    URL url = new URL(downloadUrl); // you can write here any link
    File file = new File(dir, fileName);

    // Open a connection to that URL. 
    URLConnection ucon = url.openConnection();

    // Define InputStreams to read from the URLConnection. 
    InputStream is = ucon.getInputStream();
    BufferedInputStream bis = new BufferedInputStream(is);

    // Read bytes to the Buffer until there is nothing more to read(-1).
    ByteArrayBuffer baf = new ByteArrayBuffer(5000);
    int current = 0;
    while ((current = bis.read()) != -1) {
        baf.append((byte) current);
    }

    // Convert the Bytes read to a String. 
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(baf.toByteArray());
    fos.flush();
    fos.close();
}