java.lang.Object
net.i2p.addressbook.AddressBook
All Implemented Interfaces:
Iterable<Map.Entry<String, HostTxtEntry>>

class AddressBookextends Object implements Iterable<Map.Entry<String, HostTxtEntry>>
An address book for storing human readable names mapped to base64 i2p destinations. AddressBooks can be created from local and remote files, merged together, and written out to local files.

Methods are NOT thread-safe.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final Log
    Logger for this class.
    private final Map<String, HostTxtEntry>
    Either addresses or subFile will be non-null, but not both.
    private static final Pattern
    52 chars lower/upper case Always ends in 'a' or 'q'.
    private static final Pattern
    Not a complete qualification, just a quick check.
    private static final Pattern
    5-67 chars lower/upper case.
    private final String
    Path of the hosts.txt file, for the logs.
    private static final int
    Longest a full destination can be, allowing for any known cert type.
    (package private) static final long
    Maximum permitted size of a hosts subscription file - 5MB or around 8000 hosts.
    private static final int
    Shortest a full destination can be in the text, in characters.
    private boolean
    True when entries changed since the last save.
    private final File
    The subscription file whose contents are shown, null if the addresses are cached.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Construct an AddressBook from the contents of the file at file.
    AddressBook(String testsubfile)
    Test only.
    Construct an AddressBook from the contents of the Map addresses.
    AddressBook(Subscription subscription, String proxyHost, int proxyPort)
    Construct an AddressBook from the Subscription subscription.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Delete the temp file or clear the map.
    Return the location of the file this AddressBook was constructed from.
    private static boolean
    Do basic validation of the b64 dest, without bothering to instantiate it.
    static boolean
    Basic validation of the hostname.
    Return an iterator over the addresses in the AddressBook.
    void
    merge(AddressBook other, boolean overwrite, Log log)
    Merge this AddressBook with AddressBook other, writing messages about new addresses or conflicts to log.
    private void
    merge2(AddressBook other, Iterator<Map.Entry<String, HostTxtEntry>> iter, boolean overwrite, Log log)
    Merges entries from another book into this one.
    Return a string representation of the origin of the AddressBook.
    void
    Write this AddressBook out to the file it was read from.
    void
    write(File file)
    Write the contents of this AddressBook out to the File file.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface Iterable

    forEach, spliterator
  • Field Details

    • location

      private final String location
      Path of the hosts.txt file, for the logs.
    • addresses

      private final Map<String, HostTxtEntry> addresses
      Either addresses or subFile will be non-null, but not both.
    • subFile

      private final File subFile
      The subscription file whose contents are shown, null if the addresses are cached.
    • modified

      private boolean modified
      True when entries changed since the last save.
    • _log

      private static final Log _log
      Logger for this class.
    • MIN_DEST_LENGTH

      private static final int MIN_DEST_LENGTH
      Shortest a full destination can be in the text, in characters.
      See Also:
    • MAX_DEST_LENGTH

      private static final int MAX_DEST_LENGTH
      Longest a full destination can be, allowing for any known cert type.
      See Also:
    • HOST_PATTERN

      private static final Pattern HOST_PATTERN
      5-67 chars lower/upper case.
    • B32_PATTERN

      private static final Pattern B32_PATTERN
      52 chars lower/upper case Always ends in 'a' or 'q'.
    • B64_PATTERN

      private static final Pattern B64_PATTERN
      Not a complete qualification, just a quick check.
    • MAX_SUB_SIZE

      static final long MAX_SUB_SIZE
      Maximum permitted size of a hosts subscription file - 5MB or around 8000 hosts.
      See Also:
  • Constructor Details

    • AddressBook

      public AddressBook(Map<String, HostTxtEntry> addresses)
      Construct an AddressBook from the contents of the Map addresses.
      Parameters:
      addresses - A Map containing human readable addresses as keys, mapped to base64 i2p destinations.
    • AddressBook

      public AddressBook(Subscription subscription, String proxyHost, int proxyPort)
      Construct an AddressBook from the Subscription subscription. If the address book at subscription has not changed since the last time it was read or cannot be read, return an empty AddressBook. Set a maximum size of the remote book to make it a little harder for a malicious book-sender.

      Yes, the EepGet fetch() is done in this constructor.

      This stores the subscription in a temporary file and does not read the whole thing into memory. An AddressBook created with this constructor may not be modified or written using write(). It may be a merge source (an parameter for another AddressBook's merge(AddressBook, boolean, Log)) but may not be a merge target (this.merge() will throw an exception).

      Parameters:
      subscription - A Subscription instance pointing at a remote address book.
      proxyHost - hostname of proxy
      proxyPort - port number of proxy
    • AddressBook

      public AddressBook(File file)
      Construct an AddressBook from the contents of the file at file. If the file cannot be read, construct an empty AddressBook. This reads the entire file into memory. The resulting map is modifiable and may be a merge target.
      Parameters:
      file - A File pointing at a file with lines in the format "key=value", where key is a human readable name, and value is a base64 i2p destination.
    • AddressBook

      public AddressBook(String testsubfile)
      Test only.
      Parameters:
      testsubfile - path to a file containing the simulated fetch of a subscription
  • Method Details

    • iterator

      public Iterator<Map.Entry<String, HostTxtEntry>> iterator()
      Return an iterator over the addresses in the AddressBook.
      Specified by:
      iterator in interface Iterable<Map.Entry<String, HostTxtEntry>>
    • delete

      public void delete()
      Delete the temp file or clear the map.
    • getLocation

      public String getLocation()
      Return the location of the file this AddressBook was constructed from.
      Returns:
      A String representing either an abstract path, or a url, depending on how the instance was constructed. Will be null if created with the Map constructor.
    • toString

      public String toString()
      Return a string representation of the origin of the AddressBook.
      Overrides:
      toString in class Object
      Returns:
      A String representing the origin of the AddressBook.
    • isValidKey

      public static boolean isValidKey(String host)
      Basic validation of the hostname. Already converted to lower case by HostTxtParser.parse(BufferedReader).
      Parameters:
      host - the hostname to validate
      Returns:
      true if the hostname is valid, false otherwise
    • isValidDest

      private static boolean isValidDest(String dest)
      Do basic validation of the b64 dest, without bothering to instantiate it.
      Parameters:
      dest - the destination to validate
      Returns:
      true if the destination is valid, false otherwise
    • merge

      public void merge(AddressBook other, boolean overwrite, Log log)
      Merge this AddressBook with AddressBook other, writing messages about new addresses or conflicts to log. Addresses in AddressBook other that are not in this AddressBook are added to this AddressBook.

      In case of a conflict, addresses in this AddressBook take precedence.

      Parameters:
      other - An AddressBook to merge with.
      overwrite - True to overwrite
      log - The log to write messages about new addresses or conflicts to. May be null.
      Throws:
      IllegalStateException - if this was created with the Subscription constructor.
    • merge2

      private void merge2(AddressBook other, Iterator<Map.Entry<String, HostTxtEntry>> iter, boolean overwrite, Log log)
      Merges entries from another book into this one.
      Parameters:
      other - the book being merged from
      iter - the entries to consider
      overwrite - true to replace existing entries
      log - where merge decisions are logged
    • write

      public void write(File file)
      Write the contents of this AddressBook out to the File file. If the file cannot be written to, this method will silently fail.
      Parameters:
      file - The file to write the contents of this AddressBook too.
      Throws:
      IllegalStateException - if this was created with the Subscription constructor.
    • write

      public void write()
      Write this AddressBook out to the file it was read from. Requires that AddressBook was constructed from a file on the local filesystem. If the file cannot be written to, this method will silently fail.
      Throws:
      IllegalStateException - if this was not created with the File constructor.