if (options.getPerms() != null) { FileAttribute<?> attrs = PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString(options.getPerms())); ch = AsynchronousFileChannel.open(file, opts, vertx.getWorkerPool(), attrs); } else { ch = AsynchronousFileChannel.open(file, opts, vertx.getWorkerPool()); if (options.isAppend()) writePos = ch.size(); } catch (IOException e) { throw new FileSystemException(e);
buff.limit(readSize); ch.read(buff, position, null, new java.nio.channels.CompletionHandler<Integer, Object>() {
private void writeInternal(ByteBuffer buff, long position, Handler<AsyncResult<Void>> handler) { ch.write(buff, position, null, new java.nio.channels.CompletionHandler<Integer, Object>() { public void completed(Integer bytesWritten, Object attachment) { long pos = position; if (buff.hasRemaining()) { // partial write pos += bytesWritten; // resubmit writeInternal(buff, pos, handler); } else { // It's been fully written context.runOnContext((v) -> { synchronized (AsyncFileImpl.this) { writesOutstanding -= buff.limit(); } handler.handle(Future.succeededFuture()); }); } } public void failed(Throwable exc, Object attachment) { if (exc instanceof Exception) { context.runOnContext((v) -> handler.handle(Future.failedFuture(exc))); } else { log.error("Error occurred", exc); } } }); }
@Test public void writeAsynchronousFileChannel() throws Exception { DataBuffer foo = stringBuffer("foo"); DataBuffer bar = stringBuffer("bar"); DataBuffer baz = stringBuffer("baz"); DataBuffer qux = stringBuffer("qux"); Flux<DataBuffer> flux = Flux.just(foo, bar, baz, qux); AsynchronousFileChannel channel = AsynchronousFileChannel.open(tempFile, StandardOpenOption.WRITE); Flux<DataBuffer> writeResult = DataBufferUtils.write(flux, channel); verifyWrittenData(writeResult); channel.close(); }
/** * Creates I/O implementation for specified {@code file} * @param file Random access file * @param modes Open modes. */ public AsyncFileIO(File file, ThreadLocal<ChannelOpFuture> holder, OpenOption... modes) throws IOException { ch = AsynchronousFileChannel.open(file.toPath(), modes); fd = getFileDescriptor(ch); fsBlockSize = FileSystemUtils.getFileSystemBlockSize(fd); this.holder = holder; }
public AsynchronousFileChannel getAsyncChannel() { if (asyncChannel != null) { if (asyncChannel.isOpen()) return asyncChannel; } synchronized (this) { if (asyncChannel != null) { // check again while synchronized if (asyncChannel.isOpen()) return asyncChannel; } try { asyncChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ); } catch (IOException e) { throw new RuntimeException(e); } asyncChannelOpenCount++; return asyncChannel; } }
final AsynchronousFileChannel channel = AsynchronousFileChannel.open(Paths.get(dirAbsolutePath, queEntry.fileName), StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); channel.write(ByteBuffer.wrap(queEntry.dataBytes), 0, null, new CompletionHandler<Integer, Object>() { @Override public void completed(Integer result, Object attachment) {
private void done() throws IOException { if (fileChannel != null) { if (!contentStream.isEmpty()) { ByteBuffer bb = ByteBuffer.allocate(contentStream.length()); bb.put(contentStream.toByteArray()); bb.flip(); contentStream.clear(); fileChannel.write(bb, fileChannel.size()); fileChannel.close(); } } }
private void doClose(Handler<AsyncResult<Void>> handler) { ContextInternal handlerContext = vertx.getOrCreateContext(); handlerContext.executeBlockingInternal(res -> { try { ch.close(); res.complete(null); } catch (IOException e) { res.fail(e); } }, handler); }
@Override public boolean configure(Properties properties) { try { destination = AsynchronousFileChannel.open(Paths.get(fileName), StandardOpenOption.WRITE, StandardOpenOption.CREATE); // Failed to used StandardOpenOption.(APPEND vs CREATE), so truncate if need after creation if (truncate) { destination.truncate(0); } position = new AtomicLong(destination.size()); } catch (IOException | UnsupportedOperationException e) { logger.error("error openening output file {}: {}", fileName, Helpers.resolveThrowableException(e)); logger.catching(Level.DEBUG, e); return false; } return super.configure(properties); }
if (!fileChannel.isOpen()) { log.debug("[{}] File is not open, not performing more reads.", path); fileInput.setReaderFinished(this); final long size = fileChannel.size(); throw e; final Future<Integer> read = fileChannel.read(byteBuffer, position); final Integer bytesRead = read.get(); log.trace("[{}] Read {} bytes from position {}", path, bytesRead, position);
@Override public void stopSending() { if (destination.isOpen()) { try { // The lock will not be released, as you don't release a closed file destination.lock(); destination.force(true); destination.close(); } catch (IOException e) { logger.error("Failed to close {}: {}", fileName, Helpers.resolveThrowableException(e)); logger.catching(Level.DEBUG, e); } } super.stopSending(); }
/** {@inheritDoc} */ @Override public long size() throws IOException { return ch.size(); }
private void closeForReal() { _log.debug("closing file " + file.getAbsolutePath()); try { ch.force(true); } catch (Exception x) { _log.error("unable to force update file " + file.getAbsolutePath(), x); } try { ch.close(); } catch (Exception x) { _log.warn("unable to close file " + file.getAbsolutePath(), x); } }
/** * Returns if channel is open. * * @return Returns if channel is open. */ public boolean isOpened() { return (fileChannel != null) && fileChannel.isOpen(); }