Example usage for java.nio.channels AsynchronousFileChannel open

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

Introduction

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

Prototype

public static AsynchronousFileChannel open(Path file, OpenOption... options) throws IOException 

Source Link

Document

Opens or creates a file for reading and/or writing, returning an asynchronous file channel to access the file.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Path path = Paths.get("test.txt");

    try (AsynchronousFileChannel afc = AsynchronousFileChannel.open(path, StandardOpenOption.READ)) {
        int fileSize = (int) afc.size();
        ByteBuffer dataBuffer = ByteBuffer.allocate(fileSize);

        Future<Integer> result = afc.read(dataBuffer, 0);
        int readBytes = result.get();

        System.out.format("%s bytes read   from  %s%n", readBytes, path);
        System.out.format("Read data is:%n");

        byte[] byteData = dataBuffer.array();
        Charset cs = Charset.forName("UTF-8");
        String data = new String(byteData, cs);

        System.out.println(data);
    } catch (IOException ex) {
        ex.printStackTrace();//from   w  ww. ja  v  a  2 s  . co m
    }
}

From source file:ubicrypt.core.Utils.java

public static InputStream readIs(final Path path) {
    final PipedInputStream pis = new PipedInputStream();
    final AtomicLong pos = new AtomicLong(0);
    try {/*from   w  w w  .j  a v  a 2 s .  c o  m*/
        final PipedOutputStream ostream = new PipedOutputStream(pis);
        final AsynchronousFileChannel channel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);
        final ByteBuffer buffer = ByteBuffer.allocate(1 << 16);
        channel.read(buffer, pos.get(), buffer, new CompletionHandler<Integer, ByteBuffer>() {
            @Override
            public void completed(final Integer result, final ByteBuffer buf) {
                try {
                    if (result == -1) {
                        ostream.close();
                        return;
                    }
                    final byte[] bytes = new byte[result];
                    System.arraycopy(buf.array(), 0, bytes, 0, result);
                    ostream.write(bytes);
                    ostream.flush();
                    if (result < 1 << 16) {
                        ostream.close();
                        return;
                    }
                    pos.addAndGet(result);
                    final ByteBuffer buffer = ByteBuffer.allocate(1 << 16);
                    channel.read(buffer, pos.get(), buffer, this);
                } catch (final IOException e) {
                    Throwables.propagate(e);
                }
            }

            @Override
            public void failed(final Throwable exc, final ByteBuffer attachment) {
                log.error(exc.getMessage(), exc);
            }
        });
    } catch (final IOException e) {
        if (e instanceof NoSuchFileException) {
            throw new NotFoundException(path);
        }
        Throwables.propagate(e);
    }
    return pis;
}

From source file:ubicrypt.core.Utils.java

public static Observable<byte[]> read(final Path path) {
    return Observable.create(subscriber -> {
        final AtomicLong pos = new AtomicLong(0);
        try {/*from   w w  w .  j  a  v  a 2 s . com*/
            final AsynchronousFileChannel channel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);
            read(pos, ByteBuffer.allocate(1 << 16), channel, subscriber);
        } catch (final Throwable e) {
            subscriber.onError(e);
        }
    });
}