Cleanups: Close resources via try-finally

We can't use try-with-resources until we bump the minimum-supported Android
version for the client library to API 19.
This commit is contained in:
str4d
2017-12-09 01:02:17 +00:00
parent fe5e4a2c7a
commit a67ea4b2f2
14 changed files with 81 additions and 46 deletions

View File

@ -551,11 +551,12 @@ public class CPUID {
URL resource = CPUID.class.getClassLoader().getResource(resourceName);
if (resource == null)
return false;
InputStream libStream = null;
File outFile = null;
FileOutputStream fos = null;
String filename = getLibraryPrefix() + "jcpuid" + getLibrarySuffix();
try {
InputStream libStream = resource.openStream();
libStream = resource.openStream();
outFile = new File(I2PAppContext.getGlobalContext().getTempDir(), filename);
fos = new FileOutputStream(outFile);
DataHelper.copy(libStream, fos);
@ -580,6 +581,7 @@ public class CPUID {
outFile.delete();
return false;
} finally {
if (libStream != null) try { libStream.close(); } catch (IOException ioe) {}
if (fos != null) {
try { fos.close(); } catch (IOException ioe) {}
}

View File

@ -210,11 +210,13 @@ public class SingleFileNamingService extends NamingService {
}
return success;
} catch (IOException ioe) {
if (in != null) try { in.close(); } catch (IOException e) {}
if (out != null) try { out.close(); } catch (IOException e) {}
_log.error("Error adding " + hostname, ioe);
return false;
} finally { releaseWriteLock(); }
} finally {
if (in != null) try { in.close(); } catch (IOException e) {}
if (out != null) try { out.close(); } catch (IOException e) {}
releaseWriteLock();
}
}
/**
@ -333,11 +335,11 @@ public class SingleFileNamingService extends NamingService {
}
return success;
} catch (IOException ioe) {
if (in != null) try { in.close(); } catch (IOException e) {}
if (out != null) try { out.close(); } catch (IOException e) {}
_log.error("Error removing " + hostname, ioe);
return false;
} finally {
if (in != null) try { in.close(); } catch (IOException e) {}
if (out != null) try { out.close(); } catch (IOException e) {}
releaseWriteLock();
}
}

View File

@ -764,13 +764,8 @@ riCe6OlAEiNpcc6mMyIYYWFICbrDFTrDR3wXqwc/Jkcx6L5VVWoagpSzbo3yGhc=
return null;
} finally {
if (bytesToSignInputStream != null)
try {
bytesToSignInputStream.close();
fileInputStream.close();
} catch (IOException ioe) {
}
if (bytesToSignInputStream != null) try { bytesToSignInputStream.close(); } catch (IOException ioe) {}
if (fileInputStream != null) try { fileInputStream.close(); } catch (IOException ioe) {}
}
FileOutputStream fileOutputStream = null;

View File

@ -494,11 +494,12 @@ public class DataHelper {
* or a value contains '#' or '\n'
*/
public static void storeProps(Properties props, File file) throws IOException {
FileOutputStream fos = null;
PrintWriter out = null;
IllegalArgumentException iae = null;
File tmpFile = new File(file.getPath() + ".tmp");
try {
FileOutputStream fos = new SecureFileOutputStream(tmpFile);
fos = new SecureFileOutputStream(tmpFile);
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(fos, "UTF-8")));
out.println("# NOTE: This I2P config file must use UTF-8 encoding");
for (Map.Entry<Object, Object> entry : props.entrySet()) {
@ -533,6 +534,7 @@ public class DataHelper {
throw new IOException("Failed rename from " + tmpFile + " to " + file);
} finally {
if (out != null) out.close();
if (fos != null) try { fos.close(); } catch (IOException ioe) {}
}
if (iae != null)
throw iae;

View File

@ -1119,11 +1119,12 @@ public class NativeBigInteger extends BigInteger {
return false;
}
InputStream libStream = null;
File outFile = null;
FileOutputStream fos = null;
String filename = _libPrefix + "jbigi" + _libSuffix;
try {
InputStream libStream = resource.openStream();
libStream = resource.openStream();
outFile = new File(I2PAppContext.getGlobalContext().getTempDir(), filename);
fos = new FileOutputStream(outFile);
DataHelper.copy(libStream, fos);
@ -1143,6 +1144,7 @@ public class NativeBigInteger extends BigInteger {
outFile.delete();
return false;
} finally {
if (libStream != null) try { libStream.close(); } catch (IOException ioe) {}
if (fos != null) {
try { fos.close(); } catch (IOException ioe) {}
}

View File

@ -395,15 +395,19 @@ public class TranslateReader extends FilterReader {
private static void test(String file) throws IOException {
FileInputStream fio = new FileInputStream(file);
TranslateReader r = new TranslateReader(I2PAppContext.getGlobalContext(),
"net.i2p.router.web.messages",
fio);
int c;
while ((c = r.read()) >= 0) {
System.out.print((char)c);
TranslateReader r = null;
try {
r = new TranslateReader(I2PAppContext.getGlobalContext(),
"net.i2p.router.web.messages",
fio);
int c;
while ((c = r.read()) >= 0) {
System.out.print((char)c);
}
System.out.flush();
} finally {
if (r != null) try { r.close(); } catch (IOException ioe) {}
}
System.out.flush();
r.close();
}
/** @param files ignore 0 */