Distributed Peer-to-Peer Web Search Engine and Intranet Search Appliance
Java decentralizedintranet-searchintranet-search-enginep2pp2p-networkprivacyprivacy-protectionsearch-enginesearchenginewebweb-search-enginewebsearchwebseearch-engine
Properly close resources (even on error) on OS and ThreadDump classes.
luccioman6e497241f78e
diff --git a/source/net/yacy/kelondro/logging/ThreadDump.java b/source/net/yacy/kelondro/logging/ThreadDump.javaindex d5a4f6988..1e8d2c78a 100644--- a/source/net/yacy/kelondro/logging/ThreadDump.java+++ b/source/net/yacy/kelondro/logging/ThreadDump.java@@ -114,11 +114,15 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> impreturn true;}+ /**+ * Try to get the thread dump from a yacy.log file which is available when YaCy is started with+ * startYACY.sh -l+ * @param logFile the log file to read+ * @throws IOException when a read/write error occurred+ */public ThreadDump(final File logFile) throws IOException {super();- // try to get the thread dump from yacy.log which is available when YaCy is started with- // startYACY.sh -llong sizeBefore = 0;if (canProduceLockedBy(logFile)) {sizeBefore = logFile.length();@@ -126,7 +130,7 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp// get the current process PIDfinal int pid = OS.getPID();- // call kill -3 on the pid+ // call kill -3 (SIGQUIT) on the pid to request a core dump from the current processif (pid >= 0) try {OS.execSynchronous("kill -3 " + pid);} catch (final IOException e) {}}@@ -134,14 +138,14 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> impfinal long sizeAfter = logFile.length();if (sizeAfter <= sizeBefore) return;- final RandomAccessFile raf = new RandomAccessFile(logFile, "r");- raf.seek(sizeBefore);- final byte[] b = new byte[(int) (sizeAfter - sizeBefore)];- raf.readFully(b);- raf.close();-- // import the thread dump;- importText(new ByteArrayInputStream(b));+ try(final RandomAccessFile raf = new RandomAccessFile(logFile, "r");) {+ raf.seek(sizeBefore);+ final byte[] b = new byte[(int) (sizeAfter - sizeBefore)];+ raf.readFully(b);++ // import the thread dump;+ importText(new ByteArrayInputStream(b));+ }}public ThreadDump(final InputStream is) throws IOException {@@ -446,9 +450,6 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imppublic static void main(final String[] args) {ThreadDump dump = null;- if (args.length == 0) {- //dump = new ThreadDump();- }if (args.length == 2 && args[0].equals("-f")) {final File dumpfile = new File(args[1]);try {@@ -456,8 +457,10 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp} catch (final IOException e) {e.printStackTrace();}+ } else {+ System.out.println("Usage : java " + ThreadDump.class.getName() + " -f /path/to/yacy.log");+ return;}- //dump.print();assert dump != null;final Map<StackTrace, Integer> locks = dump.countLocks();final List<Map.Entry<StackTrace, List<String>>> freerun = dump.freerun();diff --git a/source/net/yacy/kelondro/util/OS.java b/source/net/yacy/kelondro/util/OS.javaindex 7913e6356..f4fa0dd68 100644--- a/source/net/yacy/kelondro/util/OS.java+++ b/source/net/yacy/kelondro/util/OS.java@@ -178,13 +178,21 @@ public final class OS {private static List<String> execSynchronousProcess(Process p) throws IOException {String line;- BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));- final List<String> output = new ArrayList<String>();- while ((line = in.readLine()) != null) output.add(line);- in.close();- in = new BufferedReader(new InputStreamReader(p.getErrorStream()));- while ((line = in.readLine()) != null) output.add(line);- in.close();+ final List<String> output = new ArrayList<String>();++ try (final InputStreamReader streamReader = new InputStreamReader(p.getInputStream());+ final BufferedReader in = new BufferedReader(streamReader);) {+ while ((line = in.readLine()) != null) {+ output.add(line);+ }+ }++ try (final InputStreamReader streamReader = new InputStreamReader(p.getErrorStream());+ final BufferedReader in = new BufferedReader(streamReader);) {+ while ((line = in.readLine()) != null) {+ output.add(line);+ }+ }return output;}