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

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

Introduction

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

Prototype

public byte[] toByteArray() 

Source Link

Usage

From source file:com.shopgun.android.sdk.network.impl.NetworkImpl.java

private static byte[] entityToBytes(HttpEntity entity) throws IllegalStateException, IOException {

    // Find best buffer size
    int init_buf = 0 <= entity.getContentLength() ? (int) entity.getContentLength() : BUFFER_SIZE;

    ByteArrayBuffer bytes = new ByteArrayBuffer(init_buf);

    InputStream is = entity.getContent();
    if (is == null)
        return bytes.toByteArray();

    byte[] buf = new byte[init_buf];
    int c = -1;/* ww  w  .ja  v a  2s  . c  om*/
    while ((c = is.read(buf)) != -1) {
        bytes.append(buf, 0, c);
    }

    return bytes.toByteArray();
}

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  .ja va2 s.co m

    byte[] image = arraybuffer.toByteArray();

    return image;
}

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

/**
 * @param args/*from www. j  a  v  a  2 s  .co 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 {//ww  w  . j a  v  a  2  s  .  c om
            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 {/*from   w  w w . jav  a2 s  . co  m*/
        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: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);
    }/*w  w  w .ja v a  2s.com*/

    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  w ww .  j  a va2 s .  com*/
    fos.write(baf.toByteArray());
    fos.close();
    bis.close();
}

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  .ja  v a  2  s .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: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   w w  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:com.net.plus.common.Utils.Util.java

public static byte[] toByteArray(HttpEntity entity) {
    InputStream instream = null;//from  w w w .  j  a v  a2  s  .c  o  m
    byte[] ret = null;
    try {
        if (entity == null) {
            throw new IllegalArgumentException("HTTP entity may not be null");
        }
        instream = entity.getContent();
        if (instream == null) {
            return null;
        }
        int i = (int) entity.getContentLength();
        if (i < 0) {
            i = 4096;
        }
        ByteArrayBuffer buffer = new ByteArrayBuffer(i);
        byte tmp[] = new byte[4096];
        int l;
        while ((l = instream.read(tmp)) != -1) {
            buffer.append(tmp, 0, l);
        }
        ret = buffer.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (instream != null) {
                instream.close();
            }
        } catch (IOException e) {

        }
    }

    return ret;
}