Use try-with-resources (IDEA-CR-24263)

This commit is contained in:
Dmitry Trofimov
2017-09-25 15:12:30 +02:00
parent 33d89b68ba
commit 3dc2aed999
2 changed files with 7 additions and 16 deletions
@@ -149,15 +149,12 @@ public class TarUtil {
public static void extract(@NotNull File file, @NotNull File outputDir, @Nullable FilenameFilter filenameFilter, boolean overwrite) public static void extract(@NotNull File file, @NotNull File outputDir, @Nullable FilenameFilter filenameFilter, boolean overwrite)
throws IOException { throws IOException {
FileInputStream fis = new FileInputStream(file);
GzipCompressorInputStream gcis = new GzipCompressorInputStream(fis); try (FileInputStream fis = new FileInputStream(file);
TarArchiveInputStream tis = new TarArchiveInputStream(gcis); GzipCompressorInputStream gcis = new GzipCompressorInputStream(fis);
try { TarArchiveInputStream tis = new TarArchiveInputStream(gcis)) {
extract(tis, outputDir, filenameFilter, overwrite); extract(tis, outputDir, filenameFilter, overwrite);
} }
finally {
fis.close();
}
} }
public static void extract(@NotNull final TarArchiveInputStream tis, public static void extract(@NotNull final TarArchiveInputStream tis,
@@ -219,10 +216,8 @@ public class TarUtil {
* update an existing jar file. Adds/replace files specified in relpathToFile map * update an existing jar file. Adds/replace files specified in relpathToFile map
*/ */
public static void update(InputStream in, OutputStream out, Map<String, File> relpathToFile) throws IOException { public static void update(InputStream in, OutputStream out, Map<String, File> relpathToFile) throws IOException {
TarArchiveInputStream tis = new TarArchiveInputStream(in); try (TarArchiveInputStream tis = new TarArchiveInputStream(in);
TarArchiveOutputStream tos = new TarArchiveOutputStream(out); TarArchiveOutputStream tos = new TarArchiveOutputStream(out)) {
try {
// put the old entries first, replace if necessary // put the old entries first, replace if necessary
TarArchiveEntry e; TarArchiveEntry e;
while ((e = tis.getNextTarEntry()) != null) { while ((e = tis.getNextTarEntry()) != null) {
@@ -256,9 +251,5 @@ public class TarUtil {
addFileToTar(tos, file, path, null, null); addFileToTar(tos, file, path, null, null);
} }
} }
finally {
tis.close();
tos.close();
}
} }
} }
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<module relativePaths="true" type="JAVA_MODULE" version="4"> <module relativePaths="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_6" inherit-compiler-output="true"> <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8" inherit-compiler-output="true">
<exclude-output /> <exclude-output />
<annotation-paths> <annotation-paths>
<root url="file://$MODULE_DIR$/anno" /> <root url="file://$MODULE_DIR$/anno" />