Search in sources :

Example 1 with ByteArrayBuffer

use of org.apache.http.util.ByteArrayBuffer in project Libraries-for-Android-Developers by eoecn.

the class AsyncHttpResponseHandler method getResponseData.

/**
     * Returns byte array of response HttpEntity contents
     *
     * @param entity can be null
     * @return response entity body or null
     * @throws java.io.IOException if reading entity or creating byte array failed
     */
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");
            }
            int buffersize = (contentLength < 0) ? BUFFER_SIZE : (int) contentLength;
            try {
                ByteArrayBuffer buffer = new ByteArrayBuffer(buffersize);
                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.toByteArray();
            } catch (OutOfMemoryError e) {
                System.gc();
                throw new IOException("File too large to fit into available memory");
            }
        }
    }
    return responseBody;
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) ByteArrayBuffer(org.apache.http.util.ByteArrayBuffer)

Example 2 with ByteArrayBuffer

use of org.apache.http.util.ByteArrayBuffer in project android_frameworks_base by ParanoidAndroid.

the class BandwidthTestUtil method DownloadFromUrl.

/**
     * Download a given file from a target url to a given destination file.
     * @param targetUrl the url to download
     * @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;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) DataInputStream(java.io.DataInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) URL(java.net.URL) URLConnection(java.net.URLConnection) ByteArrayBuffer(org.apache.http.util.ByteArrayBuffer)

Example 3 with ByteArrayBuffer

use of org.apache.http.util.ByteArrayBuffer in project SmartAndroidSource by jaychou2012.

the class DataAsyncHttpResponseHandler method getResponseData.

/**
     * Returns byte array of response HttpEntity contents
     *
     * @param entity can be null
     * @return response entity body or null
     * @throws java.io.IOException if reading entity or creating byte array failed
     */
@Override
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;
                    // do not send messages if request has been cancelled
                    while ((l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) {
                        buffer.append(tmp, 0, l);
                        sendProgressDataMessage(copyOfRange(tmp, 0, l));
                    }
                } finally {
                    AsyncHttpClient.silentCloseInputStream(instream);
                }
                responseBody = buffer.toByteArray();
            } catch (OutOfMemoryError e) {
                System.gc();
                throw new IOException("File too large to fit into available memory");
            }
        }
    }
    return responseBody;
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) ByteArrayBuffer(org.apache.http.util.ByteArrayBuffer)

Example 4 with ByteArrayBuffer

use of org.apache.http.util.ByteArrayBuffer in project robovm by robovm.

the class AbstractSessionInputBuffer method init.

protected void init(final InputStream instream, int buffersize, final HttpParams params) {
    if (instream == null) {
        throw new IllegalArgumentException("Input stream may not be null");
    }
    if (buffersize <= 0) {
        throw new IllegalArgumentException("Buffer size may not be negative or zero");
    }
    if (params == null) {
        throw new IllegalArgumentException("HTTP parameters may not be null");
    }
    this.instream = instream;
    this.buffer = new byte[buffersize];
    this.bufferpos = 0;
    this.bufferlen = 0;
    this.linebuffer = new ByteArrayBuffer(buffersize);
    this.charset = HttpProtocolParams.getHttpElementCharset(params);
    this.ascii = this.charset.equalsIgnoreCase(HTTP.US_ASCII) || this.charset.equalsIgnoreCase(HTTP.ASCII);
    this.maxLineLen = params.getIntParameter(CoreConnectionPNames.MAX_LINE_LENGTH, -1);
    this.metrics = new HttpTransportMetricsImpl();
}
Also used : ByteArrayBuffer(org.apache.http.util.ByteArrayBuffer)

Example 5 with ByteArrayBuffer

use of org.apache.http.util.ByteArrayBuffer in project RoboZombie by sahan.

the class EntityUtils method toByteArray.

/**
     * Read the contents of an entity and return it as a byte array.
     *
     * @param entity
     * @return byte array containing the entity content. May be null if
     *   {@link HttpEntity#getContent()} is null.
     * @throws IOException if an error occurs reading the input stream
     * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE
     */
public static byte[] toByteArray(final HttpEntity entity) throws IOException {
    if (entity == null) {
        throw new IllegalArgumentException("HTTP entity may not be null");
    }
    InputStream instream = entity.getContent();
    if (instream == null) {
        return null;
    }
    try {
        if (entity.getContentLength() > Integer.MAX_VALUE) {
            throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
        }
        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);
        }
        return buffer.toByteArray();
    } finally {
        instream.close();
    }
}
Also used : InputStream(java.io.InputStream) ByteArrayBuffer(org.apache.http.util.ByteArrayBuffer)

Aggregations

ByteArrayBuffer (org.apache.http.util.ByteArrayBuffer)24 InputStream (java.io.InputStream)14 IOException (java.io.IOException)12 BufferedInputStream (java.io.BufferedInputStream)7 URL (java.net.URL)7 URLConnection (java.net.URLConnection)7 DataInputStream (java.io.DataInputStream)6 FileInputStream (java.io.FileInputStream)6 FileOutputStream (java.io.FileOutputStream)6 ContentBody (com.lidroid.xutils.http.client.multipart.content.ContentBody)1 InterruptedIOException (java.io.InterruptedIOException)1 ConnectException (java.net.ConnectException)1 HttpURLConnection (java.net.HttpURLConnection)1 ByteBuffer (java.nio.ByteBuffer)1 DataFormatException (java.util.zip.DataFormatException)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 ZipInputStream (java.util.zip.ZipInputStream)1 ProtocolException (org.apache.http.ProtocolException)1 ClientProtocolException (org.apache.http.client.ClientProtocolException)1