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:org.nhnnext.android.androidnaming.ImageDownload.java

public void copy_img(String url, String save_name) {

    File img_cache_path;//w ww. ja  va 2s . co m

    img_cache_path = new File(HomeView.FILES_DIR + save_name);
    if (!img_cache_path.exists()) {

        // ************************************
        try {
            URL Url = new URL(url);
            URLConnection conn = Url.openConnection();
            conn.connect();
            InputStream is = conn.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 = context.openFileOutput(save_name, 0);
            fos.write(baf.toByteArray());
            fos.close();
        } catch (IOException e) {

        }

    }

}

From source file:org.nhnnext.android.day3.Img_Downloader.java

public void copy_img(String url, String save_name) {

    File img_cache_path;/*from   ww  w  . ja  v a  2  s .co m*/

    img_cache_path = new File(MainActivity.FILES_DIR + save_name);
    if (!img_cache_path.exists()) {

        // ************************************
        try {
            URL Url = new URL(url);
            URLConnection conn = Url.openConnection();
            conn.connect();
            InputStream is = conn.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 = context.openFileOutput(save_name, 0);
            fos.write(baf.toByteArray());
            fos.close();
        } catch (IOException e) {

        }

    }

}

From source file:org.nhnnext.android.day5_simple.ImageDownloader.java

public void copy_img(String url, String save_name) {

    File img_cache_path;/*from  w  w w . jav  a  2s  .c o  m*/

    img_cache_path = new File(NextgramController.FILES_DIR + save_name);
    if (!img_cache_path.exists()) {

        // ************************************
        try {
            URL Url = new URL(url);
            URLConnection conn = Url.openConnection();
            conn.connect();
            InputStream is = conn.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 = context.openFileOutput(save_name, 0);
            fos.write(baf.toByteArray());
            fos.close();
        } catch (IOException e) {

        }

    }

}

From source file:org.nhnnext.android.basic.ImageDownload.java

public void copy_img(String url, String save_name) {

    File img_cache_path;/*from   ww w  .j  ava2s.c o m*/

    img_cache_path = new File(pref.getString(context.getString(R.string.files_directory), "") + save_name);
    if (!img_cache_path.exists()) {

        // ************************************
        try {
            URL Url = new URL(url);
            URLConnection conn = Url.openConnection();
            conn.connect();
            InputStream is = conn.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 = context.openFileOutput(save_name, 0);
            fos.write(baf.toByteArray());
            fos.close();
        } catch (IOException e) {

        }

    }

}

From source file:com.github.matthesrieke.jprox.JProxViaParameterServlet.java

private byte[] readInputStream(InputStream inputStream, int estimateLength) throws IOException {
    ByteArrayBuffer bb = new ByteArrayBuffer(estimateLength);
    int b;//from  w  w w .  j  ava  2s  .c o  m
    while ((b = inputStream.read()) != -1) {
        bb.append(b);
    }
    inputStream.close();
    return bb.toByteArray();
}

From source file:fr.gcastel.freeboxV6GeekIncDownloader.services.GeekIncLogoDownloadService.java

public void downloadAndResize(int requiredSize) throws Exception {
    // Si le logo existe depuis moins d'une semaine, on ne le retlcharge
    // pas//from  ww  w  . j  a v  a2s.  c o  m
    if (outFile.exists()) {
        // Moins une semaine
        long uneSemainePlusTot = System.currentTimeMillis() - (7 * 24 * 60 * 60 * 1000);

        if (outFile.lastModified() > uneSemainePlusTot) {
            Log.i("GeekIncLogoDownloadService",
                    "Le logo a dj t tlcharg il y a moins d'une semaine.");
            return;
        }
    }

    URLConnection ucon = toDownload.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);
    }

    bis.close();
    is.close();

    // Fichier temporaire pour redimensionnement
    File tmpFile = new File(outFile.getAbsolutePath() + "tmp");
    FileOutputStream fos = new FileOutputStream(tmpFile);
    fos.write(baf.toByteArray());
    fos.close();

    saveAndResizeFile(tmpFile, outFile, requiredSize);

    // Suppression du fichier temporaire
    tmpFile.delete();
}

From source file:com.zoffcc.applications.aagtl.ImageManager.java

public void DownloadFromUrl(String imageURL, String fileName) { // this is
                                                                // the downloader method
    try {//from   w w w . ja  va2  s . c  om
        URL url = new URL(imageURL);
        File file = new File(fileName);

        //long startTime = System.currentTimeMillis();
        //Log.d("ImageManager", "download begining");
        //Log.d("ImageManager", "download url:" + url);
        //Log.d("ImageManager", "downloaded file name:" + fileName);
        /* Open a connection to that URL. */
        URLConnection ucon = url.openConnection();
        ucon.setConnectTimeout(10000);
        ucon.setReadTimeout(7000);

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

        /*
         * Read bytes to the Buffer until there is nothing more to read(-1).
         */
        ByteArrayBuffer baf = new ByteArrayBuffer(HTMLDownloader.default_buffer_size);
        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.close();
        //Log.d("ImageManager", "download ready in"
        //      + ((System.currentTimeMillis() - startTime) / 1000)
        //      + " sec");

    } catch (SocketTimeoutException e2) {
        Log.d("ImageManager", "Connectiont timout: " + e2);
    } catch (IOException e) {
        Log.d("ImageManager", "Error: " + e);
    } catch (Exception e) {
        Log.d("ImageManager", "Error: " + e);
    }

}

From source file:cz.tomas.StockAnalyze.utils.DownloadService.java

public byte[] DownloadFromUrl(String downloadUrl, boolean compress) throws IOException {
    InputStream is = null;/*from w ww.  j  a  v  a2  s  .  c  o m*/
    try {
        URL url = new URL(downloadUrl);

        long startTime = System.currentTimeMillis();
        if (Utils.DEBUG)
            Log.d(Utils.LOG_TAG, "DownloadService: download url:" + url);
        /* Open a connection to the URL. */
        is = openHttpConnection(downloadUrl);

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

        if (Utils.DEBUG)
            Log.d(Utils.LOG_TAG, "DownloadService: download finished in"
                    + ((System.currentTimeMillis() - startTime) / 1000) + " sec");

        return baf.toByteArray();
    } catch (IOException e) {
        Log.d(Utils.LOG_TAG, "DownloadService: Error: " + e);
        throw e;
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

From source file:jp.co.brilliantservice.android.writertdtext.HomeActivity.java

/**
 * RTD Text Record??NdefMessage????//from  ww w  . ja va2s.c  o  m
 * 
 * @param text 
 * @param languageCode (ISO/IANA)
 * @return
 */
private NdefMessage createTextMessage(String text, String languageCode) {
    try {
        byte statusByte = (byte) languageCode.length();
        byte[] rawLanguageCode = languageCode.getBytes("US-ASCII");
        byte[] rawText = text.getBytes("UTF-8");

        ByteArrayBuffer buffer = new ByteArrayBuffer(1 + rawLanguageCode.length + rawText.length);
        buffer.append(statusByte);
        buffer.append(rawLanguageCode, 0, rawLanguageCode.length);
        buffer.append(rawText, 0, rawText.length);

        byte[] payload = buffer.toByteArray();
        NdefMessage message = new NdefMessage(new NdefRecord[] {
                new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], payload) });
        return message;
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

From source file:de.luhmer.owncloudnewsreader.async_tasks.GetImageThreaded.java

@Override
public void run() {
    try {//from ww  w  . jav a2  s. c om
        File cacheFile = ImageHandler.getFullPathOfCacheFile(WEB_URL_TO_FILE.toString(), rootPath);

        DatabaseConnectionOrm dbConn = new DatabaseConnectionOrm(cont);
        Feed feed = dbConn.getFeedById(ThreadId);
        if (!cacheFile.isFile() || feed.getAvgColour() == null) {
            File dir = new File(rootPath);
            dir.mkdirs();
            cacheFile = ImageHandler.getFullPathOfCacheFile(WEB_URL_TO_FILE.toString(), rootPath);
            //cacheFile.createNewFile();

            /* Open a connection to that URL. */
            URLConnection urlConn = WEB_URL_TO_FILE.openConnection();

            urlConn.setRequestProperty("User-Agent",
                    "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");

            /*
             * Define InputStreams to read from the URLConnection.
             */
            InputStream is = urlConn.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;
            while ((current = bis.read()) != -1) {
                baf.append((byte) current);
            }

            //If the file is not empty
            if (baf.length() > 0) {
                bmp = BitmapFactory.decodeByteArray(baf.toByteArray(), 0, baf.length());

                FileOutputStream fos = new FileOutputStream(cacheFile);
                fos.write(baf.toByteArray());
                fos.close();
            }
        }
        //return cacheFile.getPath();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    //return bmp;

    if (imageDownloadFinished != null)
        imageDownloadFinished.DownloadFinished(ThreadId, bmp);

    super.run();
}