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;
|
return;
|
||||||
}
|
}
|
||||||
boolean init = !(new File(args[0])).exists();
|
boolean init = !(new File(args[0])).exists();
|
||||||
|
RAIFile raif = null;
|
||||||
|
BlockFile bf = null;
|
||||||
try {
|
try {
|
||||||
RAIFile raif = new RAIFile(new File(args[0]), true, true);
|
raif = new RAIFile(new File(args[0]), true, true);
|
||||||
BlockFile bf = new BlockFile(raif, init);
|
bf = new BlockFile(raif, init);
|
||||||
bf.bfck(true);
|
bf.bfck(true);
|
||||||
bf.close();
|
|
||||||
raif.close();
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
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) {
|
private void runRun(String args[], Logging l) {
|
||||||
if (args.length == 1) {
|
if (args.length == 1) {
|
||||||
|
BufferedReader br = null;
|
||||||
try {
|
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;
|
String line;
|
||||||
while ((line = br.readLine()) != null) {
|
while ((line = br.readLine()) != null) {
|
||||||
runCommand(line, l);
|
runCommand(line, l);
|
||||||
@ -1619,6 +1620,8 @@ public class I2PTunnel extends EventDispatcherImpl implements Logging {
|
|||||||
l.log("IO error running the file");
|
l.log("IO error running the file");
|
||||||
_log.error(getPrefix() + "Error running the file", ioe);
|
_log.error(getPrefix() + "Error running the file", ioe);
|
||||||
notifyEvent("runResult", "error");
|
notifyEvent("runResult", "error");
|
||||||
|
} finally {
|
||||||
|
if (br != null) try { br.close(); } catch (IOException ioe) {}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
l.log("run <commandfile>\n" +
|
l.log("run <commandfile>\n" +
|
||||||
|
@ -159,7 +159,9 @@ public class I2Ping extends I2PTunnelClientBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (hostListFile != null) {
|
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;
|
String line;
|
||||||
List<PingHandler> pingHandlers = new ArrayList<PingHandler>();
|
List<PingHandler> pingHandlers = new ArrayList<PingHandler>();
|
||||||
int i = 0;
|
int i = 0;
|
||||||
@ -181,6 +183,9 @@ public class I2Ping extends I2PTunnelClientBase {
|
|||||||
for (Thread t : pingHandlers)
|
for (Thread t : pingHandlers)
|
||||||
t.join();
|
t.join();
|
||||||
return;
|
return;
|
||||||
|
} finally {
|
||||||
|
if (br != null) try { br.close(); } catch (IOException ioe) {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String host = argv[g.getOptind()];
|
String host = argv[g.getOptind()];
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package net.i2p.jetty;
|
package net.i2p.jetty;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
// Contains code from org.mortbay.xml.XmlConfiguation:
|
// Contains code from org.mortbay.xml.XmlConfiguation:
|
||||||
|
|
||||||
// ========================================================================
|
// ========================================================================
|
||||||
@ -17,6 +19,7 @@ package net.i2p.jetty;
|
|||||||
// ========================================================================
|
// ========================================================================
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.net.URL;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -79,14 +82,24 @@ public class JettyStart implements ClientApp {
|
|||||||
public void parseArgs(String[] args) throws Exception {
|
public void parseArgs(String[] args) throws Exception {
|
||||||
Properties properties=new Properties();
|
Properties properties=new Properties();
|
||||||
XmlConfiguration last=null;
|
XmlConfiguration last=null;
|
||||||
InputStream in = null;
|
Resource r = null;
|
||||||
for (int i = 0; i < args.length; i++) {
|
for (int i = 0; i < args.length; i++) {
|
||||||
if (args[i].toLowerCase().endsWith(".properties")) {
|
if (args[i].toLowerCase().endsWith(".properties")) {
|
||||||
in = Resource.newResource(args[i]).getInputStream();
|
try {
|
||||||
properties.load(in);
|
r = Resource.newResource(args[i]);
|
||||||
in.close();
|
properties.load(r.getInputStream());
|
||||||
|
} finally {
|
||||||
|
if (r != null) r.close();
|
||||||
|
}
|
||||||
} else {
|
} 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)
|
if (last!=null)
|
||||||
configuration.getIdMap().putAll(last.getIdMap());
|
configuration.getIdMap().putAll(last.getIdMap());
|
||||||
if (properties.size()>0) {
|
if (properties.size()>0) {
|
||||||
|
@ -173,11 +173,9 @@ public class LogsHelper extends HelperBase {
|
|||||||
*/
|
*/
|
||||||
private static String readTextFile(File f, int maxNumLines) {
|
private static String readTextFile(File f, int maxNumLines) {
|
||||||
if (!f.exists()) return null;
|
if (!f.exists()) return null;
|
||||||
FileInputStream fis = null;
|
|
||||||
BufferedReader in = null;
|
BufferedReader in = null;
|
||||||
try {
|
try {
|
||||||
fis = new FileInputStream(f);
|
in = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
|
||||||
in = new BufferedReader(new InputStreamReader(fis));
|
|
||||||
List<String> lines = new ArrayList<String>(maxNumLines);
|
List<String> lines = new ArrayList<String>(maxNumLines);
|
||||||
String line = null;
|
String line = null;
|
||||||
while ( (line = in.readLine()) != null) {
|
while ( (line = in.readLine()) != null) {
|
||||||
|
@ -243,10 +243,11 @@ public class SAMStreamSink {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
|
DatagramSocket dg = null;
|
||||||
byte[] buf = new byte[32768];
|
byte[] buf = new byte[32768];
|
||||||
try {
|
try {
|
||||||
Sink sink = new Sink("FAKE", "FAKEFROM");
|
Sink sink = new Sink("FAKE", "FAKEFROM");
|
||||||
DatagramSocket dg = new DatagramSocket(V3DGPORT);
|
dg = new DatagramSocket(V3DGPORT);
|
||||||
while (true) {
|
while (true) {
|
||||||
DatagramPacket p = new DatagramPacket(buf, 32768);
|
DatagramPacket p = new DatagramPacket(buf, 32768);
|
||||||
dg.receive(p);
|
dg.receive(p);
|
||||||
@ -283,6 +284,8 @@ public class SAMStreamSink {
|
|||||||
}
|
}
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
_log.error("DGRcvr", 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;
|
long done = System.currentTimeMillis() + MAX_WAIT_TIME;
|
||||||
for (int i = 0; i < MAX_TRIES; i++) {
|
for (int i = 0; i < MAX_TRIES; i++) {
|
||||||
try {
|
try {
|
||||||
Socket test = new Socket();
|
Socket test = null;
|
||||||
// this will usually fail right away if it's going to fail since it's local
|
|
||||||
test.connect(sa, WAIT_TIME);
|
|
||||||
// it worked
|
|
||||||
try {
|
try {
|
||||||
test.close();
|
test = new Socket();
|
||||||
} catch (IOException ioe) {}
|
// 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
|
// Jetty 6 seems to start the Connector before the
|
||||||
// webapp is completely ready
|
// webapp is completely ready
|
||||||
try {
|
try {
|
||||||
|
@ -551,11 +551,12 @@ public class CPUID {
|
|||||||
URL resource = CPUID.class.getClassLoader().getResource(resourceName);
|
URL resource = CPUID.class.getClassLoader().getResource(resourceName);
|
||||||
if (resource == null)
|
if (resource == null)
|
||||||
return false;
|
return false;
|
||||||
|
InputStream libStream = null;
|
||||||
File outFile = null;
|
File outFile = null;
|
||||||
FileOutputStream fos = null;
|
FileOutputStream fos = null;
|
||||||
String filename = getLibraryPrefix() + "jcpuid" + getLibrarySuffix();
|
String filename = getLibraryPrefix() + "jcpuid" + getLibrarySuffix();
|
||||||
try {
|
try {
|
||||||
InputStream libStream = resource.openStream();
|
libStream = resource.openStream();
|
||||||
outFile = new File(I2PAppContext.getGlobalContext().getTempDir(), filename);
|
outFile = new File(I2PAppContext.getGlobalContext().getTempDir(), filename);
|
||||||
fos = new FileOutputStream(outFile);
|
fos = new FileOutputStream(outFile);
|
||||||
DataHelper.copy(libStream, fos);
|
DataHelper.copy(libStream, fos);
|
||||||
@ -580,6 +581,7 @@ public class CPUID {
|
|||||||
outFile.delete();
|
outFile.delete();
|
||||||
return false;
|
return false;
|
||||||
} finally {
|
} finally {
|
||||||
|
if (libStream != null) try { libStream.close(); } catch (IOException ioe) {}
|
||||||
if (fos != null) {
|
if (fos != null) {
|
||||||
try { fos.close(); } catch (IOException ioe) {}
|
try { fos.close(); } catch (IOException ioe) {}
|
||||||
}
|
}
|
||||||
|
@ -210,11 +210,13 @@ public class SingleFileNamingService extends NamingService {
|
|||||||
}
|
}
|
||||||
return success;
|
return success;
|
||||||
} catch (IOException ioe) {
|
} 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);
|
_log.error("Error adding " + hostname, ioe);
|
||||||
return false;
|
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;
|
return success;
|
||||||
} catch (IOException ioe) {
|
} 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);
|
_log.error("Error removing " + hostname, ioe);
|
||||||
return false;
|
return false;
|
||||||
} finally {
|
} finally {
|
||||||
|
if (in != null) try { in.close(); } catch (IOException e) {}
|
||||||
|
if (out != null) try { out.close(); } catch (IOException e) {}
|
||||||
releaseWriteLock();
|
releaseWriteLock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -764,13 +764,8 @@ riCe6OlAEiNpcc6mMyIYYWFICbrDFTrDR3wXqwc/Jkcx6L5VVWoagpSzbo3yGhc=
|
|||||||
|
|
||||||
return null;
|
return null;
|
||||||
} finally {
|
} finally {
|
||||||
if (bytesToSignInputStream != null)
|
if (bytesToSignInputStream != null) try { bytesToSignInputStream.close(); } catch (IOException ioe) {}
|
||||||
try {
|
if (fileInputStream != null) try { fileInputStream.close(); } catch (IOException ioe) {}
|
||||||
bytesToSignInputStream.close();
|
|
||||||
fileInputStream.close();
|
|
||||||
} catch (IOException ioe) {
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
FileOutputStream fileOutputStream = null;
|
FileOutputStream fileOutputStream = null;
|
||||||
|
@ -494,11 +494,12 @@ public class DataHelper {
|
|||||||
* or a value contains '#' or '\n'
|
* or a value contains '#' or '\n'
|
||||||
*/
|
*/
|
||||||
public static void storeProps(Properties props, File file) throws IOException {
|
public static void storeProps(Properties props, File file) throws IOException {
|
||||||
|
FileOutputStream fos = null;
|
||||||
PrintWriter out = null;
|
PrintWriter out = null;
|
||||||
IllegalArgumentException iae = null;
|
IllegalArgumentException iae = null;
|
||||||
File tmpFile = new File(file.getPath() + ".tmp");
|
File tmpFile = new File(file.getPath() + ".tmp");
|
||||||
try {
|
try {
|
||||||
FileOutputStream fos = new SecureFileOutputStream(tmpFile);
|
fos = new SecureFileOutputStream(tmpFile);
|
||||||
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(fos, "UTF-8")));
|
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(fos, "UTF-8")));
|
||||||
out.println("# NOTE: This I2P config file must use UTF-8 encoding");
|
out.println("# NOTE: This I2P config file must use UTF-8 encoding");
|
||||||
for (Map.Entry<Object, Object> entry : props.entrySet()) {
|
for (Map.Entry<Object, Object> entry : props.entrySet()) {
|
||||||
@ -533,6 +534,7 @@ public class DataHelper {
|
|||||||
throw new IOException("Failed rename from " + tmpFile + " to " + file);
|
throw new IOException("Failed rename from " + tmpFile + " to " + file);
|
||||||
} finally {
|
} finally {
|
||||||
if (out != null) out.close();
|
if (out != null) out.close();
|
||||||
|
if (fos != null) try { fos.close(); } catch (IOException ioe) {}
|
||||||
}
|
}
|
||||||
if (iae != null)
|
if (iae != null)
|
||||||
throw iae;
|
throw iae;
|
||||||
|
@ -1119,11 +1119,12 @@ public class NativeBigInteger extends BigInteger {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
InputStream libStream = null;
|
||||||
File outFile = null;
|
File outFile = null;
|
||||||
FileOutputStream fos = null;
|
FileOutputStream fos = null;
|
||||||
String filename = _libPrefix + "jbigi" + _libSuffix;
|
String filename = _libPrefix + "jbigi" + _libSuffix;
|
||||||
try {
|
try {
|
||||||
InputStream libStream = resource.openStream();
|
libStream = resource.openStream();
|
||||||
outFile = new File(I2PAppContext.getGlobalContext().getTempDir(), filename);
|
outFile = new File(I2PAppContext.getGlobalContext().getTempDir(), filename);
|
||||||
fos = new FileOutputStream(outFile);
|
fos = new FileOutputStream(outFile);
|
||||||
DataHelper.copy(libStream, fos);
|
DataHelper.copy(libStream, fos);
|
||||||
@ -1143,6 +1144,7 @@ public class NativeBigInteger extends BigInteger {
|
|||||||
outFile.delete();
|
outFile.delete();
|
||||||
return false;
|
return false;
|
||||||
} finally {
|
} finally {
|
||||||
|
if (libStream != null) try { libStream.close(); } catch (IOException ioe) {}
|
||||||
if (fos != null) {
|
if (fos != null) {
|
||||||
try { fos.close(); } catch (IOException ioe) {}
|
try { fos.close(); } catch (IOException ioe) {}
|
||||||
}
|
}
|
||||||
|
@ -395,15 +395,19 @@ public class TranslateReader extends FilterReader {
|
|||||||
|
|
||||||
private static void test(String file) throws IOException {
|
private static void test(String file) throws IOException {
|
||||||
FileInputStream fio = new FileInputStream(file);
|
FileInputStream fio = new FileInputStream(file);
|
||||||
TranslateReader r = new TranslateReader(I2PAppContext.getGlobalContext(),
|
TranslateReader r = null;
|
||||||
"net.i2p.router.web.messages",
|
try {
|
||||||
fio);
|
r = new TranslateReader(I2PAppContext.getGlobalContext(),
|
||||||
int c;
|
"net.i2p.router.web.messages",
|
||||||
while ((c = r.read()) >= 0) {
|
fio);
|
||||||
System.out.print((char)c);
|
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 */
|
/** @param files ignore 0 */
|
||||||
|
@ -194,8 +194,8 @@ public class HTTPMUSocket
|
|||||||
|
|
||||||
public boolean send(String msg, String bindAddr, int bindPort)
|
public boolean send(String msg, String bindAddr, int bindPort)
|
||||||
{
|
{
|
||||||
|
MulticastSocket msock = null;
|
||||||
try {
|
try {
|
||||||
MulticastSocket msock;
|
|
||||||
if ((bindAddr) != null && (0 < bindPort)) {
|
if ((bindAddr) != null && (0 < bindPort)) {
|
||||||
msock = new MulticastSocket(null);
|
msock = new MulticastSocket(null);
|
||||||
msock.bind(new InetSocketAddress(bindAddr, bindPort));
|
msock.bind(new InetSocketAddress(bindAddr, bindPort));
|
||||||
@ -206,11 +206,12 @@ public class HTTPMUSocket
|
|||||||
// Thnaks for Theo Beisch (11/09/04)
|
// Thnaks for Theo Beisch (11/09/04)
|
||||||
msock.setTimeToLive(UPnP.getTimeToLive());
|
msock.setTimeToLive(UPnP.getTimeToLive());
|
||||||
msock.send(dgmPacket);
|
msock.send(dgmPacket);
|
||||||
msock.close();
|
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
Debug.warning(e);
|
Debug.warning(e);
|
||||||
return false;
|
return false;
|
||||||
|
} finally {
|
||||||
|
if (msock != null) msock.close();
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user