*) adding many missing (File)?(Input|Output)Stream.close() calls to avoid "Too many open files bug".

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@90 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
theli 20 years ago
parent ca93d02cca
commit cbdc499ba6

@ -470,13 +470,16 @@ public final class httpc {
// this writes the input stream to either another output stream or
// a file or both.
FileOutputStream bufferOS = null;
try {
if (file != null) bufferOS = new FileOutputStream(file);
writeContentX(procOS, bufferOS, httpc.this.clientInput);
} finally {
if (bufferOS != null) {
bufferOS.close();
if (file.length() == 0) file.delete();
}
}
}
public void writeContentX(OutputStream procOS, OutputStream bufferOS, InputStream clientInput) throws IOException {
// we write length bytes, but if length == -1 (or < 0) then we
@ -563,9 +566,9 @@ public final class httpc {
public void close() {
// closes the connection
try {
clientInput.close();
clientOutput.close();
socket.close();
this.clientInput.close();
this.clientOutput.close();
this.socket.close();
} catch (IOException e) {}
}

@ -116,14 +116,17 @@ public final class httpdFileHandler extends httpdAbstractHandler implements http
public httpdFileHandler(serverSwitch switchboard) {
this.switchboard = switchboard;
if (mimeTable == null) {
if (this.mimeTable == null) {
// load the mime table
mimeTable = new Properties();
this.mimeTable = new Properties();
String mimeTablePath = switchboard.getConfig("mimeConfig","");
FileInputStream mimeTableInputStream = null;
try {
serverLog.logSystem("HTTPDFiles", "Loading mime mapping file " + mimeTablePath);
mimeTable.load(new FileInputStream(new File(switchboard.getRootPath(), mimeTablePath)));
mimeTableInputStream = new FileInputStream(new File(switchboard.getRootPath(), mimeTablePath));
this.mimeTable.load(mimeTableInputStream);
} catch (Exception e) {
if (mimeTableInputStream != null) try { mimeTableInputStream.close(); } catch (Exception e1) {}
serverLog.logError("HTTPDFiles", "ERROR: path to configuration file or configuration invalid\n" + e);
System.exit(1);
}
@ -416,11 +419,18 @@ public final class httpdFileHandler extends httpdAbstractHandler implements http
// read templates
tp.putAll(templates);
// rewrite the file
ByteArrayOutputStream o = new ByteArrayOutputStream();
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream o = null;
FileInputStream fis = null;
try {
o = new ByteArrayOutputStream();
fis = new FileInputStream(file);
httpTemplate.writeTemplate(fis, o, tp, "-UNRESOLVED_PATTERN-".getBytes());
o.close();
result = o.toByteArray();
} finally {
if (o != null) try {o.close();} catch(Exception e) {}
if (fis != null) try {fis.close();} catch(Exception e) {}
}
} else { // no html
// write the file to the client
result = serverFileUtils.read(file);
@ -459,7 +469,8 @@ public final class httpdFileHandler extends httpdAbstractHandler implements http
String[] templates = path.list();
int c;
for (int i = 0; i < templates.length; i++) {
if (templates[i].endsWith(".template")) try {
if (templates[i].endsWith(".template"))
try {
//System.out.println("TEMPLATE " + templates[i].substring(0, templates[i].length() - 9) + ": " + new String(buf, 0, c));
result.put(templates[i].substring(0, templates[i].length() - 9),
new String(serverFileUtils.read(new File(path, templates[i]))));

@ -165,8 +165,9 @@ public final class httpdProxyHandler extends httpdAbstractHandler implements htt
private static HashSet loadSet(String setname, String filename) {
HashSet set = new HashSet();
BufferedReader br = null;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
br = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
String line;
while ((line = br.readLine()) != null) {
line = line.trim();
@ -174,14 +175,18 @@ public final class httpdProxyHandler extends httpdAbstractHandler implements htt
}
br.close();
serverLog.logInfo("PROXY", "read " + setname + " set from file " + filename);
} catch (IOException e) {}
} catch (IOException e) {
} finally {
if (br != null) try { br.close(); } catch (Exception e) {}
}
return set;
}
private static TreeMap loadMap(String mapname, String filename, String sep) {
TreeMap map = new TreeMap();
BufferedReader br = null;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
br = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
String line;
int pos;
while ((line = br.readLine()) != null) {
@ -189,9 +194,11 @@ public final class httpdProxyHandler extends httpdAbstractHandler implements htt
if ((line.length() > 0) && (!(line.startsWith("#"))) && ((pos = line.indexOf(sep)) > 0))
map.put(line.substring(0, pos).trim().toLowerCase(), line.substring(pos + sep.length()).trim());
}
br.close();
serverLog.logInfo("PROXY", "read " + mapname + " map from file " + filename);
} catch (IOException e) {}
} catch (IOException e) {
} finally {
if (br != null) try { br.close(); } catch (Exception e) {}
}
return map;
}
@ -200,7 +207,7 @@ public final class httpdProxyHandler extends httpdAbstractHandler implements htt
if (switchboard == null) return map; // not initialized yet
File listsPath = new File(switchboard.getRootPath(), switchboard.getConfig("listsPath", "DATA/LISTS"));
String filenamesarray[] = filenames.split(",");
String filename = "";
if(filenamesarray.length >0)
for(int i = 0; i < filenamesarray.length; i++)
map.putAll(loadMap(mapname, (new File(listsPath, filenamesarray[i])).toString(), sep));
@ -445,11 +452,15 @@ public final class httpdProxyHandler extends httpdAbstractHandler implements htt
// send also the complete body now from the cache
// simply read the file and transfer to out socket
InputStream is = new FileInputStream(cacheFile);
InputStream is = null;
try {
is = new FileInputStream(cacheFile);
byte[] buffer = new byte[2048];
int l;
while ((l = is.read(buffer)) > 0) {hfos.write(buffer, 0, l);}
is.close();
} finally {
if (is != null) try { is.close(); } catch (Exception e) {}
}
if (hfos instanceof htmlFilterOutputStream) ((htmlFilterOutputStream) hfos).finalize();
}
// that's it!
@ -607,7 +618,6 @@ public final class httpdProxyHandler extends httpdAbstractHandler implements htt
e.printStackTrace();
}
}
remote.close();
} catch (Exception e) {
// this may happen if the targeted host does not exist or anything with the
// remote server was wrong.
@ -632,6 +642,7 @@ public final class httpdProxyHandler extends httpdAbstractHandler implements htt
private void respondError(OutputStream respond, String origerror, int errorcase, String url) {
FileInputStream fis = null;
try {
// set rewrite values
serverObjects tp = new serverObjects();
@ -643,7 +654,7 @@ public final class httpdProxyHandler extends httpdAbstractHandler implements htt
File file = new File(htRootPath, "/proxymsg/error.html");
byte[] result;
ByteArrayOutputStream o = new ByteArrayOutputStream();
FileInputStream fis = new FileInputStream(file);
fis = new FileInputStream(file);
httpTemplate.writeTemplate(fis, o, tp, "-UNRESOLVED_PATTERN-".getBytes());
o.close();
result = o.toByteArray();
@ -660,7 +671,8 @@ public final class httpdProxyHandler extends httpdAbstractHandler implements htt
serverFileUtils.write(result, respond);
respond.flush();
} catch (IOException e) {
} finally {
if (fis != null) try { fis.close(); } catch (Exception e) {}
}
}

@ -59,23 +59,36 @@ public final class serverFileUtils {
}
public static void copy(InputStream source, File dest) throws IOException {
FileOutputStream fos = new FileOutputStream(dest);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(dest);
copy(source, fos);
fos.close();
} finally {
if (fos != null) try {fos.close();} catch (Exception e) {}
}
}
public static void copy(File source, OutputStream dest) throws IOException {
InputStream fis = new FileInputStream(source);
InputStream fis = null;
try {
fis = new FileInputStream(source);
copy(fis, dest);
fis.close();
} finally {
if (fis != null) try { fis.close(); } catch (Exception e) {}
}
}
public static void copy(File source, File dest) throws IOException {
FileInputStream fis = new FileInputStream(source);
FileOutputStream fos = new FileOutputStream(dest);
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(source);
fos = new FileOutputStream(dest);
copy(fis, fos);
fis.close();
fos.close();
} finally {
if (fis != null) try {fis.close();} catch (Exception e) {}
if (fos != null) try {fos.close();} catch (Exception e) {}
}
}
public static byte[] read(InputStream source) throws IOException {
@ -87,11 +100,14 @@ public final class serverFileUtils {
public static byte[] read(File source) throws IOException {
byte[] buffer = new byte[(int) source.length()];
InputStream fis = new FileInputStream(source);
int p = 0;
int c;
InputStream fis = null;
try {
fis = new FileInputStream(source);
int p = 0, c;
while ((c = fis.read(buffer, p, buffer.length - p)) > 0) p += c;
fis.close();
} finally {
if (fis != null) try { fis.close(); } catch (Exception e) {}
}
return buffer;
}

Loading…
Cancel
Save