Skip to content

Commit

Permalink
master - documentation and fixed bufferTailIndex value when not strea…
Browse files Browse the repository at this point in the history
…ming.
  • Loading branch information
masterwok committed Aug 1, 2018
1 parent 1e6351c commit 2c80360
Showing 1 changed file with 37 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,51 +1,66 @@
package com.masterwok.simpletorrentandroid.models


/**
* This buffer contains the current download state of torrent pieces and is
* used internally to keep track of which pieces to prioritize when streaming.
*/
@Suppress("CanBeParameter", "MemberVisibilityCanBePrivate")
class TorrentSessionBufferState constructor(
class TorrentSessionBufferState internal constructor(
val bufferSize: Int
, val startIndex: Int = 0
, val endIndex: Int = 0
) {
/**
* The total number of pieces.
*/
val pieceCount = (endIndex - startIndex) + 1

private val pieceDownloadStates = BooleanArray(pieceCount)

/**
* The number of pieces downloaded.
*/
var downloadedPieceCount = 0
@Synchronized
get() = field
private set(value) {
field = value
}

/**
* The index of the head of the buffer.
*/
var bufferHeadIndex = startIndex
@Synchronized
get() = field
private set(value) {
field = value
}

var bufferTailIndex = startIndex + bufferSize - 1
/**
* The index of the tail of the buffer.
*/
var bufferTailIndex = if (bufferSize == 0) endIndex else startIndex + bufferSize - 1
@Synchronized
get() = field
private set(value) {
field = value
}

/**
* The index of the last downloaded piece. If no pieces were downloaded, then
* this value will be -1.
*/
var lastDownloadedPieceIndex = -1
@Synchronized
get() = field
private set(value) {
field = value
}

@Synchronized
fun allPiecesAreDownloaded() = !pieceDownloadStates.contains(false)

@Synchronized
fun isPieceDownloaded(position: Int) = pieceDownloadStates[position]
private val pieceDownloadStates = BooleanArray(pieceCount)

@Synchronized
fun setPieceDownloaded(index: Int): Boolean {
internal fun setPieceDownloaded(index: Int): Boolean {
// Ignore if less than head or already downloaded.
if (index < bufferHeadIndex || isPieceDownloaded(index)) {
return true
Expand Down Expand Up @@ -76,6 +91,18 @@ class TorrentSessionBufferState constructor(
return false
}

/**
* Determine if all pieces are downloaded.
*/
@Synchronized
fun allPiecesAreDownloaded() = !pieceDownloadStates.contains(false)

/**
* Check if piece at [index] is downloaded.
*/
@Synchronized
fun isPieceDownloaded(index: Int) = pieceDownloadStates[index]

@Synchronized
override fun toString(): String = "Total Pieces: $pieceCount" +
", Start: $startIndex" +
Expand Down

0 comments on commit 2c80360

Please sign in to comment.