Source:
<code>public static Block read(final int major, InputStream inputStream) { final boolean v3OrHigher = major >= CramVersions.CRAM_v3.major; if (v3OrHigher) { inputStream = new CRC32InputStream(inputStream); } try { final BlockCompressionMethod method = BlockCompressionMethod.byId(inputStream.read()); final BlockContentType type = BlockContentType.byId(inputStream.read()); final int contentId = ITF8.readUnsignedITF8(inputStream); final int compressedSize = ITF8.readUnsignedITF8(inputStream); final int rawSize = ITF8.readUnsignedITF8(inputStream); final byte[] compressedContent = new byte[compressedSize]; InputStreamUtils.readFully(inputStream, compressedContent, 0, compressedSize); if (v3OrHigher) { final int actualChecksum = ((CRC32InputStream) inputStream).getCRC32(); final int checksum = CramInt.readInt32(inputStream); if (checksum != actualChecksum) { throw new RuntimeException(String.format("Block CRC32 mismatch: %04x vs %04x", checksum, actualChecksum)); } } <START> final byte[] uncompressedContent = ExternalCompression.uncompress(method, compressedContent); <END> if (uncompressedContent.length != rawSize) { throw new CRAMException(String.format("Block uncompressed size did not match expected size: %04x vs %04x", rawSize, uncompressedContent.length)); } switch (type) { case FILE_HEADER: return new FileHeaderBlock(method, compressedContent); case COMPRESSION_HEADER: return new CompressionHeaderBlock(method, compressedContent); case MAPPED_SLICE: return new SliceHeaderBlock(method, compressedContent); case EXTERNAL: return new ExternalDataBlock(method, compressedContent, contentId); case CORE: return new CoreDataBlock(method, compressedContent); default: throw new CRAMException("Unknown BlockContentType " + type.name()); } } catch (final IOException e) { throw new RuntimeIOException(e); } }</code><technical_language>validation a good idea, expensive do speculatively. do lazily "un-compression" instead. retain original size stream, cheap</technical_language>

Target:
public static Block read(final int major, InputStream inputStream) { final boolean v3OrHigher = major >= CramVersions.CRAM_v3.major; if (v3OrHigher) { inputStream = new CRC32InputStream(inputStream); } try { final BlockCompressionMethod compressionMethod = BlockCompressionMethod.byId(inputStream.read()); final BlockContentType contentType = BlockContentType.byId(inputStream.read()); final int contentId = ITF8.readUnsignedITF8(inputStream); final int compressedSize = ITF8.readUnsignedITF8(inputStream); final int uncompressedSize = ITF8.readUnsignedITF8(inputStream); final byte[] compressedContent = new byte[compressedSize]; InputStreamUtils.readFully(inputStream, compressedContent, 0, compressedSize); if (v3OrHigher) { final int actualChecksum = ((CRC32InputStream) inputStream).getCRC32(); final int checksum = CramInt.readInt32(inputStream); if (checksum != actualChecksum) { throw new RuntimeException(String.format("Block CRC32 mismatch: %04x vs %04x", checksum, actualChecksum)); } } if (contentType == BlockContentType.EXTERNAL) { return new ExternalBlock(compressionMethod, contentId, compressedContent, uncompressedSize); } else { return new Block(compressionMethod, contentType, compressedContent, uncompressedSize); } } catch (final IOException e) { throw new RuntimeIOException(e); } }