java.lang.Object
net.i2p.addressbook.HostTxtParser
Utility class providing methods to parse and write files in a hosts.txt file format, and subscription file format.
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final booleanTrue on Windows, where the path separators differ.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionstatic voidUsage: HostTxtParser [-q] validate example.i2p=b64dest[#!key1=val1#key2=val2].private static Map<String, HostTxtEntry> parse(BufferedReader input) Return a Map using the contents of BufferedReader input.static Map<String, HostTxtEntry> Return a Map using the contents of the File file.static Map<String, HostTxtEntry> parse(File file, Map<String, HostTxtEntry> map) Return a Map using the contents of the File file.static HostTxtEntryReturn a HostTxtEntry from the contents of the inputLine.private static voidwrite(Map<String, HostTxtEntry> map, BufferedWriter output) Write contents of Map map to BufferedWriter output.static voidwrite(Map<String, HostTxtEntry> map, File file) Write contents of Map map to the File file.
Field Details
isWindows
private static final boolean isWindowsTrue on Windows, where the path separators differ.
Constructor Details
HostTxtParser
private HostTxtParser()Utility class, not intended for instantiation.
Method Details
parse
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
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
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
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
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
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
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