View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2010 utgenome.org
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *--------------------------------------------------------------------------*/
16  //--------------------------------------
17  // utgb-core Project
18  //
19  // BlockwiseFileReader.java
20  // Since: 2010/10/05
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   * Reading files in block-wise manner
33   * 
34   * @author leo
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  }