package net.sf.cram.encoding;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.Map;
import net.sf.cram.EncodingID;
import net.sf.cram.EncodingParams;
import net.sf.cram.io.ByteBufferUtils;
import net.sf.cram.io.ExposedByteArrayOutputStream;
public class BetaIntegerEncoding implements Encoding<Integer> {
public static final EncodingID ENCODING_ID = EncodingID.BETA;
private int offset;
private int bitLimit;
public BetaIntegerEncoding() {
}
public BetaIntegerEncoding(int bitLimit) {
this.bitLimit = bitLimit;
}
@Override
public EncodingID id() {
return ENCODING_ID;
}
public static EncodingParams toParam(int offset, int bitLimit) {
BetaIntegerEncoding e = new BetaIntegerEncoding();
e.offset = offset;
e.bitLimit = bitLimit;
return new EncodingParams(ENCODING_ID, e.toByteArray());
}
@Override
public byte[] toByteArray() {
ByteBuffer buf = ByteBuffer.allocate(10);
ByteBufferUtils.writeUnsignedITF8(offset, buf);
ByteBufferUtils.writeUnsignedITF8(bitLimit, buf);
buf.flip();
byte[] array = new byte[buf.limit()];
buf.get(array);
return array;
}
@Override
public void fromByteArray(byte[] data) {
ByteBuffer buf = ByteBuffer.wrap(data);
offset = ByteBufferUtils.readUnsignedITF8(buf);
bitLimit = ByteBufferUtils.readUnsignedITF8(buf);
}
@Override
public BitCodec<Integer> buildCodec(Map<Integer, InputStream> inputMap,
Map<Integer, ExposedByteArrayOutputStream> outputMap) {
return new BetaIntegerCodec(offset, bitLimit);
}
}