SaveLoad Fast Resume

Version 2 (Alan McGovern, 04/16/2009 10:28 PM) → Version 3/4 (Alan McGovern, 04/20/2009 11:00 PM)


h1. Save/Load Fast Resume

<pre><code class="java"> <pre><code>
public void SaveFastResume (List <TorrentManager> managers)
{
// Store the fast resume for each torrent in a list,
// then serialise the list to the disk.
BEncodedList list = new BEncodedList ();
foreach (TorrentManager manager in managers) {

// Get the fast resume data for the torrent
FastResume data = manager.SaveFastResume ();

// Encode the FastResume data to a BEncodedDictionary.
BEncodedDictionary fastResume = data.Encode ();

// Add the FastResume dictionary to the main dictionary using
// the torrents infohash as the key
list.Add (data);
}

// Write all the fast resume data to disk
File.WriteAllBytes (fastResumePath, list.Encode ());
}

public void LoadFastResume (List <TorrentManager> managers)
{
// Read the main dictionary from disk and iterate through
// all the fast resume items
BEncodedList list = (BEncodedList) BEncodedValue.Decode (File.ReadAllBytes (fastResumePath));
foreach (BEncodedDictionary fastResume in list) {

// Decode the FastResume data from the BEncodedDictionary
FastResume data = new FastResume (fastResume);

// Find the torrentmanager that the fastresume belongs to
// and then load it
foreach (TorrentManager manager in managers)
if (manager.InfoHash == data.Infohash)
manager.LoadFastResume (data);
}
}
</code></pre>