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) {
}
}
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);
}
}
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();
}
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();
}
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;
}
Aggregations