Search in sources :

Example 56 with SeekableByteChannel

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

the class SeekableByteChannelPrefetcherTest method testCloseWhilePrefetching.

@Test
public void testCloseWhilePrefetching() throws Exception {
    SeekableByteChannel chan = new SeekableByteChannelPrefetcher(Files.newByteChannel(Paths.get(input)), 10 * 1024 * 1024);
    // read just 1 byte, get the prefetching going
    ByteBuffer one = ByteBuffer.allocate(1);
    readFully(chan, one);
    // closing must not throw an exception, even if the prefetching
    // thread is active.
    chan.close();
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) ByteBuffer(java.nio.ByteBuffer) Test(org.testng.annotations.Test)

Example 57 with SeekableByteChannel

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

the class SeekableByteChannelPrefetcherTest method testRead.

@Test
public void testRead() throws Exception {
    SeekableByteChannel chan1 = Files.newByteChannel(Paths.get(input));
    SeekableByteChannel chan2 = new SeekableByteChannelPrefetcher(Files.newByteChannel(Paths.get(input)), 1024);
    testReading(chan1, chan2, 0);
    testReading(chan1, chan2, 128);
    testReading(chan1, chan2, 1024);
    testReading(chan1, chan2, 1500);
    testReading(chan1, chan2, 2048);
    testReading(chan1, chan2, 3000);
    testReading(chan1, chan2, 6000);
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) Test(org.testng.annotations.Test)

Example 58 with SeekableByteChannel

use of java.nio.channels.SeekableByteChannel in project jgnash by ccavanaugh.

the class FileMagic method isFile.

private static boolean isFile(final Path path, final byte[] header) {
    boolean result = false;
    if (Files.exists(path)) {
        try (final SeekableByteChannel channel = Files.newByteChannel(path, EnumSet.of(READ))) {
            if (channel.size() > 0) {
                // must not be a zero length file
                final ByteBuffer buff = ByteBuffer.allocate(header.length);
                channel.read(buff);
                result = Arrays.equals(buff.array(), header);
                buff.clear();
            }
        } catch (final IOException ex) {
            Logger.getLogger(FileMagic.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return result;
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Aggregations

SeekableByteChannel (java.nio.channels.SeekableByteChannel)58 Path (java.nio.file.Path)33 Test (org.junit.Test)20 ByteBuffer (java.nio.ByteBuffer)16 IOException (java.io.IOException)10 Test (org.testng.annotations.Test)9 File (java.io.File)6 CloudStorageFileSystem (com.google.cloud.storage.contrib.nio.CloudStorageFileSystem)5 GcsPath (org.apache.beam.sdk.util.gcsfs.GcsPath)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 ImmutableList (com.google.common.collect.ImmutableList)3 InputStream (java.io.InputStream)3 FileSystem (java.nio.file.FileSystem)3 java.util (java.util)3 Function (java.util.function.Function)3 CharStream (org.antlr.v4.runtime.CharStream)3 CodePointCharStream (org.antlr.v4.runtime.CodePointCharStream)3 UserException (org.broadinstitute.hellbender.exceptions.UserException)3 IOUtils (org.broadinstitute.hellbender.utils.io.IOUtils)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2