1 package com.bumptech.glide.load.model.stream; 2 3 import android.content.Context; 4 5 import com.bumptech.glide.load.data.DataFetcher; 6 import com.bumptech.glide.load.data.HttpUrlFetcher; 7 import com.bumptech.glide.load.model.GenericLoaderFactory; 8 import com.bumptech.glide.load.model.GlideUrl; 9 import com.bumptech.glide.load.model.ModelCache; 10 import com.bumptech.glide.load.model.ModelLoader; 11 import com.bumptech.glide.load.model.ModelLoaderFactory; 12 13 import java.io.InputStream; 14 15 /** 16 * An {@link com.bumptech.glide.load.model.ModelLoader} for translating {@link com.bumptech.glide.load.model.GlideUrl} 17 * (http/https URLS) into {@link java.io.InputStream} data. 18 */ 19 public class HttpUrlGlideUrlLoader implements ModelLoader<GlideUrl, InputStream> { 20 21 private final ModelCache<GlideUrl, GlideUrl> modelCache; 22 23 /** 24 * The default factory for {@link com.bumptech.glide.load.model.stream.HttpUrlGlideUrlLoader}s. 25 */ 26 public static class Factory implements ModelLoaderFactory<GlideUrl, InputStream> { 27 private final ModelCache<GlideUrl, GlideUrl> modelCache = new ModelCache<GlideUrl, GlideUrl>(500); 28 29 @Override 30 public ModelLoader<GlideUrl, InputStream> build(Context context, GenericLoaderFactory factories) { 31 return new HttpUrlGlideUrlLoader(modelCache); 32 } 33 34 @Override 35 public void teardown() { 36 // Do nothing. 37 } 38 } 39 40 public HttpUrlGlideUrlLoader() { 41 this(null); 42 } 43 44 public HttpUrlGlideUrlLoader(ModelCache<GlideUrl, GlideUrl> modelCache) { 45 this.modelCache = modelCache; 46 } 47 48 @Override 49 public DataFetcher<InputStream> getResourceFetcher(GlideUrl model, int width, int height) { 50 // GlideUrls memoize parsed URLs so caching them saves a few object instantiations and time spent parsing urls. 51 GlideUrl url = model; 52 if (modelCache != null) { 53 url = modelCache.get(model, 0, 0); 54 if (url == null) { 55 modelCache.put(model, 0, 0, model); 56 url = model; 57 } 58 } 59 return new HttpUrlFetcher(url); 60 } 61 } 62