Search in sources :

Example 6 with AsynchronousFileChannel

use of java.nio.channels.AsynchronousFileChannel in project jimfs by google.

the class JimfsFileSystemCloseTest method testOpenChannelsClosed.

@Test
public void testOpenChannelsClosed() throws IOException {
    Path p = fs.getPath("/foo");
    FileChannel fc = FileChannel.open(p, READ, WRITE, CREATE);
    SeekableByteChannel sbc = Files.newByteChannel(p, READ);
    AsynchronousFileChannel afc = AsynchronousFileChannel.open(p, READ, WRITE);
    assertTrue(fc.isOpen());
    assertTrue(sbc.isOpen());
    assertTrue(afc.isOpen());
    fs.close();
    assertFalse(fc.isOpen());
    assertFalse(sbc.isOpen());
    assertFalse(afc.isOpen());
    try {
        fc.size();
        fail();
    } catch (ClosedChannelException expected) {
    }
    try {
        sbc.size();
        fail();
    } catch (ClosedChannelException expected) {
    }
    try {
        afc.size();
        fail();
    } catch (ClosedChannelException expected) {
    }
}
Also used : Path(java.nio.file.Path) SeekableByteChannel(java.nio.channels.SeekableByteChannel) AsynchronousFileChannel(java.nio.channels.AsynchronousFileChannel) ClosedChannelException(java.nio.channels.ClosedChannelException) AsynchronousFileChannel(java.nio.channels.AsynchronousFileChannel) FileChannel(java.nio.channels.FileChannel) Test(org.junit.Test)

Example 7 with AsynchronousFileChannel

use of java.nio.channels.AsynchronousFileChannel in project spring-framework by spring-projects.

the class ResourceEncoder method encode.

@Override
protected Flux<DataBuffer> encode(Resource resource, DataBufferFactory dataBufferFactory, ResolvableType type, MimeType mimeType, Map<String, Object> hints) {
    try {
        if (resource.isFile()) {
            File file = resource.getFile();
            AsynchronousFileChannel channel = AsynchronousFileChannel.open(file.toPath(), StandardOpenOption.READ);
            return DataBufferUtils.read(channel, dataBufferFactory, this.bufferSize);
        }
    } catch (IOException ignore) {
    // fallback to resource.readableChannel(), below
    }
    try {
        ReadableByteChannel channel = resource.readableChannel();
        return DataBufferUtils.read(channel, dataBufferFactory, this.bufferSize);
    } catch (IOException ex) {
        return Flux.error(ex);
    }
}
Also used : AsynchronousFileChannel(java.nio.channels.AsynchronousFileChannel) ReadableByteChannel(java.nio.channels.ReadableByteChannel) IOException(java.io.IOException) File(java.io.File)

Example 8 with AsynchronousFileChannel

use of java.nio.channels.AsynchronousFileChannel in project spring-framework by spring-projects.

the class DataBufferUtilsTests method readAsynchronousFileChannel.

@Test
public void readAsynchronousFileChannel() throws Exception {
    URI uri = DataBufferUtilsTests.class.getResource("DataBufferUtilsTests.txt").toURI();
    AsynchronousFileChannel channel = AsynchronousFileChannel.open(Paths.get(uri), StandardOpenOption.READ);
    Flux<DataBuffer> flux = DataBufferUtils.read(channel, this.bufferFactory, 3);
    StepVerifier.create(flux).consumeNextWith(stringConsumer("foo")).consumeNextWith(stringConsumer("bar")).consumeNextWith(stringConsumer("baz")).consumeNextWith(stringConsumer("qux")).expectComplete().verify();
}
Also used : AsynchronousFileChannel(java.nio.channels.AsynchronousFileChannel) URI(java.net.URI) Test(org.junit.Test)

Example 9 with AsynchronousFileChannel

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

the class TestLeakFS method testLeakAsyncFileChannel.

/** Test leaks via AsynchronousFileChannel.open */
public void testLeakAsyncFileChannel() throws IOException {
    Path dir = wrap(createTempDir());
    OutputStream file = Files.newOutputStream(dir.resolve("stillopen"));
    file.write(5);
    file.close();
    AsynchronousFileChannel leak = AsynchronousFileChannel.open(dir.resolve("stillopen"));
    try {
        dir.getFileSystem().close();
        fail("should have gotten exception");
    } catch (Exception e) {
        assertTrue(e.getMessage().contains("file handle leaks"));
    }
    leak.close();
}
Also used : Path(java.nio.file.Path) AsynchronousFileChannel(java.nio.channels.AsynchronousFileChannel) OutputStream(java.io.OutputStream) IOException(java.io.IOException)

Example 10 with AsynchronousFileChannel

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

the class HandleTrackingFS method newAsynchronousFileChannel.

@Override
public AsynchronousFileChannel newAsynchronousFileChannel(Path path, Set<? extends OpenOption> options, ExecutorService executor, FileAttribute<?>... attrs) throws IOException {
    AsynchronousFileChannel channel = new FilterAsynchronousFileChannel(super.newAsynchronousFileChannel(path, options, executor, 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 "AsynchronousFileChannel(" + 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 : AsynchronousFileChannel(java.nio.channels.AsynchronousFileChannel)

Aggregations

AsynchronousFileChannel (java.nio.channels.AsynchronousFileChannel)11 IOException (java.io.IOException)7 Path (java.nio.file.Path)4 File (java.io.File)3 Test (org.junit.Test)3 URI (java.net.URI)2 ByteBuffer (java.nio.ByteBuffer)2 ReadableByteChannel (java.nio.channels.ReadableByteChannel)2 ExecutorService (java.util.concurrent.ExecutorService)2 Suspendable (co.paralleluniverse.fibers.Suspendable)1 Http2Response (com.webpieces.hpack.api.dto.Http2Response)1 StreamWriter (com.webpieces.http2engine.api.StreamWriter)1 DataFrame (com.webpieces.http2parser.api.dto.DataFrame)1 StatusCode (com.webpieces.http2parser.api.dto.StatusCode)1 Http2Header (com.webpieces.http2parser.api.dto.lib.Http2Header)1 Http2HeaderName (com.webpieces.http2parser.api.dto.lib.Http2HeaderName)1 OutputStream (java.io.OutputStream)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 CompletionHandler (java.nio.channels.CompletionHandler)1 FileChannel (java.nio.channels.FileChannel)1