private static CramHeader readFormatDefinition(final InputStream inputStream) throws IOException { for (final byte magicByte : CramHeader.MAGIC) { if (magicByte != inputStream.read()) throw new RuntimeException("Unknown file format."); } final Version version = new Version(inputStream.read(), inputStream.read(), 0); final CramHeader header = new CramHeader(version, null, null); final DataInputStream dataInputStream = new DataInputStream(inputStream); dataInputStream.readFully(header.getId()); return header; }
private static CramHeader readFormatDefinition(final InputStream inputStream) throws IOException { for (final byte magicByte : CramHeader.MAGIC) { if (magicByte != inputStream.read()) throw new RuntimeException("Unknown file format."); } final Version version = new Version(inputStream.read(), inputStream.read(), 0); final CramHeader header = new CramHeader(version, null, null); final DataInputStream dataInputStream = new DataInputStream(inputStream); dataInputStream.readFully(header.getId()); return header; }
/** * Read CRAM header from the given {@link InputStream}. * * @param inputStream input stream to read from * @return complete {@link CramHeader} object * @throws IOException as per java IO contract */ public static CramHeader readCramHeader(final InputStream inputStream) throws IOException { final CramHeader header = readFormatDefinition(inputStream); final SAMFileHeader samFileHeader = readSAMFileHeader(header.getVersion(), inputStream, new String(header.getId())); return new CramHeader(header.getVersion(), new String(header.getId()), samFileHeader); }
/** * Writes CRAM header into the specified {@link OutputStream}. * * @param cramHeader the {@link CramHeader} object to write * @param outputStream the output stream to write to * @return the number of bytes written out * @throws IOException as per java IO contract */ public static long writeCramHeader(final CramHeader cramHeader, final OutputStream outputStream) throws IOException { // if (cramHeader.getVersion().major < 3) throw new RuntimeException("Deprecated CRAM version: " + cramHeader.getVersion().major); outputStream.write("CRAM".getBytes("US-ASCII")); outputStream.write(cramHeader.getVersion().major); outputStream.write(cramHeader.getVersion().minor); outputStream.write(cramHeader.getId()); for (int i = cramHeader.getId().length; i < 20; i++) outputStream.write(0); final long length = CramIO.writeContainerForSamFileHeader(cramHeader.getVersion().major, cramHeader.getSamFileHeader(), outputStream); return CramIO.DEFINITION_LENGTH + length; }
/** * Read CRAM header from the given {@link InputStream}. * * @param inputStream input stream to read from * @return complete {@link CramHeader} object */ public static CramHeader readCramHeader(final InputStream inputStream) { try { final CramHeader header = readFormatDefinition(inputStream); final SAMFileHeader samFileHeader = readSAMFileHeader(header.getVersion(), inputStream, new String(header.getId())); return new CramHeader(header.getVersion(), new String(header.getId()), samFileHeader); } catch (final IOException e) { throw new RuntimeIOException(e); } }
/** * Writes CRAM header into the specified {@link OutputStream}. * * @param cramHeader the {@link CramHeader} object to write * @param outputStream the output stream to write to * @return the number of bytes written out */ public static long writeCramHeader(final CramHeader cramHeader, final OutputStream outputStream) { // if (cramHeader.getVersion().major < 3) throw new RuntimeException("Deprecated CRAM version: " + cramHeader.getVersion().major); try { outputStream.write("CRAM".getBytes("US-ASCII")); outputStream.write(cramHeader.getVersion().major); outputStream.write(cramHeader.getVersion().minor); outputStream.write(cramHeader.getId()); for (int i = cramHeader.getId().length; i < 20; i++) outputStream.write(0); final long length = CramIO.writeContainerForSamFileHeader(cramHeader.getVersion().major, cramHeader.getSamFileHeader(), outputStream); return CramIO.DEFINITION_LENGTH + length; } catch (final IOException e) { throw new RuntimeIOException(e); } }