Java Code Examples for com.bumptech.glide.util.ContentLengthInputStream#obtain()

The following examples show how to use com.bumptech.glide.util.ContentLengthInputStream#obtain() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImageStreamFetcher.java    From hipda with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void loadData(Priority priority, DataCallback<? super InputStream> callback) {
    Request request = getRequest();
    try {
        Response response = client.newCall(request).execute();
        responseBody = response.body();
        if (!response.isSuccessful()) {
            throw new IOException(OkHttpHelper.ERROR_CODE_PREFIX + response.code());
        }
        long contentLength = responseBody.contentLength();
        stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);
        callback.onDataReady(stream);
    } catch (Exception e) {
        callback.onLoadFailed(e);
    }
}
 
Example 2
Source File: OkHttpStreamFetcher.java    From android-tutorials-glide with MIT License 6 votes vote down vote up
@Override
public InputStream loadData(Priority priority) throws Exception {
    Request.Builder requestBuilder = new Request.Builder()
            .url(url.toStringUrl());

    for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) {
        String key = headerEntry.getKey();
        requestBuilder.addHeader(key, headerEntry.getValue());
    }

    Request request = requestBuilder.build();

    Response response = client.newCall(request).execute();
    responseBody = response.body();
    if (!response.isSuccessful()) {
        throw new IOException("Request failed with code: " + response.code());
    }

    long contentLength = responseBody.contentLength();
    stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);
    return stream;
}
 
Example 3
Source File: OkHttpStreamFetcher.java    From imsdk-android with MIT License 6 votes vote down vote up
@Override
public InputStream loadData(Priority priority) throws Exception {
    Request.Builder requestBuilder = new Request.Builder().url(url.toStringUrl());

    for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) {
        String key = headerEntry.getKey();
        requestBuilder.addHeader(key, headerEntry.getValue());
    }
    Request request = requestBuilder.build();

    Response response;
    call = client.newCall(request);
    response = call.execute();
    responseBody = response.body();
    if (!response.isSuccessful()) {
        throw new IOException("Request failed with code: " + response.code());
    }

    long contentLength = responseBody.contentLength();
    stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);
    return stream;
}
 
Example 4
Source File: OkHttpStreamFetcher.java    From AndroidBase with Apache License 2.0 6 votes vote down vote up
@Override
public InputStream loadData(Priority priority) throws Exception {
  Request.Builder requestBuilder = new Request.Builder().url(url.toStringUrl());

  for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) {
    String key = headerEntry.getKey();
    requestBuilder.addHeader(key, headerEntry.getValue());
  }
  Request request = requestBuilder.build();

  Response response;
  call = client.newCall(request);
  response = call.execute();
  responseBody = response.body();
  if (!response.isSuccessful()) {
    throw new IOException("Request failed with code: " + response.code());
  }

  long contentLength = responseBody.contentLength();
  stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);
  return stream;
}
 
Example 5
Source File: OkHttpStreamFetcher.java    From NewFastFrame with Apache License 2.0 6 votes vote down vote up
@Override
public void loadData(@NonNull Priority priority, @NonNull DataCallback<? super InputStream> callback) {
    Request.Builder requestBuilder = new Request.Builder().url(url.toStringUrl());
    for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) {
        String key = headerEntry.getKey();
        requestBuilder.addHeader(key, headerEntry.getValue());
    }
    Request request = requestBuilder.build();
    Response response = null;
    call = client.newCall(request);
    try {
        response = call.execute();
        responseBody = response.body();
    } catch (IOException e) {
        e.printStackTrace();
        callback.onLoadFailed(new IOException("Request failed with code: "));
        return;
    }
        long contentLength = responseBody.contentLength();
        stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);
        callback.onDataReady(stream);
}
 
Example 6
Source File: OkHttpStreamFetcher.java    From TestChat with Apache License 2.0 6 votes vote down vote up
@Override
public InputStream loadData(Priority priority) throws Exception {
    Request.Builder requestBuilder = new Request.Builder()
            .url(url.toStringUrl());

    for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) {
        String key = headerEntry.getKey();
        requestBuilder.addHeader(key, headerEntry.getValue());
    }

    Request request = requestBuilder.build();

    Response response = client.newCall(request).execute();
    responseBody = response.body();
    if (!response.isSuccessful()) {
        throw new IOException("Request failed with code: " + response.code());
    }

    long contentLength = responseBody.contentLength();
    stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);
    return stream;
}
 
Example 7
Source File: StethoHttpUrlFetcher.java    From glide-support with The Unlicense 6 votes vote down vote up
private InputStream getStreamForSuccessfulRequest(HttpURLConnection urlConnection) throws IOException {
	try {
		InputStream responseStream = stethoManager.interpretResponseStream(urlConnection.getInputStream());
		if (TextUtils.isEmpty(urlConnection.getContentEncoding())) {
			int contentLength = urlConnection.getContentLength();
			stream = ContentLengthInputStream.obtain(responseStream, contentLength);
		} else {
			if (Log.isLoggable(TAG, Log.DEBUG)) {
				Log.d(TAG, "Got non empty content encoding: " + urlConnection.getContentEncoding());
			}
			stream = responseStream;
		}
		return stream;
	} catch (IOException ex) {
		stethoManager.httpExchangeFailed(ex);
		throw ex;
	}
}
 
Example 8
Source File: OkHttpStreamFetcher.java    From AndroidModulePattern with Apache License 2.0 6 votes vote down vote up
@Override
public InputStream loadData(Priority priority) throws Exception {
    Request.Builder requestBuilder = new Request.Builder()
            .url(url.toStringUrl());

    for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) {
        String key = headerEntry.getKey();
        requestBuilder.addHeader(key, headerEntry.getValue());
    }

    Request request = requestBuilder.build();

    Response response = client.newCall(request).execute();
    responseBody = response.body();
    if (!response.isSuccessful()) {
        throw new IOException("Request failed with code: " + response.code());
    }

    long contentLength = responseBody.contentLength();
    stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);
    return stream;
}
 
Example 9
Source File: OkHttpStreamFetcher.java    From Aurora with Apache License 2.0 5 votes vote down vote up
@Override
public void onResponse(Call call, Response response) throws IOException {
    responseBody = response.body();
    if (response.isSuccessful()) {
        long contentLength = responseBody.contentLength();
        stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);
        callback.onDataReady(stream);
    } else {
        callback.onLoadFailed(new HttpException(response.message(), response.code()));
    }
}
 
Example 10
Source File: OkHttpStreamFetcher.java    From MVPArms with Apache License 2.0 5 votes vote down vote up
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) {
    responseBody = response.body();
    if (response.isSuccessful()) {
        long contentLength = Preconditions.checkNotNull(responseBody).contentLength();
        stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);
        callback.onDataReady(stream);
    } else {
        callback.onLoadFailed(new HttpException(response.message(), response.code()));
    }
}
 
Example 11
Source File: OkHttpStreamFetcher.java    From pandroid with Apache License 2.0 5 votes vote down vote up
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) {
  responseBody = response.body();
  if (response.isSuccessful()) {
    long contentLength = Preconditions.checkNotNull(responseBody).contentLength();
    stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);
    callback.onDataReady(stream);
  } else {
    callback.onLoadFailed(new HttpException(response.message(), response.code()));
  }
}
 
Example 12
Source File: OkHttpStreamFetcher.java    From MVVMArms with Apache License 2.0 5 votes vote down vote up
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
    responseBody = response.body();
    if (response.isSuccessful()) {
        long contentLength = responseBody.contentLength();
        stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);
        callback.onDataReady(stream);
    } else {
        callback.onLoadFailed(new HttpException(response.message(), response.code()));
    }
}
 
Example 13
Source File: OkHttpStreamFetcher.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void loadData(@NonNull Priority priority, @NonNull DataCallback<? super InputStream> callback) {
  try {
    Request.Builder requestBuilder = new Request.Builder()
        .url(url.toStringUrl());

    for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) {
      String key = headerEntry.getKey();
      requestBuilder.addHeader(key, headerEntry.getValue());
    }

    Request  request  = requestBuilder.build();
    Response response = client.newCall(request).execute();

    responseBody = response.body();

    if (!response.isSuccessful()) {
      throw new IOException("Request failed with code: " + response.code());
    }

    long contentLength = responseBody.contentLength();
    stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);

    callback.onDataReady(stream);
  } catch (IOException e) {
    callback.onLoadFailed(e);
  }
}
 
Example 14
Source File: OkHttpStreamFetcher.java    From FastAndroid with Apache License 2.0 5 votes vote down vote up
@Override
public void onResponse(Call call, Response response) throws IOException {
    responseBody = response.body();
    if (response.isSuccessful()) {
        long contentLength = responseBody.contentLength();
        stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);
        callback.onDataReady(stream);
    } else {
        callback.onLoadFailed(new HttpException(response.message(), response.code()));
    }
}
 
Example 15
Source File: OkHttpFetcher.java    From AndroidProject with Apache License 2.0 5 votes vote down vote up
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) {
    mResponseBody = response.body();
    if (response.isSuccessful()) {
        long contentLength = Preconditions.checkNotNull(mResponseBody).contentLength();
        mInputStream = ContentLengthInputStream.obtain(mResponseBody.byteStream(), contentLength);
        mDataCallback.onDataReady(mInputStream);
    } else {
        mDataCallback.onLoadFailed(new HttpException(response.message(), response.code()));
    }
}
 
Example 16
Source File: OkHttpStreamFetcher.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void loadData(Priority priority, DataCallback<? super InputStream> callback) {
  try {
    Request.Builder requestBuilder = new Request.Builder()
        .url(url.toStringUrl());

    for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) {
      String key = headerEntry.getKey();
      requestBuilder.addHeader(key, headerEntry.getValue());
    }

    Request  request  = requestBuilder.build();
    Response response = client.newCall(request).execute();

    responseBody = response.body();

    if (!response.isSuccessful()) {
      throw new IOException("Request failed with code: " + response.code());
    }

    long contentLength = responseBody.contentLength();
    stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);

    callback.onDataReady(stream);
  } catch (IOException e) {
    callback.onLoadFailed(e);
  }
}
 
Example 17
Source File: HttpUrlFetcher.java    From giffun with Apache License 2.0 5 votes vote down vote up
private InputStream getStreamForSuccessfulRequest(HttpURLConnection urlConnection)
        throws IOException {
    if (TextUtils.isEmpty(urlConnection.getContentEncoding())) {
        int contentLength = urlConnection.getContentLength();
        stream = ContentLengthInputStream.obtain(urlConnection.getInputStream(), contentLength);
    } else {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "Got non empty content encoding: " + urlConnection.getContentEncoding());
        }
        stream = urlConnection.getInputStream();
    }
    return stream;
}