たา세 v1.0.0版本的 MD5 值为:c37bbc638045a51dd4bc7ed739fbb06b
以下内容为反编译后的 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 width, int height, 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;
DataUriFetcher(String dataUri, DataDecoder<Data> reader) {
this.dataUri = dataUri;
this.reader = reader;
}
@Override
public void loadData(Priority priority, DataFetcher.DataCallback<? super Data> callback) {
try {
Data decode = this.reader.decode(this.dataUri);
this.data = decode;
callback.onDataReady(decode);
} catch (IllegalArgumentException e) {
callback.onLoadFailed(e);
}
}
@Override
public void cleanup() {
try {
this.reader.close(this.data);
} catch (IOException e) {
}
}
@Override
public void cancel() {
}
@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 url) {
if (!url.startsWith(DataUrlLoader.DATA_SCHEME_IMAGE)) {
throw new IllegalArgumentException("Not a valid image data URL.");
}
int commaIndex = url.indexOf(44);
if (commaIndex == -1) {
throw new IllegalArgumentException("Missing comma in data URL.");
}
String beforeComma = url.substring(0, commaIndex);
if (!beforeComma.endsWith(DataUrlLoader.BASE64_TAG)) {
throw new IllegalArgumentException("Not a base64 image data URL.");
}
String afterComma = url.substring(commaIndex + 1);
byte[] bytes = Base64.decode(afterComma, 0);
return new ByteArrayInputStream(bytes);
}
@Override
public void close(InputStream inputStream) throws IOException {
inputStream.close();
}
@Override
public Class<InputStream> getDataClass() {
return InputStream.class;
}
};
@Override
public ModelLoader<Model, InputStream> build(MultiModelLoaderFactory multiFactory) {
return new DataUrlLoader(this.opener);
}
@Override
public void teardown() {
}
}
}