-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #319 from xxjy/master
- Loading branch information
Showing
3 changed files
with
57 additions
and
9 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
library/src/main/java/top/zibin/luban/InputStreamAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,5 +12,7 @@ public interface InputStreamProvider { | |
|
||
InputStream open() throws IOException; | ||
|
||
void close(); | ||
|
||
String getPath(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters