@Override public CRAMCodec<Integer> buildCodec(final BitInputStream coreBlockInputStream, final BitOutputStream coreBlockOutputStream, final Map<Integer, ByteArrayInputStream> externalBlockInputMap, final Map<Integer, ByteArrayOutputStream> externalBlockOutputMap) { return new BetaIntegerCodec(coreBlockInputStream, coreBlockOutputStream, offset, bitsPerValue); }
@Override public final void write(final Integer value) { coreBlockOutputStream.write(getAndCheckOffsetValue(value), bitsPerValue); } }
@Override public CRAMCodec<Integer> buildCodec(final BitInputStream coreBlockInputStream, final BitOutputStream coreBlockOutputStream, final Map<Integer, ByteArrayInputStream> externalBlockInputMap, final Map<Integer, ByteArrayOutputStream> externalBlockOutputMap) { return new BetaIntegerCodec(coreBlockInputStream, coreBlockOutputStream, offset, bitsPerValue); }
@Override public final void write(final Integer value) { try { coreBlockOutputStream.write(getAndCheckOffsetValue(value), bitsPerValue); } catch (IOException e) { throw new RuntimeIOException(e); } } }
@Test(dataProvider = "overflow", expectedExceptions = IllegalArgumentException.class) public void overflow(final int bitsPerValue, final int offset, final int value) throws IOException { try (final ByteArrayOutputStream os = new ByteArrayOutputStream(); final BitOutputStream bos = new DefaultBitOutputStream(os)) { final CRAMCodec<Integer> codec = new BetaIntegerCodec(null, bos, offset, bitsPerValue); codec.write(value); } }
@Test(dataProvider = "negativeTest", expectedExceptions = IllegalArgumentException.class) public void negativeTest(final int bitsPerValue, final int offset, final int value) throws IOException { try (final ByteArrayOutputStream os = new ByteArrayOutputStream(); final BitOutputStream bos = new DefaultBitOutputStream(os)) { final CRAMCodec<Integer> codec = new BetaIntegerCodec(null, bos, offset, bitsPerValue); codec.write(value); } } }
private void testCodec(final int offset, final int bitsPerValue, final int[] values) throws IOException { byte[] written; try (final ByteArrayOutputStream os = new ByteArrayOutputStream(); final BitOutputStream bos = new DefaultBitOutputStream(os)) { final CRAMCodec<Integer> writeCodec = new BetaIntegerCodec(null, bos, offset, bitsPerValue); for (final int value : values) { writeCodec.write(value); } bos.flush(); written = os.toByteArray(); } final int[] actual = new int[values.length]; try (final InputStream is = new ByteArrayInputStream(written); final DefaultBitInputStream dbis = new DefaultBitInputStream(is)) { final CRAMCodec<Integer> readCodec = new BetaIntegerCodec(dbis, null, offset, bitsPerValue); for (int i = 0; i < values.length; i++) { actual[i] = readCodec.read(); } } Assert.assertEquals(actual, values); }
final CRAMCodec<Integer> lenWriteCodec = new BetaIntegerCodec(null, coreBitOS, 0, 8); final CRAMCodec<byte[]> valWriteCodec = new ExternalByteArrayCodec(null, externalOS); final ByteArrayInputStream externalIS = new ByteArrayInputStream(writtenExternal)) { final CRAMCodec<Integer> lenReadCodec = new BetaIntegerCodec(coreBitIS, null, 0, 8); final CRAMCodec<byte[]> valReadCodec = new ExternalByteArrayCodec(externalIS, null);