@Override public String toString() { return String.format("BCF%d.%d", getMajorVersion(), getMinorVersion()); } }
@Override public boolean canDecode( final String path ) { try (InputStream fis = Files.newInputStream(IOUtil.getPath(path)) ){ final BCFVersion version = BCFVersion.readBCFVersion(fis); return version != null && version.getMajorVersion() == ALLOWED_MAJOR_VERSION; } catch ( final IOException e ) { return false; } }
try { bcfVersion = BCFVersion.readBCFVersion(inputStream); if ( bcfVersion == null ) error("Input stream does not contain a BCF encoded file; BCF magic header info not found"); if ( bcfVersion.getMajorVersion() != ALLOWED_MAJOR_VERSION ) error("BCF2Codec can only process BCF2 files, this file has major version " + bcfVersion.getMajorVersion()); if ( bcfVersion.getMinorVersion() < MIN_MINOR_VERSION ) error("BCF2Codec can only process BCF2 files with minor version >= " + MIN_MINOR_VERSION + " but this file has minor version " + bcfVersion.getMinorVersion());
@Override public void writeHeader(VCFHeader header) { setHeader(header); try { // write out the header into a byte stream, get its length, and write everything to the file final ByteArrayOutputStream capture = new ByteArrayOutputStream(); final OutputStreamWriter writer = new OutputStreamWriter(capture); this.header = VCFWriter.writeHeader(this.header, writer, VCFWriter.getVersionLine(), "BCF2 stream"); writer.append('\0'); // the header is null terminated by a byte writer.close(); final byte[] headerBytes = capture.toByteArray(); new BCFVersion(MAJOR_VERSION, MINOR_VERSION).write(outputStream); BCF2Type.INT32.write(headerBytes.length, outputStream); outputStream.write(headerBytes); outputHasBeenWritten = true; } catch (IOException e) { throw new RuntimeIOException("BCF2 stream: Got IOException while trying to write BCF2 header", e); } }
/** * Return a new BCFVersion object describing the major and minor version of the BCF file in stream * * Note that stream must be at the very start of the file. * * @param stream * @return a BCFVersion object, or null if stream doesn't contain a BCF file * @throws IOException */ public static BCFVersion readBCFVersion(final InputStream stream) throws IOException { final byte[] magicBytes = new byte[MAGIC_HEADER_START.length]; stream.read(magicBytes); if ( Arrays.equals(magicBytes, MAGIC_HEADER_START) ) { // we're a BCF file final int majorByte = stream.read(); final int minorByte = stream.read(); return new BCFVersion( majorByte, minorByte ); } else return null; }
/** try to read a BCFVersion from an uncompressed BufferedInputStream. * The buffer must be large enough to contain {@link #SIZEOF_BCF_HEADER} * * @param bufferedinput the uncompressed input stream * @return the BCFVersion if it can be decoded, or null if not found. * @throws IOException */ public static BCFVersion tryReadBCFVersion(final BufferedInputStream uncompressedBufferedInput) throws IOException { uncompressedBufferedInput.mark(SIZEOF_BCF_HEADER); final BCFVersion bcfVersion = BCFVersion.readBCFVersion(uncompressedBufferedInput); uncompressedBufferedInput.reset(); return bcfVersion; }
try { bcfVersion = BCFVersion.readBCFVersion(inputStream); if ( bcfVersion == null ) error("Input stream does not contain a BCF encoded file; BCF magic header info not found"); if ( bcfVersion.getMajorVersion() != ALLOWED_MAJOR_VERSION ) error("BCF2Codec can only process BCF2 files, this file has major version " + bcfVersion.getMajorVersion()); if ( bcfVersion.getMinorVersion() < MIN_MINOR_VERSION ) error("BCF2Codec can only process BCF2 files with minor version >= " + MIN_MINOR_VERSION + " but this file has minor version " + bcfVersion.getMinorVersion());
@Override public void writeHeader(VCFHeader header) { setHeader(header); try { // write out the header into a byte stream, get its length, and write everything to the file final ByteArrayOutputStream capture = new ByteArrayOutputStream(); final OutputStreamWriter writer = new OutputStreamWriter(capture); this.header = VCFWriter.writeHeader(this.header, writer, VCFWriter.getVersionLine(), "BCF2 stream"); writer.append('\0'); // the header is null terminated by a byte writer.close(); final byte[] headerBytes = capture.toByteArray(); new BCFVersion(MAJOR_VERSION, MINOR_VERSION).write(outputStream); BCF2Type.INT32.write(headerBytes.length, outputStream); outputStream.write(headerBytes); outputHasBeenWritten = true; } catch (IOException e) { throw new RuntimeIOException("BCF2 stream: Got IOException while trying to write BCF2 header", e); } }
/** * Return a new BCFVersion object describing the major and minor version of the BCF file in stream * * Note that stream must be at the very start of the file. * * @param stream * @return a BCFVersion object, or null if stream doesn't contain a BCF file * @throws IOException */ public static BCFVersion readBCFVersion(final InputStream stream) throws IOException { final byte[] magicBytes = new byte[MAGIC_HEADER_START.length]; stream.read(magicBytes); if ( Arrays.equals(magicBytes, MAGIC_HEADER_START) ) { // we're a BCF file final int majorByte = stream.read(); final int minorByte = stream.read(); return new BCFVersion( majorByte, minorByte ); } else return null; }
/** try to read a BCFVersion from an uncompressed BufferedInputStream. * The buffer must be large enough to contain {@link #SIZEOF_BCF_HEADER} * * @param bufferedinput the uncompressed input stream * @return the BCFVersion if it can be decoded, or null if not found. * @throws IOException */ public static BCFVersion tryReadBCFVersion(final BufferedInputStream uncompressedBufferedInput) throws IOException { uncompressedBufferedInput.mark(SIZEOF_BCF_HEADER); final BCFVersion bcfVersion = BCFVersion.readBCFVersion(uncompressedBufferedInput); uncompressedBufferedInput.reset(); return bcfVersion; }
try { bcfVersion = BCFVersion.readBCFVersion(inputStream); if ( bcfVersion == null ) error("Input stream does not contain a BCF encoded file; BCF magic header info not found"); if ( bcfVersion.getMajorVersion() != ALLOWED_MAJOR_VERSION ) error("BCF2Codec can only process BCF2 files, this file has major version " + bcfVersion.getMajorVersion()); if ( bcfVersion.getMinorVersion() < MIN_MINOR_VERSION ) error("BCF2Codec can only process BCF2 files with minor version >= " + MIN_MINOR_VERSION + " but this file has minor version " + bcfVersion.getMinorVersion());
@Override public String toString() { return String.format("BCF%d.%d", getMajorVersion(), getMinorVersion()); } }
@Override public boolean canDecode( final String path ) { try (InputStream fis = Files.newInputStream(IOUtil.getPath(path)) ){ final BCFVersion version = BCFVersion.readBCFVersion(fis); return version != null && version.getMajorVersion() == ALLOWED_MAJOR_VERSION; } catch ( final IOException e ) { return false; } }
new BCFVersion(MAJOR_VERSION, MINOR_VERSION).write(outputStream); BCF2Type.INT32.write(headerBytes.length, outputStream); outputStream.write(headerBytes);
/** * Return a new BCFVersion object describing the major and minor version of the BCF file in stream * * Note that stream must be at the very start of the file. * * @param stream * @return a BCFVersion object, or null if stream doesn't contain a BCF file * @throws IOException */ public static BCFVersion readBCFVersion(final InputStream stream) throws IOException { final byte[] magicBytes = new byte[MAGIC_HEADER_START.length]; stream.read(magicBytes); if ( Arrays.equals(magicBytes, MAGIC_HEADER_START) ) { // we're a BCF file final int majorByte = stream.read(); final int minorByte = stream.read(); return new BCFVersion( majorByte, minorByte ); } else return null; }
@Override public String toString() { return String.format("BCF%d.%d", getMajorVersion(), getMinorVersion()); } }
@Override public boolean canDecode( final String path ) { FileInputStream fis = null; try { fis = new FileInputStream(path); final BCFVersion version = BCFVersion.readBCFVersion(fis); return version != null && version.getMajorVersion() == ALLOWED_MAJOR_VERSION; } catch ( FileNotFoundException e ) { return false; } catch ( IOException e ) { return false; } finally { try { if ( fis != null ) fis.close(); } catch ( IOException e ) { // do nothing } } }
/** * Write out the BCF magic information indicating this is a BCF file with corresponding major and minor versions * @param out * @throws IOException */ public void write(final OutputStream out) throws IOException { out.write(MAGIC_HEADER_START); out.write(getMajorVersion() & 0xFF); out.write(getMinorVersion() & 0xFF); }
/** * Write out the BCF magic information indicating this is a BCF file with corresponding major and minor versions * @param out * @throws IOException */ public void write(final OutputStream out) throws IOException { out.write(MAGIC_HEADER_START); out.write(getMajorVersion() & 0xFF); out.write(getMinorVersion() & 0xFF); }
/** * Write out the BCF magic information indicating this is a BCF file with corresponding major and minor versions * @param out * @throws IOException */ public void write(final OutputStream out) throws IOException { out.write(MAGIC_HEADER_START); out.write(getMajorVersion() & 0xFF); out.write(getMinorVersion() & 0xFF); }