From 2c8036071f51c1fbb9cc14b3a55c557597e73d2b Mon Sep 17 00:00:00 2001 From: Jonathan Trowbridge Date: Tue, 31 Jul 2018 20:03:38 -0400 Subject: [PATCH] master - documentation and fixed bufferTailIndex value when not streaming. --- .../models/TorrentSessionBufferState.kt | 47 +++++++++++++++---- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/simpletorrentandroid/src/main/java/com/masterwok/simpletorrentandroid/models/TorrentSessionBufferState.kt b/simpletorrentandroid/src/main/java/com/masterwok/simpletorrentandroid/models/TorrentSessionBufferState.kt index d156815..1726a2f 100644 --- a/simpletorrentandroid/src/main/java/com/masterwok/simpletorrentandroid/models/TorrentSessionBufferState.kt +++ b/simpletorrentandroid/src/main/java/com/masterwok/simpletorrentandroid/models/TorrentSessionBufferState.kt @@ -1,15 +1,24 @@ 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 @@ -17,6 +26,9 @@ class TorrentSessionBufferState constructor( field = value } + /** + * The index of the head of the buffer. + */ var bufferHeadIndex = startIndex @Synchronized get() = field @@ -24,13 +36,20 @@ class TorrentSessionBufferState constructor( 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 @@ -38,14 +57,10 @@ class TorrentSessionBufferState constructor( 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 @@ -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" +