Skip to content

Commit

Permalink
Implemented getSequentialDownloaded at FileSliceTracker level.
Browse files Browse the repository at this point in the history
  • Loading branch information
aldenml committed Jan 26, 2015
1 parent 44313a5 commit e7d0c69
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/com/frostwire/jlibtorrent/FileSlice.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,9 @@ public long getOffset() {
public long getSize() {
return size;
}

@Override
public String toString() {
return String.format("FileSlice(fileIndex: %d, offset: %d, size: %d)", fileIndex, offset, size);
}
}
22 changes: 20 additions & 2 deletions src/com/frostwire/jlibtorrent/FileSliceTracker.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.frostwire.jlibtorrent;

import java.util.Iterator;
import java.util.TreeMap;

/**
Expand All @@ -9,19 +10,36 @@
public final class FileSliceTracker {

private final int fileIndex;

private final TreeMap<Long, Pair<FileSlice, Boolean>> slices;

public FileSliceTracker(int fileIndex) {
this.fileIndex = fileIndex;

this.slices = new TreeMap<Long, Pair<FileSlice, Boolean>>();
}

public int getFileIndex() {
return fileIndex;
}

public long getSequentialDownloaded() {
Iterator<Pair<FileSlice, Boolean>> it = slices.values().iterator();

long downloaded = 0;
boolean done = false;

while (!done && it.hasNext()) {
Pair<FileSlice, Boolean> p = it.next();

if (Boolean.TRUE.equals(p.second)) { // complete
downloaded = downloaded + p.first.getSize();
} else {
done = true;
}
}

return downloaded;
}

public void addSlice(FileSlice slice) throws IllegalArgumentException {
if (slice.getFileIndex() != fileIndex) {
throw new IllegalArgumentException("Invalid file index");
Expand Down

0 comments on commit e7d0c69

Please sign in to comment.