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

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

Introduction

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

Prototype

null buffer

To view the source code for org.apache.http.util ByteArrayBuffer buffer.

Click Source Link

Usage

From source file:com.ab.http.AsyncHttpResponseHandler.java

/**
 * Gets the response data.// w  w  w.j  a v a 2s .  com
 *
 * @param entity the entity
 * @return the response data
 * @throws IOException Signals that an I/O exception has occurred.
 */
byte[] getResponseData(HttpEntity entity) throws IOException {
    byte[] responseBody = null;
    if (entity != null) {
        InputStream instream = entity.getContent();
        if (instream != null) {
            long contentLength = entity.getContentLength();
            if (contentLength > Integer.MAX_VALUE) {
                throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
            }
            if (contentLength < 0) {
                contentLength = BUFFER_SIZE;
            }
            try {
                ByteArrayBuffer buffer = new ByteArrayBuffer((int) contentLength);
                try {
                    byte[] tmp = new byte[BUFFER_SIZE];
                    int l, count = 0;
                    // do not send messages if request has been cancelled
                    while ((l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) {
                        count += l;
                        buffer.append(tmp, 0, l);
                        sendProgressMessage(count, (int) contentLength);
                    }
                } finally {
                    instream.close();
                }
                responseBody = buffer.buffer();
            } catch (OutOfMemoryError e) {
                System.gc();
                throw new IOException("File too large to fit into available memory");
            }
        }
    }
    return responseBody;
}

From source file:com.vuze.android.remote.SessionInfo.java

public void openTorrent(final Activity activity, final String name, InputStream is) {
    try {/* w w w . j a va 2s .co m*/
        int available = is.available();
        if (available <= 0) {
            available = 32 * 1024;
        }
        ByteArrayBuffer bab = new ByteArrayBuffer(available);

        boolean ok = AndroidUtils.readInputStreamIfStartWith(is, bab, new byte[] { 'd' });
        if (!ok) {
            String s;
            if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT && bab.length() == 0) {
                s = activity.getResources().getString(R.string.not_torrent_file_kitkat, name);
            } else {
                s = activity.getResources().getString(R.string.not_torrent_file, name,
                        Math.max(bab.length(), 5));
            }
            AndroidUtils.showDialog(activity, R.string.add_torrent, Html.fromHtml(s));
            return;
        }
        final String metainfo = Base64Encode.encodeToString(bab.buffer(), 0, bab.length());
        openTorrentWithMetaData(activity, name, metainfo);
    } catch (IOException e) {
        if (AndroidUtils.DEBUG) {
            e.printStackTrace();
        }
        VuzeEasyTracker.getInstance(activity).logError(e);
    } catch (OutOfMemoryError em) {
        VuzeEasyTracker.getInstance(activity).logError(em);
        AndroidUtils.showConnectionError(activity, "Out of Memory", true);
    }
}