java.lang.Object
net.i2p.addressbook.HostTxtParser

public class HostTxtParserextends Object
Utility class providing methods to parse and write files in a hosts.txt file format, and subscription file format.
  • Field Details

    • isWindows

      private static final boolean isWindows
      True on Windows, where the path separators differ.
  • Constructor Details

    • HostTxtParser

      private HostTxtParser()
      Utility class, not intended for instantiation.
  • Method Details

    • parse

      private static Map<String, HostTxtEntry> parse(BufferedReader input) throws IOException
      Return a Map using the contents of BufferedReader input. input must have a single key, value pair on each line, in the format: key=value. Lines starting with '#' or ';' are considered comments, and ignored. Lines that are obviously not in the format key=value are also ignored. The key is converted to lower case.

      Returned map will not contain null ("remove") entries.

      Parameters:
      input - A BufferedReader with lines in key=value format to parse into a Map.
      Returns:
      A Map containing the key, value pairs from input.
      Throws:
      IOException - if the BufferedReader cannot be read.
    • 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

      private static void write(Map<String, HostTxtEntry> map, BufferedWriter output) throws IOException
      Write contents of Map map to BufferedWriter output. Output is written with one key, value pair on each line, in the format: key=value.
      Parameters:
      map - A Map to write to output.
      output - A BufferedWriter to write the Map to.
      Throws:
      IOException - if the BufferedWriter cannot be written to.
    • 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].
      Parameters:
      args - optionally "-q" to suppress the diagnostic output, then the "validate" verb and the entry to check
      Throws:
      Exception - if parsing or checking the entry fails; the stack trace is printed and the exit status is nonzero