public static void assertNonDefectiveFile(final File file) throws IOException { if (checkTermination(file) == FileTermination.DEFECTIVE) { throw new SAMException(file.getAbsolutePath() + " does not have a valid GZIP block at the end of the file."); } }
/** * * @param file the file to check * @return status of the last compressed block * @throws IOException */ public static FileTermination checkTermination(final File file) throws IOException { return checkTermination(IOUtil.toPath(file)); }
/** * * @param path to the file to check * @return status of the last compressed block * @throws IOException */ public static FileTermination checkTermination(final Path path) throws IOException { try( final SeekableByteChannel channel = Files.newByteChannel(path, StandardOpenOption.READ) ){ return checkTermination(channel); } }
/** * * @param file the file to check * @return status of the last compressed block * @throws IOException */ public static FileTermination checkTermination(final File file) throws IOException { return checkTermination(IOUtil.toPath(file)); }
public static void assertNonDefectiveFile(final File file) throws IOException { if (checkTermination(file) == FileTermination.DEFECTIVE) { throw new SAMException(file.getAbsolutePath() + " does not have a valid GZIP block at the end of the file."); } }
/** * * @param path to the file to check * @return status of the last compressed block * @throws IOException */ public static FileTermination checkTermination(final Path path) throws IOException { try( final SeekableByteChannel channel = Files.newByteChannel(path, StandardOpenOption.READ) ){ return checkTermination(channel); } }
public static void assertNonDefectiveFile(final File file) throws IOException { if (checkTermination(file) == FileTermination.DEFECTIVE) { throw new SAMException(file.getAbsolutePath() + " does not have a valid GZIP block at the end of the file."); } }
/** * * @param file the file to check * @return status of the last compressed block * @throws IOException */ public static FileTermination checkTermination(final File file) throws IOException { return checkTermination(IOUtil.toPath(file)); }
/** * * @param path to the file to check * @return status of the last compressed block * @throws IOException */ public static FileTermination checkTermination(final Path path) throws IOException { try( final SeekableByteChannel channel = Files.newByteChannel(path, StandardOpenOption.READ) ){ return checkTermination(channel); } }
public static void assertNonDefectiveFile(final File file) throws IOException { if (checkTermination(file) == FileTermination.DEFECTIVE) { throw new SAMException(file.getAbsolutePath() + " does not have a valid GZIP block at the end of the file."); } }
@Test( dataProvider = "getFiles") public void testCheckTerminationForFiles(File compressedFile, BlockCompressedInputStream.FileTermination expected) throws IOException { Assert.assertEquals(BlockCompressedInputStream.checkTermination(compressedFile), expected); }
@Override protected int doWork() { IOUtil.assertFileIsReadable(INPUT); try { final FileTermination term = BlockCompressedInputStream.checkTermination(INPUT); System.err.println(term.name()); if (term == FileTermination.DEFECTIVE) { return 100; } else { return 0; } } catch (IOException ioe) { throw new PicardException("Exception reading terminator block of file: " + INPUT.getAbsolutePath()); } } }
@Override protected int doWork() { IOUtil.assertFileIsReadable(INPUT); try { final FileTermination term = BlockCompressedInputStream.checkTermination(INPUT); System.err.println(term.name()); if (term == FileTermination.DEFECTIVE) { return 100; } else { return 0; } } catch (IOException ioe) { throw new PicardException("Exception reading terminator block of file: " + INPUT.getAbsolutePath()); } } }
/** * close() must be called in order to flush any remaining buffered bytes. An unclosed file will likely be * defective. * */ @Override public void close() throws IOException { flush(); // For debugging... // if (numberOfThrottleBacks > 0) { // System.err.println("In BlockCompressedOutputStream, had to throttle back " + numberOfThrottleBacks + // " times for file " + codec.getOutputFileName()); // } codec.writeBytes(BlockCompressedStreamConstants.EMPTY_GZIP_BLOCK); codec.close(); // Can't re-open something that is not a regular file, e.g. a named pipe or an output stream if (this.file == null || !Files.isRegularFile(this.file)) return; if (BlockCompressedInputStream.checkTermination(this.file) != BlockCompressedInputStream.FileTermination.HAS_TERMINATOR_BLOCK) { throw new IOException("Terminator block not found after closing BGZF file " + this.file); } }
@Test( dataProvider = "getFiles") public void testCheckTerminationForSeekableByteChannels(File compressedFile, BlockCompressedInputStream.FileTermination expected) throws IOException { try(SeekableByteChannel channel = Files.newByteChannel(compressedFile.toPath())){ Assert.assertEquals(BlockCompressedInputStream.checkTermination(channel), expected); } }
@Test( dataProvider = "getFiles") public void testCheckTerminationForPaths(File compressedFile, BlockCompressedInputStream.FileTermination expected) throws IOException { try(FileSystem fs = Jimfs.newFileSystem("test", Configuration.unix())){ final Path compressedFileInJimfs = Files.copy(compressedFile.toPath(), fs.getPath("something")); Assert.assertEquals(BlockCompressedInputStream.checkTermination(compressedFileInJimfs), expected); } }
@Test(dataProvider = "getFiles") public void testChannelPositionIsRestored(File compressedFile, BlockCompressedInputStream.FileTermination expected) throws IOException { final long position = 50; try(SeekableByteChannel channel = Files.newByteChannel(compressedFile.toPath())){ channel.position(position); Assert.assertEquals(channel.position(), position); Assert.assertEquals(BlockCompressedInputStream.checkTermination(channel), expected); Assert.assertEquals(channel.position(), position); } }
public void validateBamFileTermination(final File inputFile) { try { if (!IOUtil.isBlockCompressed(inputFile.toPath())) { return; } final BlockCompressedInputStream.FileTermination terminationState = BlockCompressedInputStream.checkTermination(inputFile); if (terminationState.equals(BlockCompressedInputStream.FileTermination.DEFECTIVE)) { addError(new SAMValidationError(Type.TRUNCATED_FILE, "BAM file has defective last gzip block", inputFile.getPath())); } else if (terminationState.equals(BlockCompressedInputStream.FileTermination.HAS_HEALTHY_LAST_BLOCK)) { addError(new SAMValidationError(Type.BAM_FILE_MISSING_TERMINATOR_BLOCK, "Older BAM file -- does not have terminator block", inputFile.getPath())); } } catch (IOException e) { throw new SAMException("IOException", e); } }
public void validateBamFileTermination(final File inputFile) { try { if (!IOUtil.isBlockCompressed(inputFile.toPath())) { return; } final BlockCompressedInputStream.FileTermination terminationState = BlockCompressedInputStream.checkTermination(inputFile); if (terminationState.equals(BlockCompressedInputStream.FileTermination.DEFECTIVE)) { addError(new SAMValidationError(Type.TRUNCATED_FILE, "BAM file has defective last gzip block", inputFile.getPath())); } else if (terminationState.equals(BlockCompressedInputStream.FileTermination.HAS_HEALTHY_LAST_BLOCK)) { addError(new SAMValidationError(Type.BAM_FILE_MISSING_TERMINATOR_BLOCK, "Older BAM file -- does not have terminator block", inputFile.getPath())); } } catch (IOException e) { throw new SAMException("IOException", e); } }
public void validateBamFileTermination(final File inputFile) { BufferedInputStream inputStream = null; try { inputStream = IOUtil.toBufferedStream(new FileInputStream(inputFile)); if (!BlockCompressedInputStream.isValidFile(inputStream)) { return; } final BlockCompressedInputStream.FileTermination terminationState = BlockCompressedInputStream.checkTermination(inputFile); if (terminationState.equals(BlockCompressedInputStream.FileTermination.DEFECTIVE)) { addError(new SAMValidationError(Type.TRUNCATED_FILE, "BAM file has defective last gzip block", inputFile.getPath())); } else if (terminationState.equals(BlockCompressedInputStream.FileTermination.HAS_HEALTHY_LAST_BLOCK)) { addError(new SAMValidationError(Type.BAM_FILE_MISSING_TERMINATOR_BLOCK, "Older BAM file -- does not have terminator block", inputFile.getPath())); } } catch (IOException e) { throw new SAMException("IOException", e); } finally { if (inputStream != null) { CloserUtil.close(inputStream); } } }