From 878525ced8584bf29a632ff033c4d18c9ab4175e Mon Sep 17 00:00:00 2001 From: jrandom Date: Sun, 9 May 2004 04:14:30 +0000 Subject: [PATCH] handle corrupt files more gracefully --- .../net/i2p/netmonitor/PeerSummaryReader.java | 98 ++++++++++--------- 1 file changed, 51 insertions(+), 47 deletions(-) diff --git a/apps/netmonitor/java/src/net/i2p/netmonitor/PeerSummaryReader.java b/apps/netmonitor/java/src/net/i2p/netmonitor/PeerSummaryReader.java index 1a132cf91..8bf62fd33 100644 --- a/apps/netmonitor/java/src/net/i2p/netmonitor/PeerSummaryReader.java +++ b/apps/netmonitor/java/src/net/i2p/netmonitor/PeerSummaryReader.java @@ -33,57 +33,61 @@ class PeerSummaryReader { PeerSummary summary = null; String curDescription = null; List curArgs = null; - while ((line = reader.readLine()) != null) { - if (line.startsWith("peer\t")) { - String name = line.substring("peer\t".length()).trim(); - summary = monitor.getSummary(name); - if (summary == null) - summary = new PeerSummary(name); - } else if (line.startsWith("## ")) { - curDescription = line.substring("## ".length()).trim(); - curArgs = new ArrayList(4); - } else if (line.startsWith("# param ")) { - int start = line.indexOf(':'); - String arg = line.substring(start+1).trim(); - curArgs.add(arg); - } else { - StringTokenizer tok = new StringTokenizer(line); - String name = tok.nextToken(); - try { - long when = getTime(tok.nextToken()); - boolean isDouble = false; - List argVals = new ArrayList(curArgs.size()); - while (tok.hasMoreTokens()) { - String val = (String)tok.nextToken(); - if (val.indexOf('.') >= 0) { - argVals.add(new Double(val)); - isDouble = true; - } else { - argVals.add(new Long(val)); + try { + while ((line = reader.readLine()) != null) { + if (line.startsWith("peer\t")) { + String name = line.substring("peer\t".length()).trim(); + summary = monitor.getSummary(name); + if (summary == null) + summary = new PeerSummary(name); + } else if (line.startsWith("## ")) { + curDescription = line.substring("## ".length()).trim(); + curArgs = new ArrayList(4); + } else if (line.startsWith("# param ")) { + int start = line.indexOf(':'); + String arg = line.substring(start+1).trim(); + curArgs.add(arg); + } else { + StringTokenizer tok = new StringTokenizer(line); + String name = tok.nextToken(); + try { + long when = getTime(tok.nextToken()); + boolean isDouble = false; + List argVals = new ArrayList(curArgs.size()); + while (tok.hasMoreTokens()) { + String val = (String)tok.nextToken(); + if (val.indexOf('.') >= 0) { + argVals.add(new Double(val)); + isDouble = true; + } else { + argVals.add(new Long(val)); + } } + String valDescriptions[] = new String[curArgs.size()]; + for (int i = 0; i < curArgs.size(); i++) + valDescriptions[i] = (String)curArgs.get(i); + if (isDouble) { + double values[] = new double[argVals.size()]; + for (int i = 0; i < argVals.size(); i++) + values[i] = ((Double)argVals.get(i)).doubleValue(); + summary.addData(name, curDescription, valDescriptions, when, values); + } else { + long values[] = new long[argVals.size()]; + for (int i = 0; i < argVals.size(); i++) + values[i] = ((Long)argVals.get(i)).longValue(); + summary.addData(name, curDescription, valDescriptions, when, values); + } + } catch (ParseException pe) { + _log.error("Error parsing the data line [" + line + "]", pe); + } catch (NumberFormatException nfe) { + _log.error("Error parsing the data line [" + line + "]", nfe); } - String valDescriptions[] = new String[curArgs.size()]; - for (int i = 0; i < curArgs.size(); i++) - valDescriptions[i] = (String)curArgs.get(i); - if (isDouble) { - double values[] = new double[argVals.size()]; - for (int i = 0; i < argVals.size(); i++) - values[i] = ((Double)argVals.get(i)).doubleValue(); - summary.addData(name, curDescription, valDescriptions, when, values); - } else { - long values[] = new long[argVals.size()]; - for (int i = 0; i < argVals.size(); i++) - values[i] = ((Long)argVals.get(i)).longValue(); - summary.addData(name, curDescription, valDescriptions, when, values); - } - } catch (ParseException pe) { - _log.error("Error parsing the data line [" + line + "]", pe); - } catch (NumberFormatException nfe) { - _log.error("Error parsing the data line [" + line + "]", nfe); } } - } - + } catch (Exception e) { + _log.error("Error handling the data", e); + throw new IOException("Error parsing the data"); + } if (summary == null) return; summary.coallesceData(monitor.getSummaryDurationHours() * 60*60*1000);