diff --git a/core/src/main/groovy/com/muwire/core/download/DownloadSession.groovy b/core/src/main/groovy/com/muwire/core/download/DownloadSession.groovy index 3a8cc586..7d8f9777 100644 --- a/core/src/main/groovy/com/muwire/core/download/DownloadSession.groovy +++ b/core/src/main/groovy/com/muwire/core/download/DownloadSession.groovy @@ -123,7 +123,9 @@ class DownloadSession { // parse X-Have if present if (headers.containsKey("X-Have")) { - updateAvailablePieces(headers["X-Have"]) + DataUtil.decodeXHave(headers["X-Have"]).each { + available.add(it) + } if (!available.contains(piece)) return true // try again next time } else { @@ -224,16 +226,4 @@ class DownloadSession { totalRead += reads[idx] (int)(totalRead * 1000.0 / interval) } - - private void updateAvailablePieces(String xHave) { - byte [] availablePieces = Base64.decode(xHave) - availablePieces.eachWithIndex {b, i -> - for (int j = 0; j < 8 ; j++) { - byte mask = 0x80 >>> j - if ((b & mask) == mask) { - available.add(i * 8 + j) - } - } - } - } } diff --git a/core/src/main/groovy/com/muwire/core/util/DataUtil.groovy b/core/src/main/groovy/com/muwire/core/util/DataUtil.groovy index 26bd19d7..238fa871 100644 --- a/core/src/main/groovy/com/muwire/core/util/DataUtil.groovy +++ b/core/src/main/groovy/com/muwire/core/util/DataUtil.groovy @@ -95,4 +95,18 @@ class DataUtil { } Base64.encode(raw) } + + public static List decodeXHave(String xHave) { + byte [] availablePieces = Base64.decode(xHave) + List available = new ArrayList<>() + availablePieces.eachWithIndex {b, i -> + for (int j = 0; j < 8 ; j++) { + byte mask = 0x80 >>> j + if ((b & mask) == mask) { + available.add(i * 8 + j) + } + } + } + available + } }