Example usage for java.nio.channels AsynchronousFileChannel lock

List of usage examples for java.nio.channels AsynchronousFileChannel lock

Introduction

In this page you can find the example usage for java.nio.channels AsynchronousFileChannel lock.

Prototype

public final <A> void lock(A attachment, CompletionHandler<FileLock, ? super A> handler) 

Source Link

Document

Acquires an exclusive lock on this channel's file.

Usage

From source file:ubicrypt.core.Utils.java

public static Observable<Long> write(final Path fullPath, final InputStream inputStream) {
    return Observable.create(subscriber -> {
        try {/*  www  . ja  va2  s  .  c o m*/
            final AtomicLong offset = new AtomicLong(0);
            final AsynchronousFileChannel afc = AsynchronousFileChannel.open(fullPath, StandardOpenOption.WRITE,
                    StandardOpenOption.CREATE);
            afc.lock(new Object(), new CompletionHandler<FileLock, Object>() {
                @Override
                public void completed(final FileLock lock, final Object attachment) {
                    //acquired lock
                    final byte[] buf = new byte[1 << 16];
                    try {
                        final int len = inputStream.read(buf);
                        if (len == -1) {
                            unsubscribe(subscriber, inputStream, lock);
                            return;
                        }
                        afc.write(ByteBuffer.wrap(Arrays.copyOfRange(buf, 0, len)), offset.get(), null,
                                new CompletionHandler<Integer, Object>() {
                                    @Override
                                    public void completed(final Integer result, final Object attachment) {
                                        //written chunk of bytes
                                        subscriber.onNext(offset.addAndGet(result));
                                        final byte[] buf = new byte[1 << 16];
                                        int len;
                                        try {
                                            len = inputStream.read(buf);
                                            if (len == -1) {
                                                unsubscribe(subscriber, inputStream, lock);
                                                log.debug("written:{}", fullPath);
                                                return;
                                            }
                                        } catch (final IOException e) {
                                            subscriber.onError(e);
                                            return;
                                        }
                                        if (len == -1) {
                                            unsubscribe(subscriber, inputStream, lock);
                                            log.debug("written:{}", fullPath);
                                            return;
                                        }
                                        afc.write(ByteBuffer.wrap(Arrays.copyOfRange(buf, 0, len)),
                                                offset.get(), null, this);
                                    }

                                    @Override
                                    public void failed(final Throwable exc, final Object attachment) {
                                        subscriber.onError(exc);
                                    }
                                });
                    } catch (final Exception e) {
                        close(inputStream, lock);
                        subscriber.onError(e);
                    }
                }

                @Override
                public void failed(final Throwable exc, final Object attachment) {
                    log.error("error on getting lock for:{}, error:{}", fullPath, exc.getMessage());
                    try {
                        inputStream.close();
                    } catch (final IOException e) {
                    }
                    subscriber.onError(exc);
                }
            });

        } catch (final Exception e) {
            log.error("error on file:{}", fullPath);
            subscriber.onError(e);
        }
    });
}