ArchiveTest: Open auto-closeable resources in try-with-resource

Change-Id: If11017f21027b46c7a66e52e4bc0cc73f4fbdc07
Signed-off-by: David Pursehouse <david.pursehouse@gmail.com>
This commit is contained in:
David Pursehouse 2018-03-05 20:23:03 +09:00
parent 9e4a455f9b
commit 594722530b
1 changed files with 34 additions and 41 deletions

View File

@ -633,9 +633,8 @@ private void grepForEntry(String name, String mode, String... cmdline)
}
private void assertMagic(long offset, byte[] magicBytes, File file) throws Exception {
BufferedInputStream in = new BufferedInputStream(
new FileInputStream(file));
try {
try (BufferedInputStream in = new BufferedInputStream(
new FileInputStream(file))) {
if (offset > 0) {
long skipped = in.skip(offset);
assertEquals(offset, skipped);
@ -644,8 +643,6 @@ private void assertMagic(long offset, byte[] magicBytes, File file) throws Excep
byte[] actual = new byte[magicBytes.length];
in.read(actual);
assertArrayEquals(magicBytes, actual);
} finally {
in.close();
}
}
@ -686,23 +683,19 @@ private void assertTarContainsEntry(String tarfile, String mode, String name)
private void writeRaw(String filename, byte[] data)
throws IOException {
File path = new File(db.getWorkTree(), filename);
OutputStream out = new FileOutputStream(path);
try {
try (OutputStream out = new FileOutputStream(path)) {
out.write(data);
} finally {
out.close();
}
}
private static String[] listZipEntries(byte[] zipData) throws IOException {
List<String> l = new ArrayList<>();
ZipInputStream in = new ZipInputStream(
new ByteArrayInputStream(zipData));
try (ZipInputStream in = new ZipInputStream(
new ByteArrayInputStream(zipData))) {
ZipEntry e;
while ((e = in.getNextEntry()) != null)
l.add(e.getName());
in.close();
}
return l.toArray(new String[l.size()]);
}
@ -725,7 +718,7 @@ public Object call() throws IOException {
private String[] listTarEntries(byte[] tarData) throws Exception {
List<String> l = new ArrayList<>();
Process proc = spawnAssumingCommandPresent("tar", "tf", "-");
BufferedReader reader = readFromProcess(proc);
try (BufferedReader reader = readFromProcess(proc)) {
OutputStream out = proc.getOutputStream();
// Dump tarball to tar stdin in background
@ -739,10 +732,10 @@ private String[] listTarEntries(byte[] tarData) throws Exception {
return l.toArray(new String[l.size()]);
} finally {
writing.get();
reader.close();
proc.destroy();
}
}
}
private static String[] zipEntryContent(byte[] zipData, String path)
throws IOException {
@ -771,7 +764,7 @@ private String[] tarEntryContent(byte[] tarData, String path)
throws Exception {
List<String> l = new ArrayList<>();
Process proc = spawnAssumingCommandPresent("tar", "Oxf", "-", path);
BufferedReader reader = readFromProcess(proc);
try (BufferedReader reader = readFromProcess(proc)) {
OutputStream out = proc.getOutputStream();
Future<?> writing = writeAsync(out, tarData);
@ -783,8 +776,8 @@ private String[] tarEntryContent(byte[] tarData, String path)
return l.toArray(new String[l.size()]);
} finally {
writing.get();
reader.close();
proc.destroy();
}
}
}
}