forked from I2P_Developers/i2p.i2p
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:
@ -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) {}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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" +
|
||||
|
@ -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()];
|
||||
|
@ -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) {
|
||||
|
@ -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) {
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
Socket test = null;
|
||||
try {
|
||||
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
|
||||
try {
|
||||
test.close();
|
||||
} catch (IOException ioe) {}
|
||||
} finally {
|
||||
if (test != null) try { test.close(); } catch (IOException ioe) {}
|
||||
}
|
||||
// Jetty 6 seems to start the Connector before the
|
||||
// webapp is completely ready
|
||||
try {
|
||||
|
@ -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) {}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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) {}
|
||||
}
|
||||
|
@ -395,7 +395,9 @@ public class TranslateReader extends FilterReader {
|
||||
|
||||
private static void test(String file) throws IOException {
|
||||
FileInputStream fio = new FileInputStream(file);
|
||||
TranslateReader r = new TranslateReader(I2PAppContext.getGlobalContext(),
|
||||
TranslateReader r = null;
|
||||
try {
|
||||
r = new TranslateReader(I2PAppContext.getGlobalContext(),
|
||||
"net.i2p.router.web.messages",
|
||||
fio);
|
||||
int c;
|
||||
@ -403,7 +405,9 @@ public class TranslateReader extends FilterReader {
|
||||
System.out.print((char)c);
|
||||
}
|
||||
System.out.flush();
|
||||
r.close();
|
||||
} finally {
|
||||
if (r != null) try { r.close(); } catch (IOException ioe) {}
|
||||
}
|
||||
}
|
||||
|
||||
/** @param files ignore 0 */
|
||||
|
@ -194,8 +194,8 @@ public class HTTPMUSocket
|
||||
|
||||
public boolean send(String msg, String bindAddr, int bindPort)
|
||||
{
|
||||
MulticastSocket msock = null;
|
||||
try {
|
||||
MulticastSocket msock;
|
||||
if ((bindAddr) != null && (0 < bindPort)) {
|
||||
msock = new MulticastSocket(null);
|
||||
msock.bind(new InetSocketAddress(bindAddr, bindPort));
|
||||
@ -206,11 +206,12 @@ public class HTTPMUSocket
|
||||
// Thnaks for Theo Beisch (11/09/04)
|
||||
msock.setTimeToLive(UPnP.getTimeToLive());
|
||||
msock.send(dgmPacket);
|
||||
msock.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
Debug.warning(e);
|
||||
return false;
|
||||
} finally {
|
||||
if (msock != null) msock.close();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
Reference in New Issue
Block a user