summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/filetransfer/util/FileChunk.java
diff options
context:
space:
mode:
authorSimon Rettberg2015-07-23 17:42:30 +0200
committerSimon Rettberg2015-07-23 17:42:30 +0200
commitbacb060ba31fafebd8c0f15d0b6704732a37b482 (patch)
treea20be56f5d24fe8e242cd1fbb0589826bf88dc7f /src/main/java/org/openslx/filetransfer/util/FileChunk.java
parentVm meta parser: Add virtualizer getter, change type of description (diff)
downloadmaster-sync-shared-bacb060ba31fafebd8c0f15d0b6704732a37b482.tar.gz
master-sync-shared-bacb060ba31fafebd8c0f15d0b6704732a37b482.tar.xz
master-sync-shared-bacb060ba31fafebd8c0f15d0b6704732a37b482.zip
ChunkList.getMissing() now blocks for a while if there are still pending blocks
Added HashChecker class to verify checksums of blocks
Diffstat (limited to 'src/main/java/org/openslx/filetransfer/util/FileChunk.java')
-rw-r--r--src/main/java/org/openslx/filetransfer/util/FileChunk.java66
1 files changed, 39 insertions, 27 deletions
diff --git a/src/main/java/org/openslx/filetransfer/util/FileChunk.java b/src/main/java/org/openslx/filetransfer/util/FileChunk.java
index 3e89b84..3ec6468 100644
--- a/src/main/java/org/openslx/filetransfer/util/FileChunk.java
+++ b/src/main/java/org/openslx/filetransfer/util/FileChunk.java
@@ -6,17 +6,19 @@ import java.util.List;
import org.openslx.filetransfer.FileRange;
-public class FileChunk {
+public class FileChunk
+{
public static final int CHUNK_SIZE_MIB = 16;
- public static final int CHUNK_SIZE = CHUNK_SIZE_MIB * (1024 * 1024);
+ 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);
+ public FileChunk( long startOffset, long endOffset, byte[] sha1sum )
+ {
+ this.range = new FileRange( startOffset, endOffset );
this.sha1sum = sha1sum;
}
@@ -26,52 +28,62 @@ public class FileChunk {
*
* @return Number of times the transfer failed now
*/
- public synchronized int incFailed() {
+ public synchronized int incFailed()
+ {
return ++failCount;
}
-
- public int getChunkIndex() {
- return (int)(range.startOffset / CHUNK_SIZE);
+
+ public int getChunkIndex()
+ {
+ return (int) ( range.startOffset / CHUNK_SIZE );
}
-
+
@Override
- public String toString() {
+ public String toString()
+ {
return "[Chunk " + getChunkIndex() + " (" + range.startOffset + "-" + range.endOffset + "), fails: " + failCount + "]";
}
//
- public static int fileSizeToChunkCount(long fileSize) {
- return (int) ((fileSize + CHUNK_SIZE - 1) / CHUNK_SIZE);
+ public static int fileSizeToChunkCount( long fileSize )
+ {
+ return (int) ( ( fileSize + CHUNK_SIZE - 1 ) / CHUNK_SIZE );
}
- public static void createChunkList(Collection<FileChunk> list, long fileSize, List<ByteBuffer> sha1Sums) {
- if (fileSize < 0)
- throw new IllegalArgumentException("fileSize cannot be negative");
- if (!list.isEmpty())
- throw new IllegalArgumentException("Passed list is not empty");
- long chunkCount = fileSizeToChunkCount(fileSize);
- if (sha1Sums != null) {
- if (sha1Sums.size() != chunkCount)
+ public static void createChunkList( Collection<FileChunk> list, long fileSize, List<ByteBuffer> sha1Sums )
+ {
+ if ( fileSize < 0 )
+ throw new IllegalArgumentException( "fileSize cannot be negative" );
+ if ( !list.isEmpty() )
+ throw new IllegalArgumentException( "Passed list is not empty" );
+ 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");
+ "Passed a sha1sum list, but hash count in list doesn't match expected chunk count" );
long offset = 0;
- for (ByteBuffer 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)
+ if ( end > fileSize )
end = fileSize;
- list.add(new FileChunk(offset, end, sha1sum.array()));
+ list.add( new FileChunk( offset, end, sha1sum.array() ) );
offset = end;
}
return;
}
long offset = 0;
- while (offset < fileSize) { // ...otherwise we could share this code
+ while ( offset < fileSize ) { // ...otherwise we could share this code
long end = offset + CHUNK_SIZE;
- if (end > fileSize)
+ if ( end > fileSize )
end = fileSize;
- list.add(new FileChunk(offset, end, null));
+ list.add( new FileChunk( offset, end, null ) );
offset = end;
}
}
+
+ public boolean hasSha1Sum()
+ {
+ return sha1sum != null && sha1sum.length == 20;
+ }
}