[freenet-cvs] r14976 - trunk/plugins/XMLSpider

toad at freenetproject.org toad at freenetproject.org
Wed Sep 5 16:55:56 UTC 2007


Author: toad
Date: 2007-09-05 16:55:56 +0000 (Wed, 05 Sep 2007)
New Revision: 14976

Modified:
   trunk/plugins/XMLSpider/XMLSpider.java
Log:
Save some file descriptors by feeding FileOutputStream's to StreamResult and closing them manually. (They seem to be closed lazily if we pass in a File).

Modified: trunk/plugins/XMLSpider/XMLSpider.java
===================================================================
--- trunk/plugins/XMLSpider/XMLSpider.java	2007-09-05 16:32:29 UTC (rev 14975)
+++ trunk/plugins/XMLSpider/XMLSpider.java	2007-09-05 16:55:56 UTC (rev 14976)
@@ -3,7 +3,10 @@
  * http://www.gnu.org/ for further details of the GPL. */
 package plugins.XMLSpider;
 
+import java.io.BufferedOutputStream;
 import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
@@ -323,9 +326,12 @@
 		}
 		//the main index file 
 		File outputFile = new File(DEFAULT_INDEX_DIR+"index.xml");
+		// Use a stream so we can explicitly close - minimise number of filehandles used.
+		BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(outputFile));
 		StreamResult resultStream;
-		resultStream = new StreamResult(outputFile);
+		resultStream = new StreamResult(fos);
 
+		try {
 		/* Initialize xml builder */
 		Document xmlDoc = null;
 		DocumentBuilderFactory xmlFactory = null;
@@ -415,6 +421,9 @@
 			Logger.error(this, "Spider: Error while serializing XML (transform()): "+e.toString(), e);
 			return;
 		}
+		} finally {
+			fos.close();
+		}
 
 		if(Logger.shouldLog(Logger.MINOR, this))
 			Logger.minor(this, "Spider: indexes regenerated - tProducedIndex="+(System.currentTimeMillis()-tProducedIndex)+"ms ago time taken="+time_taken+"ms");
@@ -518,9 +527,11 @@
 		String p = ((String) list.elementAt(0)).substring(0, prefix);
 		indices.add(p);
 		File outputFile = new File(DEFAULT_INDEX_DIR+"index_"+p+".xml");
+		BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(outputFile));
 		StreamResult resultStream;
-		resultStream = new StreamResult(outputFile);
+		resultStream = new StreamResult(fos);
 
+		try {
 		/* Initialize xml builder */
 		Document xmlDoc = null;
 		DocumentBuilderFactory xmlFactory = null;
@@ -631,6 +642,9 @@
 			Logger.error(this, "Spider: Error while serializing XML (transform()): "+e.toString(), e);
 			return;
 		}
+		} finally {
+			fos.close();
+		}
 
 		if(Logger.shouldLog(Logger.MINOR, this))
 			Logger.minor(this, "Spider: indexes regenerated.");
@@ -735,8 +749,16 @@
 	public void generateSubIndex(String filename){
 //		generates the new subIndex
 		File outputFile = new File(filename);
+		BufferedOutputStream fos;
+		try {
+			fos = new BufferedOutputStream(new FileOutputStream(outputFile));
+		} catch (FileNotFoundException e1) {
+			Logger.error(this, "Cannot open "+filename+" writing index : "+e1, e1);
+			return;
+		}
+		try {
 		StreamResult resultStream;
-		resultStream = new StreamResult(outputFile);
+		resultStream = new StreamResult(fos);
 
 		/* Initialize xml builder */
 		Document xmlDoc = null;
@@ -829,6 +851,13 @@
 			Logger.error(this, "Spider: Error while serializing XML (transform()): "+e.toString(), e);
 			return;
 		}
+		} finally {
+			try {
+				fos.close();
+			} catch (IOException e) {
+				// Ignore
+			}
+		}
 
 		if(Logger.shouldLog(Logger.MINOR, this))
 			Logger.minor(this, "Spider: indexes regenerated.");




More information about the cvs mailing list