Set maximum size of shared file to 128GB and max number of pieces to 128

pull/4/head
Zlatin Balevsky 2018-07-18 18:23:47 +01:00
parent c28708feb7
commit 858c223b4c
2 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,24 @@
package com.muwire.core
class FileHasher {
/** max size of shared file is 128 GB */
public static final long MAX_SIZE = 0x1 << 37
/**
* @param size of the file to be shared
* @return the size of each piece in power of 2
*/
static int getPieceSize(long size) {
if (size <= 0x1 << 25)
return 18
for (int i = 26; i <= 37; i++) {
if (size <= 0x1 << i) {
return i-7
}
}
throw new IllegalArgumentException("File too large $size")
}
}

View File

@ -0,0 +1,18 @@
package com.muwire.core
import static org.junit.jupiter.api.Assertions.assertEquals
import org.junit.Test
class FileHasherTest extends GroovyTestCase {
@Test
void testPieceSize() {
assert 18 == FileHasher.getPieceSize(1000000)
assert 20 == FileHasher.getPieceSize(100000000)
shouldFail IllegalArgumentException, {
FileHasher.getPieceSize(Long.MAX_VALUE)
}
}
}