Search in sources :

Example 6 with SeekableByteChannel

use of java.nio.channels.SeekableByteChannel in project helios by spotify.

the class X509CertificateFactory method saveToCache.

private static void saveToCache(final Path cacheDirectory, final Path cacheCertPath, final Path cacheKeyPath, final CertificateAndPrivateKey certificateAndPrivateKey) {
    try {
        Files.createDirectories(cacheDirectory);
        final String certPem = asPemString(certificateAndPrivateKey.getCertificate());
        final String keyPem = asPemString(certificateAndPrivateKey.getPrivateKey());
        // overwrite any existing file, and make sure it's only readable by the current user
        final Set<StandardOpenOption> options = ImmutableSet.of(CREATE, WRITE);
        final Set<PosixFilePermission> perms = ImmutableSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE);
        final FileAttribute<Set<PosixFilePermission>> attrs = PosixFilePermissions.asFileAttribute(perms);
        try (final SeekableByteChannel sbc = Files.newByteChannel(cacheCertPath, options, attrs)) {
            sbc.write(ByteBuffer.wrap(certPem.getBytes()));
        }
        try (final SeekableByteChannel sbc = Files.newByteChannel(cacheKeyPath, options, attrs)) {
            sbc.write(ByteBuffer.wrap(keyPem.getBytes()));
        }
        log.debug("cached generated certificate to {}", cacheCertPath);
    } catch (IOException e) {
        // couldn't save to the cache, oh well
        log.warn("error caching generated certificate", e);
    }
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) StandardOpenOption(java.nio.file.StandardOpenOption) IOException(java.io.IOException) PosixFilePermission(java.nio.file.attribute.PosixFilePermission)

Example 7 with SeekableByteChannel

use of java.nio.channels.SeekableByteChannel in project bazel by bazelbuild.

the class ZipFiles method unixExternalFileAttributes.

/**
   * Returns the external file attributes of each entry as a mapping from the entry name to the
   * 32-bit value. As long as the attributes are generated by a Unix host, this includes the POSIX
   * file permissions in the upper two bytes. Entries not generated by a Unix host are not included
   * in the result.
   */
public static Map<String, Integer> unixExternalFileAttributes(Path zipFile) throws IOException {
    // Field descriptions in comments were taken from this document:
    // http://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
    ImmutableMap.Builder<String, Integer> attributes = new ImmutableMap.Builder<>();
    try (SeekableByteChannel input = Files.newByteChannel(zipFile)) {
        // The data we care about is toward the end of the file, after the compressed data for each
        // file. We begin by looking for the start of the end of central directory record, which is
        // marked by the signature 0x06054b50
        //
        // This contains the centralDirectoryStartOffset value, which tells us where to seek to find
        // the first central directory entry. Each such entry is marked by the signature 0x02014b50
        // and appear in sequence, one entry for each file in the .zip.
        //
        // The central directory entry contains many values, including the file name, the external
        // file attributes, and the version made by value. If the version made by indicates a Unix
        // host (0x03??), we include the external file attributes in the returned map.
        long offset = input.size() - 4;
        while (offset >= 0) {
            input.position(offset);
            int signature = readBytes(4, input);
            if (signature == 0x06054b50) {
                break;
            } else if (signature == 0x06064b50) {
                throw new IOException("Zip64 format not supported: " + zipFile);
            }
            offset--;
        }
        if (offset < 0) {
            throw new IOException();
        }
        // Read end of central directory structure
        input.position(input.position() + // number of this disk
        2 + // number of the disk with the start of the central directory
        2);
        int entryCount = readBytes(2, input);
        input.position(input.position() + // total number of entries in the central directory
        2);
        input.position(input.position() + // size of the central directory
        4);
        int centralDirectoryStartOffset = readBytes(4, input);
        if (0xffffffff == centralDirectoryStartOffset) {
            throw new IOException("Zip64 format not supported.");
        }
        input.position(centralDirectoryStartOffset);
        int entriesFound = 0;
        // Read each central directory entry
        while ((entriesFound < entryCount) && (readBytes(4, input) == 0x02014b50)) {
            int versionMadeBy = readBytes(2, input);
            input.position(input.position() + // version needed to extract
            2 + // general purpose bit flag
            2 + // compression method
            2 + // last mod file time
            2 + // last mod file date
            2 + // crc-32
            4 + // compressed size
            4 + // uncompressed size
            4);
            int filenameLength = readBytes(2, input);
            int extraFieldLength = readBytes(2, input);
            int fileCommentLength = readBytes(2, input);
            input.position(input.position() + // disk number start
            2 + // internal file attributes
            2);
            int externalFileAttributes = readBytes(4, input);
            input.position(input.position() + // relative offset of local header
            4);
            ByteBuffer filenameBuffer = ByteBuffer.allocate(filenameLength);
            if (filenameLength != input.read(filenameBuffer)) {
                throw new IOException(String.format("Could not read file name (length %d) in central directory record", filenameLength));
            }
            input.position(input.position() + extraFieldLength + fileCommentLength);
            entriesFound++;
            if ((versionMadeBy >> 8) == 3) {
                // Zip made by a Unix host - the external file attributes are POSIX permissions.
                String filename = new String(filenameBuffer.array(), StandardCharsets.UTF_8);
                attributes.put(filename, externalFileAttributes);
            }
        }
        if (entriesFound != entryCount) {
            System.err.printf("WARNING: Expected %d entries in central directory record in '%s', but found %d\n", entryCount, zipFile, entriesFound);
        }
    }
    return attributes.build();
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 8 with SeekableByteChannel

use of java.nio.channels.SeekableByteChannel in project ratpack by ratpack.

the class IoUtils method read.

public static ByteBuf read(ByteBufAllocator allocator, Path path) throws IOException {
    try (SeekableByteChannel sbc = Files.newByteChannel(path);
        InputStream in = Channels.newInputStream(sbc)) {
        int size = Ints.checkedCast(sbc.size());
        ByteBuf byteBuf = allocator.directBuffer(size, size);
        byteBuf.writeBytes(in, size);
        return byteBuf;
    }
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) InputStream(java.io.InputStream) ByteBuf(io.netty.buffer.ByteBuf)

Example 9 with SeekableByteChannel

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

the class ReadsDataSourceUnitTest method testManuallySpecifiedIndicesWithCustomReaderFactoryAndNullWrappers.

@Test(dataProvider = "manuallySpecifiedIndexTestData")
public void testManuallySpecifiedIndicesWithCustomReaderFactoryAndNullWrappers(final List<Path> bams, final List<Path> indices) {
    final SamReaderFactory customFactory = SamReaderFactory.makeDefault().validationStringency(ValidationStringency.STRICT);
    // ReadsDataSource should not be using the wrapper since the files are not on the Google cloud.
    // So we pass this invalid wrapper: if the code tries to use it, it'll blow up.
    Function<SeekableByteChannel, SeekableByteChannel> nullWrapper = (SeekableByteChannel) -> null;
    try (final ReadsDataSource readsSource = new ReadsDataSource(bams, indices, customFactory, nullWrapper, nullWrapper)) {
        Assert.assertTrue(readsSource.indicesAvailable(), "Explicitly-provided indices not detected for bams: " + bams);
        final Iterator<GATKRead> queryReads = readsSource.query(new SimpleInterval("1", 1, 300));
        int queryCount = 0;
        while (queryReads.hasNext()) {
            ++queryCount;
            queryReads.next();
        }
        Assert.assertEquals(queryCount, 5, "Wrong number of reads returned in query");
    }
}
Also used : java.util(java.util) DataProvider(org.testng.annotations.DataProvider) IOUtils(org.broadinstitute.hellbender.utils.io.IOUtils) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) XorWrapper(org.broadinstitute.hellbender.utils.test.XorWrapper) SAMRecordToGATKReadAdapter(org.broadinstitute.hellbender.utils.read.SAMRecordToGATKReadAdapter) Test(org.testng.annotations.Test) GATKRead(org.broadinstitute.hellbender.utils.read.GATKRead) Function(java.util.function.Function) SimpleInterval(org.broadinstitute.hellbender.utils.SimpleInterval) File(java.io.File) ReadUtils(org.broadinstitute.hellbender.utils.read.ReadUtils) SeekableByteChannel(java.nio.channels.SeekableByteChannel) UserException(org.broadinstitute.hellbender.exceptions.UserException) Assert(org.testng.Assert) SAMFileGATKReadWriter(org.broadinstitute.hellbender.utils.read.SAMFileGATKReadWriter) htsjdk.samtools(htsjdk.samtools) Path(java.nio.file.Path) SeekableByteChannel(java.nio.channels.SeekableByteChannel) GATKRead(org.broadinstitute.hellbender.utils.read.GATKRead) SimpleInterval(org.broadinstitute.hellbender.utils.SimpleInterval) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Example 10 with SeekableByteChannel

use of java.nio.channels.SeekableByteChannel in project DataflowJavaSDK-examples by GoogleCloudPlatform.

the class MinimalWordCountJava8Test method buildMockGcsUtil.

private GcsUtil buildMockGcsUtil() throws IOException {
    GcsUtil mockGcsUtil = Mockito.mock(GcsUtil.class);
    // Any request to open gets a new bogus channel
    Mockito.when(mockGcsUtil.open(Mockito.any(GcsPath.class))).then(new Answer<SeekableByteChannel>() {

        @Override
        public SeekableByteChannel answer(InvocationOnMock invocation) throws Throwable {
            return FileChannel.open(Files.createTempFile("channel-", ".tmp"), StandardOpenOption.CREATE, StandardOpenOption.DELETE_ON_CLOSE);
        }
    });
    // Any request for expansion returns a list containing the original GcsPath
    // This is required to pass validation that occurs in TextIO during apply()
    Mockito.when(mockGcsUtil.expand(Mockito.any(GcsPath.class))).then(new Answer<List<GcsPath>>() {

        @Override
        public List<GcsPath> answer(InvocationOnMock invocation) throws Throwable {
            return ImmutableList.of((GcsPath) invocation.getArguments()[0]);
        }
    });
    return mockGcsUtil;
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) InvocationOnMock(org.mockito.invocation.InvocationOnMock) GcsPath(org.apache.beam.sdk.util.gcsfs.GcsPath) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) GcsUtil(org.apache.beam.sdk.util.GcsUtil)

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