diff options
| author | Simon Rettberg | 2015-05-29 17:58:25 +0200 |
|---|---|---|
| committer | Simon Rettberg | 2015-05-29 17:58:25 +0200 |
| commit | 8102e56cd9ebe064a1f4757b8d28c64661ab7cb3 (patch) | |
| tree | e78d7b3255a894feafc3c9c38924b11eb66bed76 /dozentenmodulserver/src/main/java/fileserv/FileChunk.java | |
| parent | [client] Compiles again, but is broken.... (diff) | |
| download | tutor-module-8102e56cd9ebe064a1f4757b8d28c64661ab7cb3.tar.gz tutor-module-8102e56cd9ebe064a1f4757b8d28c64661ab7cb3.tar.xz tutor-module-8102e56cd9ebe064a1f4757b8d28c64661ab7cb3.zip | |
[server] Started work on the internal file server
Diffstat (limited to 'dozentenmodulserver/src/main/java/fileserv/FileChunk.java')
| -rw-r--r-- | dozentenmodulserver/src/main/java/fileserv/FileChunk.java | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/dozentenmodulserver/src/main/java/fileserv/FileChunk.java b/dozentenmodulserver/src/main/java/fileserv/FileChunk.java new file mode 100644 index 00000000..2fee6378 --- /dev/null +++ b/dozentenmodulserver/src/main/java/fileserv/FileChunk.java @@ -0,0 +1,50 @@ +package fileserv; + +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 final FileRange range; + public final byte[] sha1sum; + + public FileChunk(long startOffset, long endOffset, byte[] sha1sum) { + this.range = new FileRange(startOffset, endOffset); + this.sha1sum = sha1sum; + } + + 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) { + if (fileSize < 0) + throw new IllegalArgumentException("fileSize cannot be negative"); + long chunkCount = fileSizeToChunkCount(fileSize); + 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... + long end = offset + CHUNK_SIZE; + if (end > fileSize) + end = fileSize; + list.add(new FileChunk(offset, end, sha1sum)); + offset = end; + } + return; + } + long offset = 0; + while (offset < fileSize) { // ...otherwise we could share this code + long end = offset + CHUNK_SIZE; + if (end > fileSize) + end = fileSize; + list.add(new FileChunk(offset, end, null)); + offset = end; + } + } +} |
