aboutsummaryrefslogtreecommitdiff
path: root/library/src/main/java/com/bumptech/glide/load/resource/FileToStreamDecoder.java
diff options
context:
space:
mode:
Diffstat (limited to 'library/src/main/java/com/bumptech/glide/load/resource/FileToStreamDecoder.java')
-rw-r--r--library/src/main/java/com/bumptech/glide/load/resource/FileToStreamDecoder.java68
1 files changed, 68 insertions, 0 deletions
diff --git a/library/src/main/java/com/bumptech/glide/load/resource/FileToStreamDecoder.java b/library/src/main/java/com/bumptech/glide/load/resource/FileToStreamDecoder.java
new file mode 100644
index 00000000..907c7746
--- /dev/null
+++ b/library/src/main/java/com/bumptech/glide/load/resource/FileToStreamDecoder.java
@@ -0,0 +1,68 @@
+package com.bumptech.glide.load.resource;
+
+import com.bumptech.glide.load.ResourceDecoder;
+import com.bumptech.glide.load.engine.Resource;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * A decoder that wraps an InputStream decoder to allow it to decode from a file.
+ * @param <T> The type of resource that the wrapped InputStream decoder decodes.
+ */
+public class FileToStreamDecoder<T> implements ResourceDecoder<File, T> {
+ private static final FileOpener DEFAULT_FILE_OPENER = new DefaultFileOpener();
+
+ private ResourceDecoder<InputStream, T> streamDecoder;
+ private final FileOpener fileOpener;
+
+ public FileToStreamDecoder(ResourceDecoder<InputStream, T> streamDecoder) {
+ this(streamDecoder, DEFAULT_FILE_OPENER);
+ }
+
+ // Exposed for testing.
+ FileToStreamDecoder(ResourceDecoder<InputStream, T> streamDecoder, FileOpener fileOpener) {
+ this.streamDecoder = streamDecoder;
+ this.fileOpener = fileOpener;
+ }
+
+ @Override
+ public Resource<T> decode(File source, int width, int height) throws IOException {
+ InputStream is = null;
+ Resource<T> result = null;
+ try {
+ is = fileOpener.open(source);
+ result = streamDecoder.decode(is, width, height);
+ } finally {
+ if (is != null) {
+ try {
+ is.close();
+ } catch (IOException e) {
+ // Do nothing.
+ }
+ }
+
+ }
+ return result;
+ }
+
+ @Override
+ public String getId() {
+ return "";
+ }
+
+ interface FileOpener {
+ public InputStream open(File file) throws FileNotFoundException;
+ }
+
+ private static class DefaultFileOpener implements FileOpener {
+
+ @Override
+ public InputStream open(File file) throws FileNotFoundException {
+ return new FileInputStream(file);
+ }
+ }
+}