Example usage for org.apache.http.util ByteArrayBuffer append

List of usage examples for org.apache.http.util ByteArrayBuffer append

Introduction

In this page you can find the example usage for org.apache.http.util ByteArrayBuffer append.

Prototype

public void append(int i) 

Source Link

Usage

From source file:com.digitallizard.bbcnewsreader.resource.web.ImageDownloader.java

public static byte[] getImage(URL url) throws Exception {
    URLConnection connection = url.openConnection();

    InputStream stream = connection.getInputStream();
    BufferedInputStream inputbuffer = new BufferedInputStream(stream, 8000);

    ByteArrayBuffer arraybuffer = new ByteArrayBuffer(50);
    int current = 0;
    while ((current = inputbuffer.read()) != -1) {
        arraybuffer.append((byte) current);
    }//from w  w w .  j  a  va2s .  c om

    byte[] image = arraybuffer.toByteArray();

    return image;
}

From source file:com.digitallizard.bbcnewsreader.resource.web.HtmlParser.java

/**
 * @param args//from  w w w.ja v a 2  s.  c o  m
 * @throws IOException
 * @throws ClientProtocolException
 */
public static byte[] getPage(String stringUrl) throws Exception {
    URL url = new URL(stringUrl);
    URLConnection connection = url.openConnection();

    InputStream stream = connection.getInputStream();
    BufferedInputStream inputbuffer = new BufferedInputStream(stream);

    ByteArrayBuffer arraybuffer = new ByteArrayBuffer(50);
    int current = 0;
    while ((current = inputbuffer.read()) != -1) {
        arraybuffer.append((byte) current);
    }
    return arraybuffer.toByteArray();
}

From source file:Main.java

public static byte[] downloadImage(String imageUrl) {
    if (imageUrl.endsWith(".jpg") || imageUrl.endsWith(".bmp") || imageUrl.endsWith(".png")
            || imageUrl.endsWith(".gif")) {
        try {/*from w  w  w.  j a va2 s  .c  o m*/
            URL url = new URL(imageUrl);
            URLConnection urlConn = url.openConnection();
            InputStream is = urlConn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayBuffer baf = new ByteArrayBuffer(50);
            int current = 0;

            while ((current = bis.read()) != -1) {
                baf.append((byte) current);
            }

            return baf.toByteArray();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return null;
}

From source file:com.mondospider.android.lib.LibHTTP.java

public static String get(String url) {
    String ReturnHTML = "";
    //      Log.d("LibHTTP->get->url",url);
    try {/*w w w .  jav a  2  s  . com*/
        URLConnection urlConn = new URL(url).openConnection();
        InputStream is = urlConn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is, 16000);
        ByteArrayBuffer baf = new ByteArrayBuffer(50);
        int current = 0;
        while ((current = bis.read()) != -1) {
            baf.append((byte) current);
        }
        ReturnHTML = new String(baf.toByteArray());
    } catch (Exception e) {
        ReturnHTML = e.getMessage();
    }
    //        Log.d("LibHTTP->get->ReturnHTML", ReturnHTML);
    return ReturnHTML;
}

From source file:fr.asso.vieillescharrues.parseurs.TelechargeFichier.java

/**
 * Mthode statique grant le tlechargement de fichiers
 * @param url Adresse du fichier//from   w w  w.j  a  v  a2s .c o m
 * @param fichierDest Nom du ficher en local
 */
public static void DownloadFromUrl(URL url, String fichierDest) throws IOException {
    File file;
    if (fichierDest.endsWith(".jpg"))
        file = new File(PATH + "images/", fichierDest);
    else
        file = new File(PATH, fichierDest);
    file.getParentFile().mkdirs();
    URLConnection ucon = url.openConnection();

    try {
        tailleDistant = ucon.getHeaderFieldInt("Content-Length", 0); //Rcupre le header HTTP Content-Length
        tailleLocal = (int) file.length();
    } catch (Exception e) {
        e.printStackTrace();
    }
    // Compare les tailles des fichiers
    if ((tailleDistant == tailleLocal) && (tailleLocal != 0))
        return;

    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);
    }
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(baf.toByteArray());
    fos.close();
}

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 a2  s  . 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();
}

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);
    }/*  w  w w. j a va2  s.com*/
    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:it.unicaradio.android.utils.NetworkUtils.java

public static byte[] httpGet(String urlString) throws IOException {
    URL url = new URL(urlString);

    URLConnection ucon = url.openConnection();

    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(50);
    int current = 0;
    while ((current = bis.read()) != -1) {
        baf.append((byte) current);
    }/*  ww  w  .j a  va2 s  .c  om*/

    return baf.toByteArray();
}

From source file:com.apps.android.viish.encyclolpedia.tools.ImageManager.java

private static void downloadAndSaveImageWithPrefix(Context context, String prefix, String fileName)
        throws IOException {
    String stringUrl = BASE_URL + prefix + fileName;
    URL url = new URL(stringUrl);
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    InputStream is = urlConnection.getInputStream();
    BufferedInputStream bis = new BufferedInputStream(is);
    FileOutputStream fos = getFileOutputStream(context, fileName);

    ByteArrayBuffer baf = new ByteArrayBuffer(65535);
    int current = 0;
    while ((current = bis.read()) != -1) {
        baf.append((byte) current);
    }//from ww w  . java 2  s.  com
    fos.write(baf.toByteArray());
    fos.close();
    bis.close();
}

From source file:com.android.bandwidthtest.util.BandwidthTestUtil.java

/**
 * Download a given file from a target url to a given destination file.
 * @param targetUrl the url to download//from   w w w  .  ja v  a 2s  . c om
 * @param file the {@link File} location where to save to
 * @return true if it succeeded
 */
public static boolean DownloadFromUrl(String targetUrl, File file) {
    try {
        URL url = new URL(targetUrl);
        Log.d(LOG_TAG, "Download begining");
        Log.d(LOG_TAG, "Download url:" + url);
        Log.d(LOG_TAG, "Downloaded file name:" + file.getAbsolutePath());
        URLConnection ucon = url.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);
        }
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(baf.toByteArray());
        fos.close();
    } catch (IOException e) {
        Log.d(LOG_TAG, "Failed to download file with error: " + e);
        return false;
    }
    return true;
}