mirror of https://github.com/zlatinb/muwire
Set maximum size of shared file to 128GB and max number of pieces to 128
parent
c28708feb7
commit
858c223b4c
|
@ -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")
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue