心动空间 v1.0.0版本的 MD5 值为:de8941a869ff7ed1429262fe5aa87293
以下内容为反编译后的 DataUrlLoader.java 源代码,内容仅作参考
package com.bumptech.glide.load.model;
import android.util.Base64;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.model.ModelLoader;
import com.bumptech.glide.signature.ObjectKey;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
public final class DataUrlLoader<Model, Data> implements ModelLoader<Model, Data> {
private static final String BASE64_TAG = ";base64";
private static final String DATA_SCHEME_IMAGE = "data:image";
private final DataDecoder<Data> dataDecoder;
public interface DataDecoder<Data> {
void close(Data data) throws IOException;
Data decode(String str) throws IllegalArgumentException;
Class<Data> getDataClass();
}
public DataUrlLoader(DataDecoder<Data> dataDecoder) {
this.dataDecoder = dataDecoder;
}
@Override
public ModelLoader.LoadData<Data> buildLoadData(Model model, int i, int i2, Options options) {
return new ModelLoader.LoadData<>(new ObjectKey(model), new DataUriFetcher(model.toString(), this.dataDecoder));
}
@Override
public boolean handles(Model model) {
return model.toString().startsWith(DATA_SCHEME_IMAGE);
}
private static final class DataUriFetcher<Data> implements DataFetcher<Data> {
private Data data;
private final String dataUri;
private final DataDecoder<Data> reader;
@Override
public void cancel() {
}
DataUriFetcher(String str, DataDecoder<Data> dataDecoder) {
this.dataUri = str;
this.reader = dataDecoder;
}
@Override
public void loadData(Priority priority, DataFetcher.DataCallback<? super Data> dataCallback) {
try {
Data decode = this.reader.decode(this.dataUri);
this.data = decode;
dataCallback.onDataReady(decode);
} catch (IllegalArgumentException e) {
dataCallback.onLoadFailed(e);
}
}
@Override
public void cleanup() {
try {
this.reader.close(this.data);
} catch (IOException unused) {
}
}
@Override
public Class<Data> getDataClass() {
return this.reader.getDataClass();
}
@Override
public DataSource getDataSource() {
return DataSource.LOCAL;
}
}
public static final class StreamFactory<Model> implements ModelLoaderFactory<Model, InputStream> {
private final DataDecoder<InputStream> opener = new DataDecoder<InputStream>() {
@Override
public InputStream decode(String str) {
if (!str.startsWith(DataUrlLoader.DATA_SCHEME_IMAGE)) {
throw new IllegalArgumentException("Not a valid image data URL.");
}
int indexOf = str.indexOf(44);
if (indexOf == -1) {
throw new IllegalArgumentException("Missing comma in data URL.");
}
if (!str.substring(0, indexOf).endsWith(DataUrlLoader.BASE64_TAG)) {
throw new IllegalArgumentException("Not a base64 image data URL.");
}
return new ByteArrayInputStream(Base64.decode(str.substring(indexOf + 1), 0));
}
@Override
public void close(InputStream inputStream) throws IOException {
inputStream.close();
}
@Override
public Class<InputStream> getDataClass() {
return InputStream.class;
}
};
@Override
public void teardown() {
}
@Override
public ModelLoader<Model, InputStream> build(MultiModelLoaderFactory multiModelLoaderFactory) {
return new DataUrlLoader(this.opener);
}
}
}