1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.utgenome.format;
24
25 import java.io.File;
26 import java.io.FileNotFoundException;
27 import java.io.RandomAccessFile;
28 import java.util.ArrayList;
29 import java.util.List;
30
31
32
33
34
35
36
37 public class BlockwiseFileReader {
38
39 private final File file;
40 private RandomAccessFile fileAccess;
41 private final int blockSizeInMB;
42
43 public BlockwiseFileReader(File file, int blockSizeInMB) throws FileNotFoundException {
44 this.file = file;
45 this.blockSizeInMB = blockSizeInMB;
46 this.fileAccess = new RandomAccessFile(this.file, "r");
47 }
48
49 public long getFileSize() {
50 return file.getTotalSpace();
51 }
52
53 public List<FileBlock> getBlockList() {
54 final long blockByteSize = blockSizeInMB * 1024 * 1024;
55 final long fileSize = getFileSize();
56 final int numBlocks = (int) (fileSize / blockByteSize) + (fileSize % blockByteSize != 0 ? 1 : 0);
57
58 List<FileBlock> blockList = new ArrayList<FileBlock>(numBlocks);
59 long offset = 0;
60 for (int i = 0; i < numBlocks; i++, offset += blockByteSize) {
61 blockList.add(new FileBlock(file, i + 1, blockByteSize));
62 }
63
64 return blockList;
65 }
66
67 }