Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2012 The Broad Institute
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package htsjdk.variant.bcf2;
import htsjdk.tribble.TribbleException;
import htsjdk.variant.utils.GeneralUtils;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
public final class BCF2Decoder {
byte[] recordBytes = null;
ByteArrayInputStream recordStream = null;
public BCF2Decoder() {
// nothing to do
}
/**
* Create a new decoder ready to read BCF2 data from the byte[] recordBytes, for testing purposes
*
* @param recordBytes
*/
protected BCF2Decoder(final byte[] recordBytes) {
setRecordBytes(recordBytes);
}
// ----------------------------------------------------------------------
//
// Routines to load, set, skip blocks of underlying data we are decoding
//
// ----------------------------------------------------------------------
/**
* Reads the next record from input stream and prepare this decoder to decode values from it
*
* @param stream
*/
public void readNextBlock(final int blockSizeInBytes, final InputStream stream) {
if ( blockSizeInBytes < 0 ) throw new TribbleException("Invalid block size " + blockSizeInBytes);
setRecordBytes(readRecordBytes(blockSizeInBytes, stream));
}
/**
* Skips the next record from input stream, invalidating current block data
*
* @param stream
*/
public void skipNextBlock(final int blockSizeInBytes, final InputStream stream) {
try {
final int bytesRead = (int)stream.skip(blockSizeInBytes);
validateReadBytes(bytesRead, 1, blockSizeInBytes);
} catch ( IOException e ) {
throw new TribbleException("I/O error while reading BCF2 file", e);
}
this.recordBytes = null;
this.recordStream = null;
}
/**
* Returns the byte[] for the block of data we are currently decoding
* @return
*/
public byte[] getRecordBytes() {
return recordBytes;
}
/**
* The size of the current block in bytes
*
* @return
*/
public int getBlockSize() {
return recordBytes.length;
}
public boolean blockIsFullyDecoded() {
return recordStream.available() == 0;
}
/**
* Use the recordBytes[] to read BCF2 records from now on
*
* @param recordBytes
*/
public void setRecordBytes(final byte[] recordBytes) {
this.recordBytes = recordBytes;
this.recordStream = new ByteArrayInputStream(recordBytes);
}
// ----------------------------------------------------------------------
//
// High-level decoder
//
// ----------------------------------------------------------------------
public final Object decodeTypedValue() throws IOException {
final byte typeDescriptor = readTypeDescriptor();
return decodeTypedValue(typeDescriptor);
}
public final Object decodeTypedValue(final byte typeDescriptor) throws IOException {
final int size = decodeNumberOfElements(typeDescriptor);
return decodeTypedValue(typeDescriptor, size);
}
public final Object decodeTypedValue(final byte typeDescriptor, final int size) throws IOException {
if ( size == 0 ) {
// missing value => null in java
return null;
} else {
final BCF2Type type = BCF2Utils.decodeType(typeDescriptor);
if ( type == BCF2Type.CHAR ) { // special case string decoding for efficiency
return decodeLiteralString(size);
} else if ( size == 1 ) {
return decodeSingleValue(type);
} else {
final ArrayList