Skip to content

Commit

Permalink
Merge pull request #319 from xxjy/master
Browse files Browse the repository at this point in the history
close InputStream to fix #313,#314
  • Loading branch information
Curzibn authored Feb 19, 2019
2 parents ead45cf + 399e978 commit f19d080
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
34 changes: 34 additions & 0 deletions library/src/main/java/top/zibin/luban/InputStreamAdapter.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ public interface InputStreamProvider {

InputStream open() throws IOException;

void close();

String getPath();
}
30 changes: 21 additions & 9 deletions library/src/main/java/top/zibin/luban/Luban.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<File> get(Context context) throws IOException {
Expand All @@ -165,6 +169,14 @@ private List<File> 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));
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -278,9 +290,9 @@ public <T> Builder load(List<T> 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);
}

Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit f19d080

Please sign in to comment.