1 package com.bumptech.glide.load.resource.file; 2 3 import com.bumptech.glide.load.Encoder; 4 import com.bumptech.glide.load.ResourceDecoder; 5 import com.bumptech.glide.load.ResourceEncoder; 6 import com.bumptech.glide.load.engine.Resource; 7 import com.bumptech.glide.load.model.StreamEncoder; 8 import com.bumptech.glide.load.resource.NullResourceEncoder; 9 import com.bumptech.glide.provider.DataLoadProvider; 10 11 import java.io.File; 12 import java.io.InputStream; 13 14 /** 15 * An {@link com.bumptech.glide.provider.DataLoadProvider} that provides encoders and decoders for for obtaining a 16 * cache file from {@link java.io.InputStream} data. 17 */ 18 public class StreamFileDataLoadProvider implements DataLoadProvider<InputStream, File> { 19 private static final ErrorSourceDecoder ERROR_DECODER = new ErrorSourceDecoder(); 20 21 private final ResourceDecoder<File, File> cacheDecoder; 22 private final Encoder<InputStream> encoder; 23 24 public StreamFileDataLoadProvider() { 25 cacheDecoder = new FileDecoder(); 26 encoder = new StreamEncoder(); 27 } 28 29 @Override 30 public ResourceDecoder<File, File> getCacheDecoder() { 31 return cacheDecoder; 32 } 33 34 @Override 35 public ResourceDecoder<InputStream, File> getSourceDecoder() { 36 return ERROR_DECODER; 37 } 38 39 @Override 40 public Encoder<InputStream> getSourceEncoder() { 41 return encoder; 42 } 43 44 @Override 45 public ResourceEncoder<File> getEncoder() { 46 return NullResourceEncoder.get(); 47 } 48 49 private static class ErrorSourceDecoder implements ResourceDecoder<InputStream, File> { 50 @Override 51 public Resource<File> decode(InputStream source, int width, int height) { 52 throw new Error("You cannot decode a File from an InputStream by default," 53 + " try either #diskCacheStratey(DiskCacheStrategy.SOURCE) to avoid this call or" 54 + " #decoder(ResourceDecoder) to replace this Decoder"); 55 } 56 57 @Override 58 public String getId() { 59 return ""; 60 } 61 } 62 } 63