Unverified 提交 bc763b82 authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #861 from igor-suhorukov/master

Avoid resource leak
...@@ -64,7 +64,7 @@ public class BackupCommand extends Prepared { ...@@ -64,7 +64,7 @@ public class BackupCommand extends Prepared {
} }
String name = db.getName(); String name = db.getName();
name = FileUtils.getName(name); name = FileUtils.getName(name);
OutputStream zip = FileUtils.newOutputStream(fileName, false); try (OutputStream zip = FileUtils.newOutputStream(fileName, false)) {
ZipOutputStream out = new ZipOutputStream(zip); ZipOutputStream out = new ZipOutputStream(zip);
db.flush(); db.flush();
if (db.getPageStore() != null) { if (db.getPageStore() != null) {
...@@ -97,7 +97,7 @@ public class BackupCommand extends Prepared { ...@@ -97,7 +97,7 @@ public class BackupCommand extends Prepared {
} }
} }
out.close(); out.close();
zip.close(); }
} catch (IOException e) { } catch (IOException e) {
throw DbException.convertIOException(e, fileName); throw DbException.convertIOException(e, fileName);
} }
......
...@@ -128,7 +128,7 @@ public class Backup extends Tool { ...@@ -128,7 +128,7 @@ public class Backup extends Tool {
OutputStream fileOut = null; OutputStream fileOut = null;
try { try {
fileOut = FileUtils.newOutputStream(zipFileName, false); fileOut = FileUtils.newOutputStream(zipFileName, false);
ZipOutputStream zipOut = new ZipOutputStream(fileOut); try (ZipOutputStream zipOut = new ZipOutputStream(fileOut)) {
String base = ""; String base = "";
for (String fileName : list) { for (String fileName : list) {
if (allFiles || if (allFiles ||
...@@ -168,7 +168,7 @@ public class Backup extends Tool { ...@@ -168,7 +168,7 @@ public class Backup extends Tool {
out.println("Processed: " + fileName); out.println("Processed: " + fileName);
} }
} }
zipOut.close(); }
} catch (IOException e) { } catch (IOException e) {
throw DbException.convertIOException(e, zipFileName); throw DbException.convertIOException(e, zipFileName);
} finally { } finally {
......
...@@ -220,20 +220,11 @@ public class ChangeFileEncryption extends Tool { ...@@ -220,20 +220,11 @@ public class ChangeFileEncryption extends Tool {
if (FileUtils.isDirectory(fileName)) { if (FileUtils.isDirectory(fileName)) {
return; return;
} }
FileChannel fileIn = FilePath.get(fileName).open("r");
FileChannel fileOut = null;
String temp = directory + "/temp.db"; String temp = directory + "/temp.db";
try { try (FileChannel fileIn = getFileChannel(fileName, "r", decryptKey)){
if (decryptKey != null) { try(InputStream inStream = new FileChannelInputStream(fileIn, true)) {
fileIn = new FilePathEncrypt.FileEncrypt(fileName, decryptKey, fileIn);
}
InputStream inStream = new FileChannelInputStream(fileIn, true);
FileUtils.delete(temp); FileUtils.delete(temp);
fileOut = FilePath.get(temp).open("rw"); try (OutputStream outStream = new FileChannelOutputStream(getFileChannel(temp, "rw", encryptKey), true)) {
if (encryptKey != null) {
fileOut = new FilePathEncrypt.FileEncrypt(temp, encryptKey, fileOut);
}
OutputStream outStream = new FileChannelOutputStream(fileOut, true);
byte[] buffer = new byte[4 * 1024]; byte[] buffer = new byte[4 * 1024];
long remaining = fileIn.size(); long remaining = fileIn.size();
long total = remaining; long total = remaining;
...@@ -248,18 +239,21 @@ public class ChangeFileEncryption extends Tool { ...@@ -248,18 +239,21 @@ public class ChangeFileEncryption extends Tool {
outStream.write(buffer, 0, len); outStream.write(buffer, 0, len);
remaining -= len; remaining -= len;
} }
inStream.close(); }
outStream.close();
} finally {
fileIn.close();
if (fileOut != null) {
fileOut.close();
} }
} }
FileUtils.delete(fileName); FileUtils.delete(fileName);
FileUtils.move(temp, fileName); FileUtils.move(temp, fileName);
} }
private FileChannel getFileChannel(String fileName, String r, byte[] decryptKey) throws IOException {
FileChannel fileIn = FilePath.get(fileName).open(r);
if (decryptKey != null) {
fileIn = new FilePathEncrypt.FileEncrypt(fileName, decryptKey, fileIn);
}
return fileIn;
}
private void copy(String fileName, FileStore in, byte[] key, boolean quiet) { private void copy(String fileName, FileStore in, byte[] key, boolean quiet) {
if (FileUtils.isDirectory(fileName)) { if (FileUtils.isDirectory(fileName)) {
return; return;
......
...@@ -155,7 +155,7 @@ public class Restore extends Tool { ...@@ -155,7 +155,7 @@ public class Restore extends Tool {
originalDbLen = originalDbName.length(); originalDbLen = originalDbName.length();
} }
in = FileUtils.newInputStream(zipFileName); in = FileUtils.newInputStream(zipFileName);
ZipInputStream zipIn = new ZipInputStream(in); try (ZipInputStream zipIn = new ZipInputStream(in)) {
while (true) { while (true) {
ZipEntry entry = zipIn.getNextEntry(); ZipEntry entry = zipIn.getNextEntry();
if (entry == null) { if (entry == null) {
...@@ -189,7 +189,7 @@ public class Restore extends Tool { ...@@ -189,7 +189,7 @@ public class Restore extends Tool {
zipIn.closeEntry(); zipIn.closeEntry();
} }
zipIn.closeEntry(); zipIn.closeEntry();
zipIn.close(); }
} catch (IOException e) { } catch (IOException e) {
throw DbException.convertIOException(e, zipFileName); throw DbException.convertIOException(e, zipFileName);
} finally { } finally {
......
...@@ -167,11 +167,8 @@ public class Profiler implements Runnable { ...@@ -167,11 +167,8 @@ public class Profiler implements Runnable {
continue; continue;
} }
String file = arg; String file = arg;
Reader reader; try (Reader reader = new InputStreamReader(new FileInputStream(file), "CP1252")) {
LineNumberReader r; LineNumberReader r = new LineNumberReader(reader);
reader = new InputStreamReader(
new FileInputStream(file), "CP1252");
r = new LineNumberReader(reader);
while (true) { while (true) {
String line = r.readLine(); String line = r.readLine();
if (line == null) { if (line == null) {
...@@ -180,12 +177,11 @@ public class Profiler implements Runnable { ...@@ -180,12 +177,11 @@ public class Profiler implements Runnable {
threadDumps++; threadDumps++;
} }
} }
reader.close(); }
reader = new InputStreamReader( try (Reader reader = new InputStreamReader(new FileInputStream(file), "CP1252")) {
new FileInputStream(file), "CP1252"); LineNumberReader r = new LineNumberReader(reader);
r = new LineNumberReader(reader);
processList(readStackTrace(r)); processList(readStackTrace(r));
reader.close(); }
} }
System.out.println(getTopTraces(5)); System.out.println(getTopTraces(5));
} catch (IOException e) { } catch (IOException e) {
......
...@@ -112,7 +112,7 @@ public class SortedProperties extends Properties { ...@@ -112,7 +112,7 @@ public class SortedProperties extends Properties {
} catch (Exception e) { } catch (Exception e) {
throw new IOException(e.toString(), e); throw new IOException(e.toString(), e);
} }
PrintWriter writer = new PrintWriter(new BufferedWriter(w)); try (PrintWriter writer = new PrintWriter(new BufferedWriter(w))) {
while (true) { while (true) {
String line = r.readLine(); String line = r.readLine();
if (line == null) { if (line == null) {
...@@ -122,7 +122,7 @@ public class SortedProperties extends Properties { ...@@ -122,7 +122,7 @@ public class SortedProperties extends Properties {
writer.print(line + "\n"); writer.print(line + "\n");
} }
} }
writer.close(); }
} }
/** /**
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论