Class HostTxtParser


  • class HostTxtParser
    extends Object
    Utility class providing methods to parse and write files in a hosts.txt file format, and subscription file format.
    Since:
    0.9.26 modified from ConfigParser
    • Constructor Detail

      • HostTxtParser

        HostTxtParser()
    • Method Detail

      • parse

        public static HostTxtEntry parse​(String inputLine,
                                         boolean allowCommandOnly)
        Return a HostTxtEntry from the contents of the inputLine.
        Parameters:
        inputLine - key=value[#!k1=v1#k2=v2...]
        allowCommandOnly - if true, a line starting with #! will return a HostTxtEntry with a null name and dest and non-null props. If false, these lines will return null.
        Returns:
        null if no entry found or on error
      • parse

        public static Map<String,​HostTxtEntry> parse​(File file)
                                                    throws IOException
        Return a Map using the contents of the File file. See parse(BufferedReader) for details of the input format. Returned map will not contain null ("remove") entries.
        Parameters:
        file - A File to parse.
        Returns:
        A Map containing the key, value pairs from file.
        Throws:
        IOException - if file cannot be read.
      • parse

        public static Map<String,​HostTxtEntry> parse​(File file,
                                                           Map<String,​HostTxtEntry> map)
        Return a Map using the contents of the File file. If file cannot be read, use map instead, and write the result to where file should have been. Returned map will not contain null ("remove") entries.
        Parameters:
        file - A File to attempt to parse.
        map - A Map containing values to use as defaults.
        Returns:
        A Map containing the key, value pairs from file, or if file cannot be read, map.
      • write

        public static void write​(Map<String,​HostTxtEntry> map,
                                 File file)
                          throws IOException
        Write contents of Map map to the File file. Output is written with one key, value pair on each line, in the format: key=value. Write to a temp file in the same directory and then rename, to not corrupt simultaneous accesses by the router. Except on Windows where renameTo() will fail if the target exists.
        Parameters:
        map - A Map to write to file.
        file - A File to write the Map to.
        Throws:
        IOException - if file cannot be written to.
      • main

        public static void main​(String[] args)
                         throws Exception
        Usage: HostTxtParser [-q] validate example.i2p=b64dest[#!key1=val1#key2=val2]
        Throws:
        Exception