Changeset ebbf7f2 for apps/addressbook


Ignore:
Timestamp:
Apr 16, 2016 6:35:46 PM (4 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
ca3f996
Parents:
696c215
Message:

Addressbook: Cleanups
in advance of new subscription protocol work:

  • finals
  • Add Iterable interfaces
  • Comment out unused methods
  • public→private methods
  • Close resources in finally blocks
  • Javadocs, spelling fixes
  • Increase max subscription size
  • LinkedList?ArrayList?
  • Improve while loops for reading lines
Location:
apps/addressbook/java/src/net/i2p/addressbook
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • apps/addressbook/java/src/net/i2p/addressbook/AddressBook.java

    r696c215 rebbf7f2  
    3939 * together, and written out to local files.
    4040 *
     41 * Methods are NOT thread-safe.
     42 *
    4143 * @author Ragnarok
    4244 * 
    4345 */
    44 class AddressBook {
     46class AddressBook implements Iterable<Map.Entry<String, String>> {
    4547
    4648    private final String location;
     
    108110    }
    109111*/
    110     static final long MAX_SUB_SIZE = 3 * 1024 * 1024l; //about 5,000 hosts
     112    static final long MAX_SUB_SIZE = 5 * 1024 * 1024l; //about 8,000 hosts
    111113
    112114    /**
  • apps/addressbook/java/src/net/i2p/addressbook/ConfigIterator.java

    r696c215 rebbf7f2  
    6969            return true;
    7070        try {
    71             String inputLine = input.readLine();
    72             while (inputLine != null) {
     71            String inputLine;
     72            while ((inputLine = input.readLine()) != null) {
    7373                inputLine = ConfigParser.stripComments(inputLine);
    7474                String[] splitLine = DataHelper.split(inputLine, "=");
     
    7777                    return true;
    7878                }
    79                 inputLine = input.readLine();
    8079            }
    8180        } catch (IOException ioe) {}
  • apps/addressbook/java/src/net/i2p/addressbook/ConfigParser.java

    r696c215 rebbf7f2  
    3030import java.io.OutputStreamWriter;
    3131import java.io.StringReader;
     32import java.util.ArrayList;
    3233import java.util.HashMap;
    33 import java.util.LinkedList;
    3434import java.util.List;
    3535import java.util.Locale;
     
    4545 * format, and subscription file format.
    4646 *
    47  * TODO: Change file encoding from default to UTF-8?
    48  *       Or switch to the DataHelper loadProps/storeProps methods?
     47 * TODO: switch to the DataHelper loadProps/storeProps methods?
    4948 *
    5049 * @author Ragnarok
     
    8988     * 
    9089     */
    91     public static Map<String, String>  parse(BufferedReader input) throws IOException {
    92         Map<String, String>  result = new HashMap<String, String>();
    93         String inputLine;
    94         inputLine = input.readLine();
    95         while (inputLine != null) {
    96             inputLine = stripComments(inputLine);
    97             String[] splitLine = DataHelper.split(inputLine, "=");
    98             if (splitLine.length == 2) {
    99                 result.put(splitLine[0].trim().toLowerCase(Locale.US), splitLine[1].trim());
    100             }
    101             inputLine = input.readLine();
    102         }
    103         input.close();
    104         return result;
     90    private static Map<String, String> parse(BufferedReader input) throws IOException {
     91        try {
     92            Map<String, String> result = new HashMap<String, String>();
     93            String inputLine;
     94            while ((inputLine = input.readLine()) != null) {
     95                inputLine = stripComments(inputLine);
     96                String[] splitLine = DataHelper.split(inputLine, "=");
     97                if (splitLine.length == 2) {
     98                    result.put(splitLine[0].trim().toLowerCase(Locale.US), splitLine[1].trim());
     99                }
     100            }
     101            return result;
     102        } finally {
     103            try { input.close(); } catch (IOException ioe) {}
     104        }
    105105    }
    106106
     
    115115     *             if file cannot be read.
    116116     */
    117     public static Map<String, String>  parse(File file) throws IOException {
    118         FileInputStream fileStream = new FileInputStream(file);
    119         BufferedReader input = new BufferedReader(new InputStreamReader(
    120                 fileStream, "UTF-8"));
    121         Map<String, String>  rv = parse(input);
    122         try {
    123             fileStream.close();
    124         } catch (IOException ioe) {}
    125         return rv;
     117    public static Map<String, String> parse(File file) throws IOException {
     118        FileInputStream fileStream = null;
     119        try {
     120            fileStream = new FileInputStream(file);
     121            BufferedReader input = new BufferedReader(new InputStreamReader(
     122                    fileStream, "UTF-8"));
     123            Map<String, String> rv = parse(input);
     124            return rv;
     125        } finally {
     126            if (fileStream != null) {
     127                try {
     128                    fileStream.close();
     129                } catch (IOException ioe) {}
     130            }
     131        }
    126132    }
    127133
     
    136142     *             if file cannot be read.
    137143     */
    138     public static Map<String, String>  parse(String string) throws IOException {
     144/****
     145    public static Map<String, String> parse(String string) throws IOException {
    139146        StringReader stringReader = new StringReader(string);
    140147        BufferedReader input = new BufferedReader(stringReader);
    141148        return parse(input);
    142149    }
     150****/
    143151   
    144152    /**
     
    153161     *         cannot be read, map.
    154162     */
    155     public static Map<String, String>  parse(File file, Map<String, String> map) {
    156         Map<String, String>  result;
     163    public static Map<String, String> parse(File file, Map<String, String> map) {
     164        Map<String, String> result;
    157165        try {
    158166            result = parse(file);
     
    180188     *             if input cannot be read.
    181189     */
    182     public static List<String> parseSubscriptions(BufferedReader input)
     190    private static List<String> parseSubscriptions(BufferedReader input)
    183191            throws IOException {
    184         List<String> result = new LinkedList<String>();
    185         String inputLine = input.readLine();
    186         while (inputLine != null) {
    187             inputLine = stripComments(inputLine).trim();
    188             if (inputLine.length() > 0) {
    189                 result.add(inputLine);
    190             }
    191             inputLine = input.readLine();
    192         }
    193         input.close();
    194         return result;
     192        try {
     193            List<String> result = new ArrayList<String>(4);
     194            String inputLine;
     195            while ((inputLine = input.readLine()) != null) {
     196                inputLine = stripComments(inputLine).trim();
     197                if (inputLine.length() > 0) {
     198                    result.add(inputLine);
     199                }
     200            }
     201            return result;
     202        } finally {
     203            try { input.close(); } catch (IOException ioe) {}
     204        }
    195205    }
    196206
     
    204214     *             if file cannot be read.
    205215     */
    206     public static List<String> parseSubscriptions(File file) throws IOException {
    207         FileInputStream fileStream = new FileInputStream(file);
    208         BufferedReader input = new BufferedReader(new InputStreamReader(
    209                 fileStream, "UTF-8"));
    210         List<String> rv = parseSubscriptions(input);
    211         try {
    212             fileStream.close();
    213         } catch (IOException ioe) {}
    214         return rv;
     216    private static List<String> parseSubscriptions(File file) throws IOException {
     217        FileInputStream fileStream = null;
     218        try {
     219            fileStream = new FileInputStream(file);
     220            BufferedReader input = new BufferedReader(new InputStreamReader(
     221                    fileStream, "UTF-8"));
     222            List<String> rv = parseSubscriptions(input);
     223            return rv;
     224        } finally {
     225            if (fileStream != null) {
     226                try {
     227                    fileStream.close();
     228                } catch (IOException ioe) {}
     229            }
     230        }
    215231    }
    216232
     
    224240     *             if string cannot be read.
    225241     */
     242/****
    226243    public static List<String> parseSubscriptions(String string) throws IOException {
    227244        StringReader stringReader = new StringReader(string);
     
    229246        return parseSubscriptions(input);
    230247    }
     248****/
    231249   
    232250    /**
     
    278296     *             if the BufferedWriter cannot be written to.
    279297     */
    280     public static void write(Map<String, String>  map, BufferedWriter output) throws IOException {
    281         for (Map.Entry<String, String> entry : map.entrySet()) {
    282             output.write(entry.getKey() + '=' + entry.getValue());
    283             output.newLine();
    284         }
    285         output.close();
     298    private static void write(Map<String, String> map, BufferedWriter output) throws IOException {
     299        try {
     300            for (Map.Entry<String, String> entry : map.entrySet()) {
     301                output.write(entry.getKey() + '=' + entry.getValue());
     302                output.newLine();
     303            }
     304        } finally {
     305            try { output.close(); } catch (IOException ioe) {}
     306        }
    286307    }
    287308
     
    300321     *             if file cannot be written to.
    301322     */
    302     public static void write(Map<String, String>  map, File file) throws IOException {
     323    public static void write(Map<String, String> map, File file) throws IOException {
    303324        boolean success = false;
    304325        if (!isWindows) {
     
    328349     *             if output cannot be written to.
    329350     */
    330     public static void writeSubscriptions(List<String> list, BufferedWriter output)
     351    private static void writeSubscriptions(List<String> list, BufferedWriter output)
    331352            throws IOException {
    332         for (String s : list) {
    333             output.write(s);
    334             output.newLine();
    335         }
    336         output.close();
     353        try {
     354            for (String s : list) {
     355                output.write(s);
     356                output.newLine();
     357            }
     358        } finally {
     359            try { output.close(); } catch (IOException ioe) {}
     360        }
    337361    }
    338362   
     
    348372     *             if output cannot be written to.
    349373     */
    350     public static void writeSubscriptions(List<String> list, File file)
     374    private static void writeSubscriptions(List<String> list, File file)
    351375            throws IOException {
    352376        writeSubscriptions(list, new BufferedWriter(
  • apps/addressbook/java/src/net/i2p/addressbook/Daemon.java

    r696c215 rebbf7f2  
    2424import java.io.File;
    2525import java.io.IOException;
     26import java.util.ArrayList;
    2627import java.util.HashMap;
    2728import java.util.Iterator;
    28 import java.util.LinkedList;
    2929import java.util.List;
    3030import java.util.Map;
     
    131131            long start = System.currentTimeMillis();
    132132            AddressBook sub = iter.next();
    133             long end = System.currentTimeMillis();
    134133            // SubscriptionIterator puts in a dummy AddressBook with no location if no fetch is done
    135             if (DEBUG && log != null && sub.getLocation() != null)
     134            if (DEBUG && log != null && sub.getLocation() != null) {
     135                long end = System.currentTimeMillis();
    136136                log.append("Fetch of " + sub.getLocation() + " took " + (end - start));
    137             start = end;
     137                start = end;
     138            }
    138139            int old = 0, nnew = 0, invalid = 0, conflict = 0, total = 0;
    139140            for (Iterator<Map.Entry<String, String>> eIter = sub.iterator(); eIter.hasNext(); ) {
     
    255256        delay *= 60 * 60 * 1000;
    256257       
    257         List<String> defaultSubs = new LinkedList<String>();
     258        List<String> defaultSubs = new ArrayList<String>(4);
    258259        // defaultSubs.add("http://i2p/NF2RLVUxVulR3IqK0sGJR0dHQcGXAzwa6rEO4WAWYXOHw-DoZhKnlbf1nzHXwMEJoex5nFTyiNMqxJMWlY54cvU~UenZdkyQQeUSBZXyuSweflUXFqKN-y8xIoK2w9Ylq1k8IcrAFDsITyOzjUKoOPfVq34rKNDo7fYyis4kT5bAHy~2N1EVMs34pi2RFabATIOBk38Qhab57Umpa6yEoE~rbyR~suDRvD7gjBvBiIKFqhFueXsR2uSrPB-yzwAGofTXuklofK3DdKspciclTVzqbDjsk5UXfu2nTrC1agkhLyqlOfjhyqC~t1IXm-Vs2o7911k7KKLGjB4lmH508YJ7G9fLAUyjuB-wwwhejoWqvg7oWvqo4oIok8LG6ECR71C3dzCvIjY2QcrhoaazA9G4zcGMm6NKND-H4XY6tUWhpB~5GefB3YczOqMbHq4wi0O9MzBFrOJEOs3X4hwboKWANf7DT5PZKJZ5KorQPsYRSq0E3wSOsFCSsdVCKUGsAAAA/i2p/hosts.txt");
    259260        defaultSubs.add(DEFAULT_SUB);
  • apps/addressbook/java/src/net/i2p/addressbook/DaemonThread.java

    r696c215 rebbf7f2  
    3636public class DaemonThread extends I2PAppThread implements NamingServiceUpdater {
    3737
    38     private String[] args;
     38    private final String[] args;
    3939
    4040    /**
  • apps/addressbook/java/src/net/i2p/addressbook/Log.java

    r696c215 rebbf7f2  
    3737class Log {
    3838
    39     private File file;
     39    private final File file;
    4040
    4141    /**
  • apps/addressbook/java/src/net/i2p/addressbook/Subscription.java

    r696c215 rebbf7f2  
    3030class Subscription {
    3131
    32     private String location;
    33 
     32    private final String location;
    3433    private String etag;
    35 
    3634    private String lastModified;
    3735    private long lastFetched;
     
    4240     *
    4341     * @param location
    44      *            A String representing a url to a remote address book.
     42     *            A String representing a url to a remote address book. Non-null.
    4543     * @param etag
    46      *            The etag header that we recieved the last time we read this
    47      *            subscription.
     44     *            The etag header that we received the last time we read this
     45     *            subscription. May be null.
    4846     * @param lastModified
    49      *            the last-modified header we recieved the last time we read
    50      *            this subscription.
    51      * @param lastFetched when the subscription was last fetched (Java time, as a String)
     47     *            the last-modified header we received the last time we read
     48     *            this subscription. May be null.
     49     * @param lastFetched when the subscription was last fetched (Java time, as a String).
     50     *            May be null.
    5251     */
    5352    public Subscription(String location, String etag, String lastModified, String lastFetched) {
     
    7271
    7372    /**
    74      * Return the etag header that we recieved the last time we read this
     73     * Return the etag header that we received the last time we read this
    7574     * subscription.
    7675     *
     
    9291
    9392    /**
    94      * Return the last-modified header that we recieved the last time we read
     93     * Return the last-modified header that we received the last time we read
    9594     * this subscription.
    9695     *
  • apps/addressbook/java/src/net/i2p/addressbook/SubscriptionIterator.java

    r696c215 rebbf7f2  
    3838class SubscriptionIterator implements Iterator<AddressBook> {
    3939
    40     private Iterator<Subscription> subIterator;
    41     private String proxyHost;
    42     private int proxyPort;
     40    private final Iterator<Subscription> subIterator;
     41    private final String proxyHost;
     42    private final int proxyPort;
    4343    private final long delay;
    4444
  • apps/addressbook/java/src/net/i2p/addressbook/SubscriptionList.java

    r696c215 rebbf7f2  
    2424import java.io.File;
    2525import java.io.IOException;
     26import java.util.ArrayList;
    2627import java.util.HashMap;
    27 import java.util.LinkedList;
    2828import java.util.List;
    2929import java.util.Map;
     
    3535 * 
    3636 */
    37 class SubscriptionList {
     37class SubscriptionList implements Iterable<AddressBook> {
    3838
    39     private List<Subscription> subscriptions;
    40 
    41     private File etagsFile;
    42 
    43     private File lastModifiedFile;
    44     private File lastFetchedFile;
     39    private final List<Subscription> subscriptions;
     40    private final File etagsFile;
     41    private final File lastModifiedFile;
     42    private final File lastFetchedFile;
    4543    private final long delay;
    46    
    47     private String proxyHost;
    48    
    49     private int proxyPort;
     44    private final String proxyHost;
     45    private final int proxyPort;
    5046
    5147    /**
     
    7066            File lastModifiedFile, File lastFetchedFile, long delay, List<String> defaultSubs, String proxyHost,
    7167            int proxyPort) {
    72         this.subscriptions = new LinkedList<Subscription>();
     68        this.subscriptions = new ArrayList<Subscription>(4);
    7369        this.etagsFile = etagsFile;
    7470        this.lastModifiedFile = lastModifiedFile;
  • apps/addressbook/java/src/net/i2p/addressbook/package.html

    r696c215 rebbf7f2  
    22<body>
    33<p>
    4 The addressbook application, which fetches hosts.txt files from subscription URLS via
     4The addressbook application, which fetches hosts.txt files from subscription URLs via
    55HTTP and adds new hosts to the local database.
    66While implemented as a webapp, this application contains no user interface.
Note: See TracChangeset for help on using the changeset viewer.