diff --git a/core/java/src/net/i2p/client/naming/MetaNamingService.java b/core/java/src/net/i2p/client/naming/MetaNamingService.java index f5c680549..e07073050 100644 --- a/core/java/src/net/i2p/client/naming/MetaNamingService.java +++ b/core/java/src/net/i2p/client/naming/MetaNamingService.java @@ -3,8 +3,9 @@ package net.i2p.client.naming; import java.lang.reflect.Constructor; import java.util.ArrayList; import java.util.Collections; -import java.util.Iterator; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Properties; import java.util.StringTokenizer; import java.util.concurrent.CopyOnWriteArrayList; @@ -152,4 +153,30 @@ public class MetaNamingService extends DummyNamingService { removeCache(hostname); return rv; } + + /** + * All services aggregated + */ + @Override + public Map getEntries(Properties options) { + Map rv = new HashMap(); + for (NamingService ns : _services) { + rv.putAll(ns.getEntries(options)); + } + return rv; + } + + /** + * All services aggregated + */ + @Override + public int size(Properties options) { + int rv = 0; + for (NamingService ns : _services) { + int s = ns.size(options); + if (s > 0) + rv += s; + } + return rv; + } } diff --git a/core/java/src/net/i2p/client/naming/SingleFileNamingService.java b/core/java/src/net/i2p/client/naming/SingleFileNamingService.java index ba3b2bc2a..962c67f0f 100644 --- a/core/java/src/net/i2p/client/naming/SingleFileNamingService.java +++ b/core/java/src/net/i2p/client/naming/SingleFileNamingService.java @@ -13,10 +13,13 @@ import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.IOException; -import java.util.Iterator; -import java.util.List; import java.io.OutputStream; import java.io.OutputStreamWriter; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; import java.util.Properties; import java.util.Set; import java.util.concurrent.TimeUnit; @@ -283,6 +286,78 @@ public class SingleFileNamingService extends NamingService { } } + /** + * @param options As follows: + * Key "startsWith": return only those starting with + */ + @Override + public Map getEntries(Properties options) { + if (!_file.exists()) + return Collections.EMPTY_MAP; + String startsWith = ""; + if (options != null) { + startsWith = options.getProperty("startsWith"); + } + BufferedReader in = null; + getReadLock(); + try { + in = new BufferedReader(new InputStreamReader(new FileInputStream(_file), "UTF-8"), 16*1024); + String line = null; + String search = startsWith + '='; + Map rv = new HashMap(); + while ( (line = in.readLine()) != null) { + if ((!startsWith.equals("")) && !line.startsWith(search)) + continue; + if (line.startsWith("#")) + continue; + if (line.indexOf('#') > 0) // trim off any end of line comment + line = line.substring(0, line.indexOf('#')).trim(); + int split = line.indexOf('='); + String key = line.substring(split); + String b64 = line.substring(split+1); //.trim() ?????????????? + try { + Destination dest = new Destination(b64); + rv.put(key, dest); + } catch (DataFormatException dfe) {} + } + return rv; + } catch (IOException ioe) { + _log.error("getEntries error", ioe); + return Collections.EMPTY_MAP; + } finally { + if (in != null) try { in.close(); } catch (IOException ioe) {} + releaseReadLock(); + } + } + + /** + * @param options ignored + */ + @Override + public int size(Properties options) { + if (!_file.exists()) + return 0; + BufferedReader in = null; + getReadLock(); + try { + in = new BufferedReader(new InputStreamReader(new FileInputStream(_file), "UTF-8"), 16*1024); + String line = null; + int rv = 0; + while ( (line = in.readLine()) != null) { + if (line.startsWith("#")) + continue; + rv++; + } + return rv; + } catch (IOException ioe) { + _log.error("size() error", ioe); + return -1; + } finally { + if (in != null) try { in.close(); } catch (IOException ioe) {} + releaseReadLock(); + } + } + private static boolean rename(File from, File to) { boolean success = false; boolean isWindows = System.getProperty("os.name").startsWith("Win");