Java Code Examples for htsjdk.samtools.util.BlockCompressedInputStream#setCheckCrcs()

The following examples show how to use htsjdk.samtools.util.BlockCompressedInputStream#setCheckCrcs() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestBGZFSplitGuesser.java    From Hadoop-BAM with MIT License 5 votes vote down vote up
private void canReadFromBlockStart(long blockStart) throws IOException {
  BlockCompressedInputStream blockCompressedInputStream = new
      BlockCompressedInputStream(file);
  blockCompressedInputStream.setCheckCrcs(true);
  blockCompressedInputStream.seek(blockStart << 16);
  byte[] b = new byte[100];
  blockCompressedInputStream.read(b);
}
 
Example 2
Source File: BGZFSplitGuesser.java    From Hadoop-BAM with MIT License 4 votes vote down vote up
public long guessNextBGZFBlockStart(long beg, long end)
	throws IOException
{
	// Buffer what we need to go through. Since the max size of a BGZF block
	// is 0xffff (64K), and we might be just one byte off from the start of
	// the previous one, we need 0xfffe bytes for the start, and then 0xffff
	// for the block we're looking for.

	byte[] arr = new byte[2*0xffff - 1];

	this.seekableInFile.seek(beg);
	int totalRead = 0;
	for (int left = Math.min((int)(end - beg), arr.length); left > 0;) {
		final int r = inFile.read(arr, totalRead, left);
		if (r < 0)
			break;
		totalRead += r;
		left -= r;
	}
	arr = Arrays.copyOf(arr, totalRead);

	this.in = new ByteArraySeekableStream(arr);

	final BlockCompressedInputStream bgzf =
		new BlockCompressedInputStream(this.in);
	bgzf.setCheckCrcs(true);

	final int firstBGZFEnd = Math.min((int)(end - beg), 0xffff);

	for (int pos = 0;;) {
		pos = guessNextBGZFPos(pos, firstBGZFEnd);
		if (pos < 0)
			return end;

		try {
			// Seek in order to trigger decompression of the block and a CRC
			// check.
			bgzf.seek((long)pos << 16);

		// This has to catch Throwable, because it's possible to get an
		// OutOfMemoryError due to an overly large size.
		} catch (Throwable e) {
			// Guessed BGZF position incorrectly: try the next guess.
			++pos;
			continue;
		}
		return beg + pos;
	}
}