public class OkHttpGlideModule implements GlideModule {
public OkHttpGlideModule() {
}
public void applyOptions(Context context, GlideBuilder builder) {
}
public void registerComponents(Context context, Glide glide, Registry registry) {
registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory());
}
}
public class OkHttpUrlLoader implements ModelLoader<GlideUrl, InputStream> {
private final okhttp3.Call.Factory client;
public OkHttpUrlLoader(okhttp3.Call.Factory client) {
this.client = client;
}
public boolean handles(GlideUrl url) {
return true;
}
public LoadData<InputStream> buildLoadData(GlideUrl model, int width, int height, Options options) {
//返回LoadData对象,泛型为InputStream
return new LoadData(model, new OkHttpStreamFetcher(this.client, model));
}
public static class Factory implements ModelLoaderFactory<GlideUrl, InputStream> {
private static volatile okhttp3.Call.Factory internalClient;
private okhttp3.Call.Factory client;
private static okhttp3.Call.Factory getInternalClient() {
if(internalClient == null) {
Class var0 = OkHttpUrlLoader.Factory.class;
synchronized(OkHttpUrlLoader.Factory.class) {
if(internalClient == null) {
internalClient = new OkHttpClient();
}
}
}
return internalClient;
}
public Factory() {
this(getInternalClient());
}
public Factory(okhttp3.Call.Factory client) {
this.client = client;
}
public ModelLoader<GlideUrl, InputStream> build(MultiModelLoaderFactory multiFactory) {
return new OkHttpUrlLoader(this.client);
}
public void teardown() {
}
}
}
public class OkHttpStreamFetcher implements DataFetcher<InputStream> {
private static final String TAG = "OkHttpFetcher";
private final Factory client;
private final GlideUrl url;
InputStream stream;
ResponseBody responseBody;
private volatile Call call;
public OkHttpStreamFetcher(Factory client, GlideUrl url) {
this.client = client;
this.url = url;
}
public void loadData(Priority priority, final DataCallback<? super InputStream> callback) {
Builder requestBuilder = (new Builder()).url(this.url.toStringUrl());
Iterator request = this.url.getHeaders().entrySet().iterator();
while(request.hasNext()) {
Entry headerEntry = (Entry)request.next();
String key = (String)headerEntry.getKey();
requestBuilder.addHeader(key, (String)headerEntry.getValue());
}
Request request1 = requestBuilder.build();
this.call = this.client.newCall(request1);
this.call.enqueue(new Callback() {
public void onFailure(Call call, IOException e) {
if(Log.isLoggable("OkHttpFetcher", 3)) {
Log.d("OkHttpFetcher", "OkHttp failed to obtain result", e);
}
callback.onLoadFailed(e);
}
public void onResponse(Call call, Response response) throws IOException {
OkHttpStreamFetcher.this.responseBody = response.body();
if(response.isSuccessful()) {
long contentLength = OkHttpStreamFetcher.this.responseBody.contentLength();
OkHttpStreamFetcher.this.stream = ContentLengthInputStream.obtain(OkHttpStreamFetcher.this.responseBody.byteStream(), contentLength);
callback.onDataReady(OkHttpStreamFetcher.this.stream);
} else {
callback.onLoadFailed(new HttpException(response.message(), response.code()));
}
}
});
}
public void cleanup() {
try {
if(this.stream != null) {
this.stream.close();
}
} catch (IOException var2) {
;
}
if(this.responseBody != null) {
this.responseBody.close();
}
}
public void cancel() {
Call local = this.call;
if(local != null) {
local.cancel();
}
}
public Class<InputStream> getDataClass() {
return InputStream.class;
}
public DataSource getDataSource() {
return DataSource.REMOTE;
}
}
public synchronized int read() throws IOException {
int value = super.read();
this.checkReadSoFarOrThrow(value >= 0?1:-1);
return value;
}
public int read(byte[] buffer) throws IOException {
return this.read(buffer, 0, buffer.length);
}
public synchronized int read(byte[] buffer, int byteOffset, int byteCount) throws IOException {
return this.checkReadSoFarOrThrow(super.read(buffer, byteOffset, byteCount));
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有