diff options
Diffstat (limited to 'dozentenmodulserver/src/main/java/fileserv/FileChunk.java')
| -rw-r--r-- | dozentenmodulserver/src/main/java/fileserv/FileChunk.java | 28 |
1 files changed, 22 insertions, 6 deletions
diff --git a/dozentenmodulserver/src/main/java/fileserv/FileChunk.java b/dozentenmodulserver/src/main/java/fileserv/FileChunk.java index 2fee6378..1a95d27c 100644 --- a/dozentenmodulserver/src/main/java/fileserv/FileChunk.java +++ b/dozentenmodulserver/src/main/java/fileserv/FileChunk.java @@ -1,39 +1,55 @@ package fileserv; +import java.nio.ByteBuffer; import java.util.Collection; import java.util.List; import org.openslx.filetransfer.FileRange; public class FileChunk { - public static final int CHUNK_SIZE = 16 * 1024 * 1024; + + public static final int CHUNK_SIZE_MIB = 16; + public static final int CHUNK_SIZE = CHUNK_SIZE_MIB * (1024 * 1024); public final FileRange range; public final byte[] sha1sum; + private int failCount = 0; public FileChunk(long startOffset, long endOffset, byte[] sha1sum) { this.range = new FileRange(startOffset, endOffset); this.sha1sum = sha1sum; } + /** + * Signal that transferring this chunk seems to have failed (checksum + * mismatch). + * + * @return Number of times the transfer failed now + */ + public synchronized int incFailed() { + return ++failCount; + } + + // + public static int fileSizeToChunkCount(long fileSize) { return (int) ((fileSize + CHUNK_SIZE - 1) / CHUNK_SIZE); } - public static void createChunkList(Collection<FileChunk> list, long fileSize, List<byte[]> sha1sums) { + public static void createChunkList(Collection<FileChunk> list, long fileSize, List<ByteBuffer> sha1Sums) { if (fileSize < 0) throw new IllegalArgumentException("fileSize cannot be negative"); long chunkCount = fileSizeToChunkCount(fileSize); - if (sha1sums != null) { - if (sha1sums.size() != chunkCount) + if (sha1Sums != null) { + if (sha1Sums.size() != chunkCount) throw new IllegalArgumentException( "Passed a sha1sum list, but hash count in list doesn't match expected chunk count"); long offset = 0; - for (byte[] sha1sum : sha1sums) { // Do this as we don't know how efficient List.get(index) is... + for (ByteBuffer sha1sum : sha1Sums) { // Do this as we don't know how efficient List.get(index) is... long end = offset + CHUNK_SIZE; if (end > fileSize) end = fileSize; - list.add(new FileChunk(offset, end, sha1sum)); + list.add(new FileChunk(offset, end, sha1sum.array())); offset = end; } return; |
