Search in sources :

Example 1 with ContentLengthInputStream

use of com.nostra13.universalimageloader.core.assist.ContentLengthInputStream in project Android-Universal-Image-Loader by nostra13.

the class OkHttpImageDownloader method getStreamFromNetwork.

@Override
protected InputStream getStreamFromNetwork(String imageUri, Object extra) throws IOException {
    Request request = new Request.Builder().url(imageUri).build();
    ResponseBody responseBody = client.newCall(request).execute().body();
    InputStream inputStream = responseBody.byteStream();
    int contentLength = (int) responseBody.contentLength();
    return new ContentLengthInputStream(inputStream, contentLength);
}
Also used : ContentLengthInputStream(com.nostra13.universalimageloader.core.assist.ContentLengthInputStream) ContentLengthInputStream(com.nostra13.universalimageloader.core.assist.ContentLengthInputStream) InputStream(java.io.InputStream) Request(com.squareup.okhttp.Request) ResponseBody(com.squareup.okhttp.ResponseBody)

Example 2 with ContentLengthInputStream

use of com.nostra13.universalimageloader.core.assist.ContentLengthInputStream in project Android-Universal-Image-Loader by nostra13.

the class BaseImageDownloader method getStreamFromNetwork.

/**
	 * Retrieves {@link InputStream} of image by URI (image is located in the network).
	 *
	 * @param imageUri Image URI
	 * @param extra    Auxiliary object which was passed to {@link DisplayImageOptions.Builder#extraForDownloader(Object)
	 *                 DisplayImageOptions.extraForDownloader(Object)}; can be null
	 * @return {@link InputStream} of image
	 * @throws IOException if some I/O error occurs during network request or if no InputStream could be created for
	 *                     URL.
	 */
protected InputStream getStreamFromNetwork(String imageUri, Object extra) throws IOException {
    HttpURLConnection conn = createConnection(imageUri, extra);
    int redirectCount = 0;
    while (conn.getResponseCode() / 100 == 3 && redirectCount < MAX_REDIRECT_COUNT) {
        conn = createConnection(conn.getHeaderField("Location"), extra);
        redirectCount++;
    }
    InputStream imageStream;
    try {
        imageStream = conn.getInputStream();
    } catch (IOException e) {
        // Read all data to allow reuse connection (http://bit.ly/1ad35PY)
        IoUtils.readAndCloseStream(conn.getErrorStream());
        throw e;
    }
    if (!shouldBeProcessed(conn)) {
        IoUtils.closeSilently(imageStream);
        throw new IOException("Image request failed with response code " + conn.getResponseCode());
    }
    return new ContentLengthInputStream(new BufferedInputStream(imageStream, BUFFER_SIZE), conn.getContentLength());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ContentLengthInputStream(com.nostra13.universalimageloader.core.assist.ContentLengthInputStream) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ContentLengthInputStream(com.nostra13.universalimageloader.core.assist.ContentLengthInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Aggregations

ContentLengthInputStream (com.nostra13.universalimageloader.core.assist.ContentLengthInputStream)2 InputStream (java.io.InputStream)2 Request (com.squareup.okhttp.Request)1 ResponseBody (com.squareup.okhttp.ResponseBody)1 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1