Search in sources :

Example 21 with SeekableByteChannel

use of java.nio.channels.SeekableByteChannel in project gatk by broadinstitute.

the class GatherVcfs method getReaderFromVCFUri.

private static FeatureReader<VariantContext> getReaderFromVCFUri(final Path variantPath, final int cloudPrefetchBuffer) {
    final String variantURI = variantPath.toUri().toString();
    final Function<SeekableByteChannel, SeekableByteChannel> cloudWrapper = (cloudPrefetchBuffer > 0 ? is -> SeekableByteChannelPrefetcher.addPrefetcher(cloudPrefetchBuffer, is) : Function.identity());
    return AbstractFeatureReader.getFeatureReader(variantURI, null, new VCFCodec(), false, cloudWrapper, Function.identity());
}
Also used : CloseableIterator(htsjdk.samtools.util.CloseableIterator) CommandLineProgramProperties(org.broadinstitute.barclay.argparser.CommandLineProgramProperties) java.util(java.util) ProgressLogger(org.broadinstitute.hellbender.utils.runtime.ProgressLogger) IOUtil(htsjdk.samtools.util.IOUtil) VCFHeader(htsjdk.variant.vcf.VCFHeader) Argument(org.broadinstitute.barclay.argparser.Argument) FeatureReader(htsjdk.tribble.FeatureReader) VariantContextWriterBuilder(htsjdk.variant.variantcontext.writer.VariantContextWriterBuilder) StandardArgumentDefinitions(org.broadinstitute.hellbender.cmdline.StandardArgumentDefinitions) Function(java.util.function.Function) VariantProgramGroup(org.broadinstitute.hellbender.cmdline.programgroups.VariantProgramGroup) AbstractFeatureReader(htsjdk.tribble.AbstractFeatureReader) BlockCompressedOutputStream(htsjdk.samtools.util.BlockCompressedOutputStream) RuntimeIOException(htsjdk.samtools.util.RuntimeIOException) CollectionUtil(htsjdk.samtools.util.CollectionUtil) VCFCodec(htsjdk.variant.vcf.VCFCodec) PeekableIterator(htsjdk.samtools.util.PeekableIterator) PicardCommandLineProgram(org.broadinstitute.hellbender.cmdline.PicardCommandLineProgram) Path(java.nio.file.Path) CloserUtil(htsjdk.samtools.util.CloserUtil) BlockCompressedStreamConstants(htsjdk.samtools.util.BlockCompressedStreamConstants) IOUtils(org.broadinstitute.hellbender.utils.io.IOUtils) BlockCompressedInputStream(htsjdk.samtools.util.BlockCompressedInputStream) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) Collectors(java.util.stream.Collectors) VariantContextComparator(htsjdk.variant.variantcontext.VariantContextComparator) SeekableByteChannel(java.nio.channels.SeekableByteChannel) Logger(org.apache.logging.log4j.Logger) UserException(org.broadinstitute.hellbender.exceptions.UserException) java.io(java.io) Options(htsjdk.variant.variantcontext.writer.Options) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) SeekableByteChannelPrefetcher(org.broadinstitute.hellbender.utils.nio.SeekableByteChannelPrefetcher) VariantContext(htsjdk.variant.variantcontext.VariantContext) Utils(org.broadinstitute.hellbender.utils.Utils) VisibleForTesting(com.google.common.annotations.VisibleForTesting) LogManager(org.apache.logging.log4j.LogManager) SeekableByteChannel(java.nio.channels.SeekableByteChannel) VCFCodec(htsjdk.variant.vcf.VCFCodec)

Example 22 with SeekableByteChannel

use of java.nio.channels.SeekableByteChannel in project Singularity by HubSpot.

the class ArtifactManager method extract.

public void extract(EmbeddedArtifact embeddedArtifact, Path directory) {
    final Path extractTo = directory.resolve(embeddedArtifact.getFilename());
    final Path parent = extractTo.getParent();
    try {
        if (parent != null) {
            Files.createDirectories(parent);
        }
    } catch (IOException e) {
        throw new RuntimeException(String.format("Couldn't extract %s, unable to create directory %s", embeddedArtifact.getName(), parent), e);
    }
    log.info("Extracting {} bytes of {} to {}", embeddedArtifact.getContent().length, embeddedArtifact.getName(), extractTo);
    try (SeekableByteChannel byteChannel = Files.newByteChannel(extractTo, EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE))) {
        byteChannel.write(ByteBuffer.wrap(embeddedArtifact.getContent()));
    } catch (IOException e) {
        throw new RuntimeException(String.format("Couldn't extract %s", embeddedArtifact.getName()), e);
    }
    checkMd5(embeddedArtifact, extractTo);
}
Also used : Path(java.nio.file.Path) SeekableByteChannel(java.nio.channels.SeekableByteChannel) IOException(java.io.IOException)

Example 23 with SeekableByteChannel

use of java.nio.channels.SeekableByteChannel in project herddb by diennea.

the class FileUtils method fastReadFile.

public static byte[] fastReadFile(Path f) throws IOException {
    int len = (int) Files.size(f);
    if (USE_DIRECT_BUFFER) {
        try (SeekableByteChannel c = Files.newByteChannel(f, StandardOpenOption.READ)) {
            ByteBuffer buffer = ByteBuffer.allocateDirect(len);
            try {
                long res = c.read(buffer);
                if (res != len) {
                    throw new IOException("not all file " + f.toAbsolutePath() + " was read with NIO len=" + len + " writeen=" + res);
                }
                buffer.flip();
                byte[] result = new byte[len];
                buffer.get(result);
                return result;
            } finally {
                forceReleaseBuffer(buffer);
            }
        }
    } else {
        byte[] result = new byte[len];
        try (RandomAccessFile raf = new RandomAccessFile(f.toFile(), "r")) {
            long res = raf.read(result, 0, len);
            if (res != len) {
                throw new IOException("not all file " + f.toAbsolutePath() + " was read with NIO len=" + len + " read=" + res);
            }
        }
        return result;
    }
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) RandomAccessFile(java.io.RandomAccessFile) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 24 with SeekableByteChannel

use of java.nio.channels.SeekableByteChannel in project graal by oracle.

the class FileSystemProviderTest method testNewByteChannel.

@Test
public void testNewByteChannel() throws IOException {
    try (SeekableByteChannel ch = fs.newByteChannel(existingAbsolute, EnumSet.of(StandardOpenOption.READ))) {
        final ByteBuffer buffer = ByteBuffer.allocate(1024);
        ch.read(buffer);
        buffer.flip();
        Assert.assertEquals(getClass().getSimpleName(), StandardCharsets.UTF_8.decode(buffer).toString());
    }
    try (SeekableByteChannel ch = fs.newByteChannel(existingRelative, EnumSet.of(StandardOpenOption.READ))) {
        final ByteBuffer buffer = ByteBuffer.allocate(1024);
        ch.read(buffer);
        buffer.flip();
        Assert.assertEquals(getClass().getSimpleName(), StandardCharsets.UTF_8.decode(buffer).toString());
    }
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 25 with SeekableByteChannel

use of java.nio.channels.SeekableByteChannel in project graal by oracle.

the class IOHelper method copy.

static void copy(final Path source, final Path target, final FileSystem sourceFileSystem, final FileSystem targetFileSystem, CopyOption... options) throws IOException {
    if (source.equals(target)) {
        return;
    }
    final Path sourceReal = sourceFileSystem.toRealPath(source, LinkOption.NOFOLLOW_LINKS);
    final Path targetReal = targetFileSystem.toRealPath(target, LinkOption.NOFOLLOW_LINKS);
    if (sourceReal.equals(targetReal)) {
        return;
    }
    final Set<LinkOption> linkOptions = new HashSet<>();
    final Set<StandardCopyOption> copyOptions = EnumSet.noneOf(StandardCopyOption.class);
    for (CopyOption option : options) {
        if (option instanceof StandardCopyOption) {
            copyOptions.add((StandardCopyOption) option);
        } else if (option instanceof LinkOption) {
            linkOptions.add((LinkOption) option);
        }
    }
    if (copyOptions.contains(StandardCopyOption.ATOMIC_MOVE)) {
        throw new AtomicMoveNotSupportedException(source.getFileName().toString(), target.getFileName().toString(), "Atomic move not supported");
    }
    final Map<String, Object> sourceAttributes = sourceFileSystem.readAttributes(sourceReal, "basic:isSymbolicLink,isDirectory,lastModifiedTime,lastAccessTime,creationTime", linkOptions.toArray(new LinkOption[linkOptions.size()]));
    if ((Boolean) sourceAttributes.getOrDefault("isSymbolicLink", false)) {
        throw new IOException("Copying of symbolic links is not supported.");
    }
    if (copyOptions.contains(StandardCopyOption.REPLACE_EXISTING)) {
        try {
            targetFileSystem.delete(targetReal);
        } catch (NoSuchFileException notFound) {
        // Does not exist - nothing to delete
        }
    } else {
        boolean exists;
        try {
            targetFileSystem.checkAccess(targetReal, EnumSet.noneOf(AccessMode.class));
            exists = true;
        } catch (IOException ioe) {
            exists = false;
        }
        if (exists) {
            throw new FileAlreadyExistsException(target.toString());
        }
    }
    if ((Boolean) sourceAttributes.getOrDefault("isDirectory", false)) {
        targetFileSystem.createDirectory(targetReal);
    } else {
        final Set<StandardOpenOption> readOptions = EnumSet.of(StandardOpenOption.READ);
        final Set<StandardOpenOption> writeOptions = EnumSet.of(StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW);
        try (final SeekableByteChannel sourceChannel = sourceFileSystem.newByteChannel(sourceReal, readOptions);
            final SeekableByteChannel targetChannel = targetFileSystem.newByteChannel(targetReal, writeOptions)) {
            final ByteBuffer buffer = ByteBuffer.allocateDirect(1 << 16);
            while (sourceChannel.read(buffer) != -1) {
                buffer.flip();
                while (buffer.hasRemaining()) {
                    targetChannel.write(buffer);
                }
                buffer.clear();
            }
        }
    }
    if (copyOptions.contains(StandardCopyOption.COPY_ATTRIBUTES)) {
        String[] basicMutableAttributes = { "lastModifiedTime", "lastAccessTime", "creationTime" };
        try {
            for (String key : basicMutableAttributes) {
                final Object value = sourceAttributes.get(key);
                if (value != null) {
                    targetFileSystem.setAttribute(targetReal, key, value);
                }
            }
        } catch (Throwable rootCause) {
            try {
                targetFileSystem.delete(targetReal);
            } catch (Throwable suppressed) {
                rootCause.addSuppressed(suppressed);
            }
            throw rootCause;
        }
    }
}
Also used : Path(java.nio.file.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) StandardCopyOption(java.nio.file.StandardCopyOption) CopyOption(java.nio.file.CopyOption) LinkOption(java.nio.file.LinkOption) StandardOpenOption(java.nio.file.StandardOpenOption) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) SeekableByteChannel(java.nio.channels.SeekableByteChannel) StandardCopyOption(java.nio.file.StandardCopyOption) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException) AccessMode(java.nio.file.AccessMode) HashSet(java.util.HashSet)

Aggregations

SeekableByteChannel (java.nio.channels.SeekableByteChannel)130 ByteBuffer (java.nio.ByteBuffer)58 Path (java.nio.file.Path)48 IOException (java.io.IOException)42 Test (org.junit.Test)33 InputStream (java.io.InputStream)14 NoSuchFileException (java.nio.file.NoSuchFileException)12 Test (org.testng.annotations.Test)9 ReadableByteChannel (java.nio.channels.ReadableByteChannel)8 OpenOption (java.nio.file.OpenOption)7 StandardOpenOption (java.nio.file.StandardOpenOption)7 HashSet (java.util.HashSet)7 File (java.io.File)6 FileSystem (java.nio.file.FileSystem)6 CloudStorageFileSystem (com.google.cloud.storage.contrib.nio.CloudStorageFileSystem)5 URI (java.net.URI)5 RandomAccessData (org.apache.beam.runners.dataflow.util.RandomAccessData)5 OutputStream (java.io.OutputStream)4 FileChannel (java.nio.channels.FileChannel)4 WritableByteChannel (java.nio.channels.WritableByteChannel)4