Search in sources :

Example 86 with SeekableByteChannel

use of java.nio.channels.SeekableByteChannel in project lucene-solr by apache.

the class HandleTrackingFS method newByteChannel.

@Override
public SeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException {
    SeekableByteChannel channel = new FilterSeekableByteChannel(super.newByteChannel(path, options, attrs)) {

        boolean closed;

        @Override
        public void close() throws IOException {
            try {
                if (!closed) {
                    closed = true;
                    onClose(path, this);
                }
            } finally {
                super.close();
            }
        }

        @Override
        public String toString() {
            return "SeekableByteChannel(" + path.toString() + ")";
        }

        @Override
        public int hashCode() {
            return System.identityHashCode(this);
        }

        @Override
        public boolean equals(Object obj) {
            return this == obj;
        }
    };
    callOpenHook(path, channel);
    return channel;
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel)

Example 87 with SeekableByteChannel

use of java.nio.channels.SeekableByteChannel in project lucene-solr by apache.

the class TestHandleTrackingFS method testOnOpenThrowsException.

/** Test that the delegate gets closed on exception in HandleTrackingFS#onOpen */
public void testOnOpenThrowsException() throws IOException {
    // we are using LeakFS under the hood if we don't get closed the test fails
    Path path = wrap(createTempDir());
    FileSystem fs = new HandleTrackingFS("test://", path.getFileSystem()) {

        @Override
        protected void onClose(Path path, Object stream) throws IOException {
        }

        @Override
        protected void onOpen(Path path, Object stream) throws IOException {
            throw new IOException("boom");
        }
    }.getFileSystem(URI.create("file:///"));
    Path dir = new FilterPath(path, fs);
    try {
        OutputStream file = Files.newOutputStream(dir.resolve("somefile"));
        fail("expected IOException");
    } catch (IOException ex) {
    // expected
    }
    try {
        SeekableByteChannel channel = Files.newByteChannel(dir.resolve("somefile"));
        fail("expected IOException");
    } catch (IOException ex) {
    // expected
    }
    try {
        InputStream stream = Files.newInputStream(dir.resolve("somefile"));
        fail("expected IOException");
    } catch (IOException ex) {
    // expected
    }
    fs.close();
    try {
        DirectoryStream<Path> dirStream = Files.newDirectoryStream(dir);
        fail("expected IOException");
    } catch (IOException ex) {
    // expected
    }
    fs.close();
}
Also used : Path(java.nio.file.Path) SeekableByteChannel(java.nio.channels.SeekableByteChannel) InputStream(java.io.InputStream) FileSystem(java.nio.file.FileSystem) OutputStream(java.io.OutputStream) Object(java.lang.Object) IOException(java.io.IOException) HandleTrackingFS(org.apache.lucene.mockfile.HandleTrackingFS) Override(java.lang.Override)

Example 88 with SeekableByteChannel

use of java.nio.channels.SeekableByteChannel in project lucene-solr by apache.

the class TestHandleTrackingFS method testOnCloseThrowsException.

/** Test that the delegate gets closed on exception in HandleTrackingFS#onClose */
public void testOnCloseThrowsException() throws IOException {
    // we are using LeakFS under the hood if we don't get closed the test fails
    Path path = wrap(createTempDir());
    FileSystem fs = new HandleTrackingFS("test://", path.getFileSystem()) {

        @Override
        protected void onClose(Path path, Object stream) throws IOException {
            throw new IOException("boom");
        }

        @Override
        protected void onOpen(Path path, Object stream) throws IOException {
        //
        }
    }.getFileSystem(URI.create("file:///"));
    Path dir = new FilterPath(path, fs);
    OutputStream file = Files.newOutputStream(dir.resolve("somefile"));
    file.write(5);
    try {
        file.close();
        fail("expected IOException");
    } catch (IOException ex) {
    // expected
    }
    SeekableByteChannel channel = Files.newByteChannel(dir.resolve("somefile"));
    try {
        channel.close();
        fail("expected IOException");
    } catch (IOException ex) {
    // expected
    }
    InputStream stream = Files.newInputStream(dir.resolve("somefile"));
    try {
        stream.close();
        fail("expected IOException");
    } catch (IOException ex) {
    // expected
    }
    fs.close();
    DirectoryStream<Path> dirStream = Files.newDirectoryStream(dir);
    try {
        dirStream.close();
        fail("expected IOException");
    } catch (IOException ex) {
    // expected
    }
}
Also used : Path(java.nio.file.Path) SeekableByteChannel(java.nio.channels.SeekableByteChannel) InputStream(java.io.InputStream) FileSystem(java.nio.file.FileSystem) OutputStream(java.io.OutputStream) Object(java.lang.Object) IOException(java.io.IOException) HandleTrackingFS(org.apache.lucene.mockfile.HandleTrackingFS) Override(java.lang.Override)

Example 89 with SeekableByteChannel

use of java.nio.channels.SeekableByteChannel in project jdk8u_jdk by JetBrains.

the class ReplayCachePrecise method main.

public static void main(String[] args) throws Exception {
    AuthTimeWithHash a1 = new AuthTimeWithHash(client, server, time(0), 0, "1111111111111111");
    AuthTimeWithHash a2 = new AuthTimeWithHash(client, server, time(0), 0, "2222222222222222");
    KerberosTime now = new KerberosTime(time(0) * 1000L);
    // When all new styles, must exact match
    ReplayCache cache = ReplayCache.getInstance("dfl:./c1");
    cache.checkAndStore(now, a1);
    cache.checkAndStore(now, a2);
    // When only old style in cache, partial match
    cache = ReplayCache.getInstance("dfl:./c2");
    cache.checkAndStore(now, a1);
    // A small surgery to remove the new style from the cache file
    SeekableByteChannel ch = Files.newByteChannel(Paths.get("c2"), StandardOpenOption.WRITE, StandardOpenOption.READ);
    ch.position(6);
    ch.write(ByteBuffer.wrap(a1.encode(false)));
    ch.truncate(ch.position());
    ch.close();
    try {
        cache.checkAndStore(now, a2);
        throw new Exception();
    } catch (KrbException ke) {
        // Correct
        System.out.println(ke);
    }
}
Also used : AuthTimeWithHash(sun.security.krb5.internal.rcache.AuthTimeWithHash) SeekableByteChannel(java.nio.channels.SeekableByteChannel) ReplayCache(sun.security.krb5.internal.ReplayCache) KrbException(sun.security.krb5.KrbException) KerberosTime(sun.security.krb5.internal.KerberosTime) KrbException(sun.security.krb5.KrbException)

Example 90 with SeekableByteChannel

use of java.nio.channels.SeekableByteChannel in project jdk8u_jdk by JetBrains.

the class ReplayCacheTestProc method csize.

// return size of dfl file, excluding the null hash ones
private static int csize(int p) throws Exception {
    try (SeekableByteChannel chan = Files.newByteChannel(Paths.get(dfl(p)), StandardOpenOption.READ)) {
        chan.position(6);
        int cc = 0;
        while (true) {
            try {
                if (AuthTime.readFrom(chan) != null)
                    cc++;
            } catch (BufferUnderflowException e) {
                break;
            }
        }
        return cc;
    } catch (IOException ioe) {
        return 0;
    }
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) BufferUnderflowException(java.nio.BufferUnderflowException)

Aggregations

SeekableByteChannel (java.nio.channels.SeekableByteChannel)151 ByteBuffer (java.nio.ByteBuffer)72 Path (java.nio.file.Path)56 IOException (java.io.IOException)52 Test (org.junit.Test)41 InputStream (java.io.InputStream)17 ReadableByteChannel (java.nio.channels.ReadableByteChannel)13 NoSuchFileException (java.nio.file.NoSuchFileException)13 FileSystem (java.nio.file.FileSystem)12 StandardOpenOption (java.nio.file.StandardOpenOption)11 Test (org.testng.annotations.Test)9 WritableByteChannel (java.nio.channels.WritableByteChannel)8 OpenOption (java.nio.file.OpenOption)8 HashSet (java.util.HashSet)8 OutputStream (java.io.OutputStream)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 File (java.io.File)6 CloudStorageFileSystem (com.google.cloud.storage.contrib.nio.CloudStorageFileSystem)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 URI (java.net.URI)5