/** * Write this CompressionHeader out to an internal OutputStream, wrap it in a Block, and write that * Block out to the passed-in OutputStream. * * @param cramVersion the CRAM major version number * @param blockStream the stream to write to */ public void write(final Version cramVersion, final OutputStream blockStream) { try (final ByteArrayOutputStream internalOutputStream = new ByteArrayOutputStream()) { internalWrite(internalOutputStream); final Block block = Block.createRawCompressionHeaderBlock(internalOutputStream.toByteArray()); block.write(cramVersion.major, blockStream); } catch (final IOException e) { throw new RuntimeIOException(e); } }
public static void write(final int major, final Slice slice, final OutputStream outputStream) { slice.nofBlocks = 1 + slice.external.size() + (slice.embeddedRefBlock == null ? 0 : 1); { slice.contentIDs = new int[slice.external.size()]; final int i = 0; for (final int id : slice.external.keySet()) slice.contentIDs[i] = id; } slice.headerBlock = Block.createRawSliceHeaderBlock(createSliceHeaderBlockContent(major, slice)); slice.headerBlock.write(major, outputStream); slice.coreBlock.write(major, outputStream); for (final Block block : slice.external.values()) block.write(major, outputStream); }
@Override public String toString() { final byte[] uncompressed = getUncompressedContent(); final byte[] compressed = getCompressedContent(); final String raw = Arrays.toString(Arrays.copyOf(uncompressed, Math.min(5, uncompressed.length))); final String comp = Arrays.toString(Arrays.copyOf(compressed, Math.min(5, compressed.length))); return String.format("compression method=%s, content type=%s, id=%d, raw size=%d, compressed size=%d, raw=%s, comp=%s.", getCompressionMethod().name(), getContentType().name(), getContentId(), getUncompressedContentSize(), getCompressedContentSize(), raw, comp); } }
private static void readSliceBlocks(final int major, final Slice slice, final InputStream inputStream) { slice.external = new HashMap<>(); for (int i = 0; i < slice.nofBlocks; i++) { final Block block = Block.read(major, inputStream); switch (block.getContentType()) { case CORE: slice.coreBlock = block; break; case EXTERNAL: if (slice.embeddedRefBlockContentID == block.getContentId()) { slice.embeddedRefBlock = block; } slice.external.put(block.getContentId(), block); break; default: throw new RuntimeException("Not a slice block, content type id " + block.getContentType().name()); } } }
private void contentCheck(final Block actual, final byte[] expectedRaw, final byte[] expectedCompressed) { // raw and compressed data are equal to what was given Assert.assertEquals(actual.getUncompressedContent(), expectedRaw); Assert.assertEquals(actual.getUncompressedContentSize(), expectedRaw.length); Assert.assertEquals(actual.getCompressedContent(), expectedCompressed); Assert.assertEquals(actual.getCompressedContentSize(), expectedCompressed.length); }
/** * Read a COMPRESSION_HEADER Block from an InputStream and return its contents as a CompressionHeader * We do this instead of reading the InputStream directly because the Block content may be compressed * * @param cramVersion the CRAM version * @param blockStream the stream to read from * @return a new CompressionHeader from the input */ public static CompressionHeader read(final int cramVersion, final InputStream blockStream) { final Block block = Block.read(cramVersion, blockStream); if (block.getContentType() != BlockContentType.COMPRESSION_HEADER) throw new RuntimeIOException("Compression Header Block expected, found: " + block.getContentType().name()); try (final ByteArrayInputStream internalStream = new ByteArrayInputStream(block.getUncompressedContent())) { final CompressionHeader header = new CompressionHeader(); header.internalRead(internalStream); return header; } catch (final IOException e) { throw new RuntimeIOException(e); } }
@Test public void uncompressedTest() { final byte[] testData = "TEST STRING".getBytes(); final Block fhBlock = Block.createRawFileHeaderBlock(testData); contentCheck(fhBlock, testData, testData); final Block chBlock = Block.createRawCompressionHeaderBlock(testData); contentCheck(chBlock, testData, testData); final Block shBlock = Block.createRawSliceHeaderBlock(testData); contentCheck(shBlock, testData, testData); final Block core = Block.createRawCoreDataBlock(testData); contentCheck(core, testData, testData); }
private static long writeContainerForSamFileHeader(final int major, final SAMFileHeader samFileHeader, final OutputStream os) { final byte[] data = toByteArray(samFileHeader); final int length = Math.max(1024, data.length + data.length / 2); final byte[] blockContent = new byte[length]; System.arraycopy(data, 0, blockContent, 0, Math.min(data.length, length)); final Block block = Block.createRawFileHeaderBlock(blockContent); final Container container = new Container(); container.blockCount = 1; container.blocks = new Block[]{block}; container.landmarks = new int[0]; container.slices = new Slice[0]; container.alignmentSpan = Slice.NO_ALIGNMENT_SPAN; container.alignmentStart = Slice.NO_ALIGNMENT_START; container.bases = 0; container.globalRecordCounter = 0; container.nofRecords = 0; container.sequenceId = 0; final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); block.write(major, byteArrayOutputStream); container.containerByteSize = byteArrayOutputStream.size(); final int containerHeaderByteSize = ContainerIO.writeContainerHeader(major, container, os); try { os.write(byteArrayOutputStream.toByteArray(), 0, byteArrayOutputStream.size()); } catch (final IOException e) { throw new RuntimeIOException(e); } return containerHeaderByteSize + byteArrayOutputStream.size(); }
final byte[] bytes = new byte[container.containerByteSize]; InputStreamUtils.readFully(inputStream, bytes, 0, bytes.length); block = Block.read(version.major, new ByteArrayInputStream(bytes)); block = Block.read(version.major, inputStream); inputStream = new ByteArrayInputStream(block.getUncompressedContent());
private Block roundTrip(final Block in, final Version version) throws IOException { byte[] written; try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) { in.write(version.major, os); written = os.toByteArray(); } try (final InputStream is = new ByteArrayInputStream(written)) { return Block.read(version.major, is); } }
final boolean isFileHeaderContainer = firstBlock.getContentType() == BlockContentType.FILE_HEADER; if (isFileHeaderContainer) { final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); firstBlock.write(version.major, byteArrayOutputStream); container.containerByteSize = byteArrayOutputStream.size();
private Map<Integer, ByteArrayInputStream> getExternalBlockInputMap() { return external.entrySet() .stream() .collect(Collectors.toMap( Map.Entry::getKey, e -> new ByteArrayInputStream(e.getValue().getUncompressedContent()))); }
@Test(dataProvider = "RoundTripTest") public void testCoreBlockRoundTrips(final byte[] testData, final Version version) throws IOException { final Block coreBlock = Block.createRawCoreDataBlock(testData); final Block rtBlock = roundTrip(coreBlock, version); contentCheck(rtBlock, testData, testData); }
@Test(dataProvider = "RoundTripTest") public void testFileHeaderBlockRoundTrips(final byte[] testData, final Version version) throws IOException { final Block fhBlock = Block.createRawFileHeaderBlock(testData); final Block rtBlock = roundTrip(fhBlock, version); contentCheck(rtBlock, testData, testData); }
@Test(dataProvider = "RoundTripTest") public void testSliceHeaderBlockRoundTrips(final byte[] testData, final Version version) throws IOException { final Block shBlock = Block.createRawSliceHeaderBlock(testData); final Block rtBlock = roundTrip(shBlock, version); contentCheck(rtBlock, testData, testData); }
@Test(dataProvider = "RoundTripTest") public void testCompressionHeaderBlockRoundTrips(final byte[] testData, final Version version) throws IOException { final Block chBlock = Block.createRawCompressionHeaderBlock(testData); final Block rtBlock = roundTrip(chBlock, version); contentCheck(rtBlock, testData, testData); }
Block.read(version.major, cramSeekableStream); long blockByteSyze = cramSeekableStream.position() - firstBlockStart - crcByteSize; cramSeekableStream.seek(firstBlockStart);
/** * Read a COMPRESSION_HEADER Block from an InputStream and return its contents as a CompressionHeader * We do this instead of reading the InputStream directly because the Block content may be compressed * * @param cramVersion the CRAM version * @param blockStream the stream to read from * @return a new CompressionHeader from the input */ public static CompressionHeader read(final int cramVersion, final InputStream blockStream) { final Block block = Block.read(cramVersion, blockStream); if (block.getContentType() != BlockContentType.COMPRESSION_HEADER) throw new RuntimeIOException("Compression Header Block expected, found: " + block.getContentType().name()); try (final ByteArrayInputStream internalStream = new ByteArrayInputStream(block.getUncompressedContent())) { final CompressionHeader header = new CompressionHeader(); header.internalRead(internalStream); return header; } catch (final IOException e) { throw new RuntimeIOException(e); } }
countingInputStream.close(); final Block block = Block.createRawFileHeaderBlock(toByteArray(newHeader.getSamFileHeader())); final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); block.write(newHeader.getVersion().major, byteArrayOutputStream); if (byteArrayOutputStream.size() > c.containerByteSize) { log.error("Failed to replace CRAM header because the new header does not fit.");
private static void readSliceBlocks(final int major, final Slice slice, final InputStream inputStream) throws IOException { slice.external = new HashMap<>(); for (int i = 0; i < slice.nofBlocks; i++) { final Block block = Block.read(major, inputStream); switch (block.getContentType()) { case CORE: slice.coreBlock = block; break; case EXTERNAL: if (slice.embeddedRefBlockContentID == block.getContentId()) { slice.embeddedRefBlock = block; } slice.external.put(block.getContentId(), block); break; default: throw new RuntimeException("Not a slice block, content type id " + block.getContentType().name()); } } }