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

@@ -144,14 +144,17 @@ public class BlockFile implements Closeable {
return;
}
boolean init = !(new File(args[0])).exists();
RAIFile raif = null;
BlockFile bf = null;
try {
RAIFile raif = new RAIFile(new File(args[0]), true, true);
BlockFile bf = new BlockFile(raif, init);
raif = new RAIFile(new File(args[0]), true, true);
bf = new BlockFile(raif, init);
bf.bfck(true);
bf.close();
raif.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bf != null) try { bf.close(); } catch (IOException ioe) {}
if (raif != null) try { raif.close(); } catch (IOException ioe) {}
}
}

View File

@@ -1607,8 +1607,9 @@ public class I2PTunnel extends EventDispatcherImpl implements Logging {
*/
private void runRun(String args[], Logging l) {
if (args.length == 1) {
BufferedReader br = null;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(args[0]), "UTF-8"));
br = new BufferedReader(new InputStreamReader(new FileInputStream(args[0]), "UTF-8"));
String line;
while ((line = br.readLine()) != null) {
runCommand(line, l);
@@ -1619,6 +1620,8 @@ public class I2PTunnel extends EventDispatcherImpl implements Logging {
l.log("IO error running the file");
_log.error(getPrefix() + "Error running the file", ioe);
notifyEvent("runResult", "error");
} finally {
if (br != null) try { br.close(); } catch (IOException ioe) {}
}
} else {
l.log("run <commandfile>\n" +

View File

@@ -159,7 +159,9 @@ public class I2Ping extends I2PTunnelClientBase {
}
if (hostListFile != null) {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(hostListFile), "UTF-8"));
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(hostListFile), "UTF-8"));
String line;
List<PingHandler> pingHandlers = new ArrayList<PingHandler>();
int i = 0;
@@ -181,6 +183,9 @@ public class I2Ping extends I2PTunnelClientBase {
for (Thread t : pingHandlers)
t.join();
return;
} finally {
if (br != null) try { br.close(); } catch (IOException ioe) {}
}
}
String host = argv[g.getOptind()];

View File

@@ -1,5 +1,7 @@
package net.i2p.jetty;
import java.io.IOException;
// Contains code from org.mortbay.xml.XmlConfiguation:
// ========================================================================
@@ -17,6 +19,7 @@ package net.i2p.jetty;
// ========================================================================
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -79,14 +82,24 @@ public class JettyStart implements ClientApp {
public void parseArgs(String[] args) throws Exception {
Properties properties=new Properties();
XmlConfiguration last=null;
InputStream in = null;
Resource r = null;
for (int i = 0; i < args.length; i++) {
if (args[i].toLowerCase().endsWith(".properties")) {
in = Resource.newResource(args[i]).getInputStream();
properties.load(in);
in.close();
try {
r = Resource.newResource(args[i]);
properties.load(r.getInputStream());
} finally {
if (r != null) r.close();
}
} else {
XmlConfiguration configuration = new XmlConfiguration(Resource.newResource(args[i]).getURL());
URL configUrl;
try {
r = Resource.newResource(args[i]);
configUrl = r.getURL();
} finally {
if (r != null) r.close();
}
XmlConfiguration configuration = new XmlConfiguration(configUrl);
if (last!=null)
configuration.getIdMap().putAll(last.getIdMap());
if (properties.size()>0) {

View File

@@ -173,11 +173,9 @@ public class LogsHelper extends HelperBase {
*/
private static String readTextFile(File f, int maxNumLines) {
if (!f.exists()) return null;
FileInputStream fis = null;
BufferedReader in = null;
try {
fis = new FileInputStream(f);
in = new BufferedReader(new InputStreamReader(fis));
in = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
List<String> lines = new ArrayList<String>(maxNumLines);
String line = null;
while ( (line = in.readLine()) != null) {

View File

@@ -243,10 +243,11 @@ public class SAMStreamSink {
}
public void run() {
DatagramSocket dg = null;
byte[] buf = new byte[32768];
try {
Sink sink = new Sink("FAKE", "FAKEFROM");
DatagramSocket dg = new DatagramSocket(V3DGPORT);
dg = new DatagramSocket(V3DGPORT);
while (true) {
DatagramPacket p = new DatagramPacket(buf, 32768);
dg.receive(p);
@@ -283,6 +284,8 @@ public class SAMStreamSink {
}
} catch (IOException ioe) {
_log.error("DGRcvr", ioe);
} finally {
if (dg != null) dg.close();
}
}
}

View File

@@ -136,13 +136,15 @@ public class UrlLauncher implements ClientApp {
long done = System.currentTimeMillis() + MAX_WAIT_TIME;
for (int i = 0; i < MAX_TRIES; i++) {
try {
Socket test = new Socket();
// this will usually fail right away if it's going to fail since it's local
test.connect(sa, WAIT_TIME);
// it worked
Socket test = null;
try {
test.close();
} catch (IOException ioe) {}
test = new Socket();
// this will usually fail right away if it's going to fail since it's local
test.connect(sa, WAIT_TIME);
// it worked
} finally {
if (test != null) try { test.close(); } catch (IOException ioe) {}
}
// Jetty 6 seems to start the Connector before the
// webapp is completely ready
try {