diff --git a/library/src/main/java/top/zibin/luban/InputStreamAdapter.java b/library/src/main/java/top/zibin/luban/InputStreamAdapter.java new file mode 100644 index 0000000..e085a30 --- /dev/null +++ b/library/src/main/java/top/zibin/luban/InputStreamAdapter.java @@ -0,0 +1,34 @@ +package top.zibin.luban; + +import java.io.IOException; +import java.io.InputStream; + +/** + * Automatically close the previous InputStream when opening a new InputStream, + * and finally need to manually call {@link #close()} to release the resource. + */ +public abstract class InputStreamAdapter implements InputStreamProvider { + + private InputStream inputStream; + + @Override + public InputStream open() throws IOException { + close(); + inputStream = openInternal(); + return inputStream; + } + + public abstract InputStream openInternal() throws IOException; + + @Override + public void close() { + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException ignore) { + }finally { + inputStream = null; + } + } + } +} \ No newline at end of file diff --git a/library/src/main/java/top/zibin/luban/InputStreamProvider.java b/library/src/main/java/top/zibin/luban/InputStreamProvider.java index 28b164f..70105e9 100644 --- a/library/src/main/java/top/zibin/luban/InputStreamProvider.java +++ b/library/src/main/java/top/zibin/luban/InputStreamProvider.java @@ -12,5 +12,7 @@ public interface InputStreamProvider { InputStream open() throws IOException; + void close(); + String getPath(); } diff --git a/library/src/main/java/top/zibin/luban/Luban.java b/library/src/main/java/top/zibin/luban/Luban.java index 4e1e599..758d4ab 100644 --- a/library/src/main/java/top/zibin/luban/Luban.java +++ b/library/src/main/java/top/zibin/luban/Luban.java @@ -149,7 +149,11 @@ public void run() { * start compress and return the file */ private File get(InputStreamProvider input, Context context) throws IOException { - return new Engine(input, getImageCacheFile(context, Checker.SINGLE.extSuffix(input)), focusAlpha).compress(); + try { + return new Engine(input, getImageCacheFile(context, Checker.SINGLE.extSuffix(input)), focusAlpha).compress(); + } finally { + input.close(); + } } private List get(Context context) throws IOException { @@ -165,6 +169,14 @@ private List get(Context context) throws IOException { } private File compress(Context context, InputStreamProvider path) throws IOException { + try { + return compressReal(context,path); + } finally { + path.close(); + } + } + + private File compressReal(Context context, InputStreamProvider path) throws IOException { File result; File outFile = getImageCacheFile(context, Checker.SINGLE.extSuffix(path)); @@ -233,9 +245,9 @@ public Builder load(InputStreamProvider inputStreamProvider) { } public Builder load(final File file) { - mStreamProviders.add(new InputStreamProvider() { + mStreamProviders.add(new InputStreamAdapter() { @Override - public InputStream open() throws IOException { + public InputStream openInternal() throws IOException { return new FileInputStream(file); } @@ -248,9 +260,9 @@ public String getPath() { } public Builder load(final String string) { - mStreamProviders.add(new InputStreamProvider() { + mStreamProviders.add(new InputStreamAdapter() { @Override - public InputStream open() throws IOException { + public InputStream openInternal() throws IOException { return new FileInputStream(string); } @@ -278,9 +290,9 @@ public Builder load(List list) { } public Builder load(final Uri uri) { - mStreamProviders.add(new InputStreamProvider() { + mStreamProviders.add(new InputStreamAdapter() { @Override - public InputStream open() throws IOException { + public InputStream openInternal() throws IOException { return context.getContentResolver().openInputStream(uri); } @@ -351,9 +363,9 @@ public void launch() { } public File get(final String path) throws IOException { - return build().get(new InputStreamProvider() { + return build().get(new InputStreamAdapter() { @Override - public InputStream open() throws IOException { + public InputStream openInternal() throws IOException { return new FileInputStream(path); }