@Override public Long call() throws Exception { IOUtil.copyStream(is, os); return 0L; }
/** * Read the {@link InputStream} until the end into a new byte array. * * @param input the input stream to read * @return a new byte array containing data from the input stream */ public static byte[] readFully(final InputStream input) { final ByteArrayOutputStream output = new ByteArrayOutputStream(); IOUtil.copyStream(input, output); return output.toByteArray(); }
/** * Read the {@link InputStream} until the end into a new byte array. * * @param input the input stream to read * @return a new byte array containing data from the input stream */ public static byte[] readFully(final InputStream input) { final ByteArrayOutputStream output = new ByteArrayOutputStream(); IOUtil.copyStream(input, output); return output.toByteArray(); }
/** * Copy input to output, overwriting output if it already exists. */ public static void copyFile(final File input, final File output) { try { final InputStream is = new FileInputStream(input); final OutputStream os = new FileOutputStream(output); copyStream(is, os); os.close(); is.close(); } catch (IOException e) { throw new SAMException("Error copying " + input + " to " + output, e); } }
/** * Copy input to output, overwriting output if it already exists. */ public static void copyFile(final File input, final File output) { try { final InputStream is = new FileInputStream(input); final OutputStream os = new FileOutputStream(output); copyStream(is, os); os.close(); is.close(); } catch (IOException e) { throw new SAMException("Error copying " + input + " to " + output, e); } }
/** * Copy input to output, overwriting output if it already exists. */ public static void copyFile(final File input, final File output) { try { final InputStream is = new FileInputStream(input); final OutputStream os = new FileOutputStream(output); copyStream(is, os); os.close(); is.close(); } catch (IOException e) { throw new SAMException("Error copying " + input + " to " + output, e); } }
/** * Compress a byte array into GZIP blob. The method obeys {@link ExternalCompression#GZIP_COMPRESSION_LEVEL} compression level. * * @param data byte array to compress * @return compressed blob */ public static byte[] gzip(final byte[] data) throws IOException { final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); final GZIPOutputStream gos = new GZIPOutputStream(byteArrayOutputStream) { { def.setLevel(GZIP_COMPRESSION_LEVEL); } }; IOUtil.copyStream(new ByteArrayInputStream(data), gos); gos.close(); return byteArrayOutputStream.toByteArray(); }
/** * Compress a byte array into BZIP2 blob. * * @param data byte array to compress * @return compressed blob */ public static byte[] bzip2(final byte[] data) throws IOException { final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); final BZip2CompressorOutputStream bos = new BZip2CompressorOutputStream(byteArrayOutputStream); IOUtil.copyStream(new ByteArrayInputStream(data), bos); bos.close(); return byteArrayOutputStream.toByteArray(); }
/** * Compress a byte array into BZIP2 blob. * * @param data byte array to compress * @return compressed blob */ public static byte[] bzip2(final byte[] data) { final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try (final BZip2CompressorOutputStream bos = new BZip2CompressorOutputStream(byteArrayOutputStream)) { IOUtil.copyStream(new ByteArrayInputStream(data), bos); } catch (final IOException e) { throw new RuntimeIOException(e); } return byteArrayOutputStream.toByteArray(); }
/** * Compress a byte array into GZIP blob. The method obeys {@link ExternalCompression#GZIP_COMPRESSION_LEVEL} compression level. * * @param data byte array to compress * @return compressed blob */ public static byte[] gzip(final byte[] data) { final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try (final GZIPOutputStream gos = new GZIPOutputStream(byteArrayOutputStream) { { def.setLevel(GZIP_COMPRESSION_LEVEL); } }) { IOUtil.copyStream(new ByteArrayInputStream(data), gos); } catch (final IOException e) { throw new RuntimeIOException(e); } return byteArrayOutputStream.toByteArray(); }
private static String addToRefCache(String md5, InputStream stream) { String localPath = new PathPattern(REF_CACHE).format(md5); File cachedFile = new File(localPath); if (!cachedFile.exists()) { log.info(String.format("Adding to REF_CACHE sequence md5=%s", md5)); cachedFile.getParentFile().mkdirs(); File tmpFile; try { tmpFile = File.createTempFile(md5, ".tmp", cachedFile.getParentFile()); FileOutputStream fos = new FileOutputStream(tmpFile); IOUtil.copyStream(stream, fos); fos.close(); if (!cachedFile.exists()) tmpFile.renameTo(cachedFile); else tmpFile.delete(); } catch (IOException e) { throw new RuntimeException(e); } } return localPath; }
private static byte[] readFile(File file) throws FileNotFoundException { FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtil.copyStream(fis, baos); return baos.toByteArray(); }
IOUtil.copyStream(scriptStream, scriptFileStream); return scriptFile; } catch (IOException e) {
private static byte[] readFile(File file) throws FileNotFoundException { FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtil.copyStream(fis, baos); return baos.toByteArray(); }
IOUtil.copyStream(scriptStream, scriptFileStream); return scriptFile; } catch (IOException e) {
private void testUsingZippedInput(final String filepath, final int nVariants, final Function<File,OutputStream> outputStreamProvider) throws IOException { File tmp = new File(filepath); /* TODO fix this when VCFFileReader will support BCF see * https://github.com/samtools/htsjdk/pull/837#discussion_r139490218 * https://github.com/samtools/htsjdk/issues/946 */ if( tmp.getName().endsWith(IOUtil.VCF_FILE_EXTENSION)) { tmp = File.createTempFile("tmp",IOUtil.COMPRESSED_VCF_FILE_EXTENSION); tmp.deleteOnExit(); try( FileInputStream in = new FileInputStream(filepath); OutputStream out = outputStreamProvider.apply(tmp); ) { IOUtil.copyStream(in, out); out.flush(); } catch(final IOException err) { throw err; } } try (final VCFIterator r = new VCFIteratorBuilder().open(tmp) ) { assertExpectedNumberOfVariants(r, nVariants); } }
void finish(final SBIIndex.Header header, final long finalVirtualOffset) { // complete writing the temp offsets file writeVirtualOffset(finalVirtualOffset); tempOffsetsCodec.close(); try (BinaryCodec binaryCodec = new BinaryCodec(out); InputStream tempOffsets = new BufferedInputStream(Files.newInputStream(tempOffsetsFile))) { // header binaryCodec.writeBytes(SBIIndex.SBI_MAGIC); binaryCodec.writeLong(header.getFileLength()); binaryCodec.writeBytes(header.getMd5()); binaryCodec.writeBytes(header.getUuid()); binaryCodec.writeLong(header.getTotalNumberOfRecords()); binaryCodec.writeLong(header.getGranularity()); binaryCodec.writeLong(virtualOffsetCount); // offsets IOUtil.copyStream(tempOffsets, out); } catch (IOException e) { throw new RuntimeIOException(e); } finally { try { Files.delete(tempOffsetsFile); } catch (IOException e) { throw new RuntimeIOException(e); } } } }
void finish(final SBIIndex.Header header, final long finalVirtualOffset) { // complete writing the temp offsets file writeVirtualOffset(finalVirtualOffset); tempOffsetsCodec.close(); try (BinaryCodec binaryCodec = new BinaryCodec(out); InputStream tempOffsets = new BufferedInputStream(Files.newInputStream(tempOffsetsFile))) { // header binaryCodec.writeBytes(SBIIndex.SBI_MAGIC); binaryCodec.writeLong(header.getFileLength()); binaryCodec.writeBytes(header.getMd5()); binaryCodec.writeBytes(header.getUuid()); binaryCodec.writeLong(header.getTotalNumberOfRecords()); binaryCodec.writeLong(header.getGranularity()); binaryCodec.writeLong(virtualOffsetCount); // offsets IOUtil.copyStream(tempOffsets, out); } catch (IOException e) { throw new RuntimeIOException(e); } finally { try { Files.delete(tempOffsetsFile); } catch (IOException e) { throw new RuntimeIOException(e); } } } }
@Test public void testOpenIndexUrlAsBaiOrNull() throws IOException { final SAMSequenceDictionary dictionary = new SAMSequenceDictionary(); dictionary.addSequence(new SAMSequenceRecord("1", 100)); final File file = File.createTempFile("test", ".crai"); file.deleteOnExit(); final FileOutputStream fos = new FileOutputStream(file); SAMFileHeader header = new SAMFileHeader(); header.setSortOrder(SAMFileHeader.SortOrder.coordinate); CRAMCRAIIndexer indexer = new CRAMCRAIIndexer(fos, header); final CRAIEntry entry = CRAIEntryTest.newEntry(0, 1, 2, 5, 3, 4); indexer.addEntry(entry); indexer.finish(); fos.close(); final InputStream baiStream = SamIndexes.openIndexUrlAsBaiOrNull(file.toURI().toURL(), dictionary); Assert.assertNotNull(baiStream); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtil.copyStream(baiStream, baos); final CachingBAMFileIndex bamIndex = new CachingBAMFileIndex(new SeekableMemoryStream(baos.toByteArray(), null), dictionary); final BAMFileSpan span = bamIndex.getSpanOverlapping(entry.getSequenceId(), entry.getAlignmentStart(), entry.getAlignmentStart()); Assert.assertNotNull(span); final long[] coordinateArray = span.toCoordinateArray(); Assert.assertEquals(coordinateArray.length, 2); Assert.assertEquals(coordinateArray[0] >> 16, entry.getContainerStartByteOffset()); Assert.assertEquals(coordinateArray[1] & 0xFFFF, 1); } }
@Test public void testCraiInMemory() throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); SAMFileHeader header = new SAMFileHeader(); header.setSortOrder(SAMFileHeader.SortOrder.coordinate); CRAMCRAIIndexer indexer = new CRAMCRAIIndexer(baos, header); final CRAIEntry entry = CRAIEntryTest.newEntry(0, 1, 2, 5, 3, 4); indexer.addEntry(entry); indexer.finish(); baos.close(); final SAMSequenceDictionary dictionary = new SAMSequenceDictionary(); dictionary.addSequence(new SAMSequenceRecord("1", 100)); final InputStream baiStream = SamIndexes.asBaiStreamOrNull(new ByteArrayInputStream(baos.toByteArray()), dictionary); Assert.assertNotNull(baiStream); baos = new ByteArrayOutputStream(); IOUtil.copyStream(baiStream, baos); final CachingBAMFileIndex bamIndex = new CachingBAMFileIndex(new SeekableMemoryStream(baos.toByteArray(), null), dictionary); final BAMFileSpan span = bamIndex.getSpanOverlapping(entry.getSequenceId(), entry.getAlignmentStart(), entry.getAlignmentStart()); Assert.assertNotNull(span); final long[] coordinateArray = span.toCoordinateArray(); Assert.assertEquals(coordinateArray.length, 2); Assert.assertEquals(coordinateArray[0] >> 16, entry.getContainerStartByteOffset()); Assert.assertEquals(coordinateArray[1] & 0xFFFF, 1); }