Package java.nio.channels

Examples of java.nio.channels.AsynchronousFileChannel


            if (log.isDebugEnabled()) {
                log.debug("open({}, {}, {})", pathStr, flags, mode);
            }

            Path path = translatePath(pathStr);
            AsynchronousFileChannel file = null;

            // To support "lchmod", we need to check "O_SYMLINK" here too
            if (!Files.isDirectory(path)) {
                // Open an AsynchronousFileChannel using all the relevant open options.
                // But if we are opening a symbolic link or directory, just record the path and go on
                HashSet<OpenOption> options = new HashSet<OpenOption>();
                if ((flags & Constants.O_CREAT) != 0) {
                    if ((flags & Constants.O_EXCL) != 0) {
                        options.add(StandardOpenOption.CREATE_NEW);
                    } else {
                        options.add(StandardOpenOption.CREATE);
                    }
                }
                if ((flags & Constants.O_RDWR) != 0) {
                    options.add(StandardOpenOption.READ);
                    options.add(StandardOpenOption.WRITE);
                } else if ((flags & Constants.O_WRONLY) != 0) {
                    options.add(StandardOpenOption.WRITE);
                } else {
                    options.add(StandardOpenOption.READ);
                }

                if ((flags & Constants.O_TRUNC) != 0) {
                    options.add(StandardOpenOption.TRUNCATE_EXISTING);
                }
                if ((flags & Constants.O_SYNC) != 0) {
                    options.add(StandardOpenOption.SYNC);
                }

                try {
                    if (log.isDebugEnabled()) {
                        log.debug("Opening {} with {}", path, options);
                    }
                    if (Platform.get().isPosixFilesystem()) {
                        file = AsynchronousFileChannel.open(path, options, pool,
                            PosixFilePermissions.asFileAttribute(modeToPerms(mode, true)));
                    } else {
                        file = AsynchronousFileChannel.open(path, options, pool,
                                                            new FileAttribute<?>[0]);
                        setModeNoPosix(path, mode);
                    }

                } catch (IOException ioe) {
                    throw new NodeOSException(getErrorCode(ioe), ioe, pathStr);
                }
            }

            try {
                FileHandle fileHandle = new FileHandle(path, file);
                // Replace this if we choose to support "lchmod"
                /*
                if ((flags & Constants.O_SYMLINK) != 0) {
                    fileHandle.noFollow = true;
                }
                */
                int fd = nextFd.getAndIncrement();
                if (log.isDebugEnabled()) {
                    log.debug("  open({}) = {}", pathStr, fd);
                }
                if (((flags & Constants.O_APPEND) != 0) && (file != null) && (file.size() > 0)) {
                    if (log.isDebugEnabled()) {
                        log.debug("  setting file position to {}", file.size());
                    }
                    fileHandle.position = file.size();
                }
                descriptors.put(fd, fileHandle);
                return new Object [] { Context.getUndefinedValue(), fd };
            } catch (IOException ioe) {
                throw new NodeOSException(getErrorCode(ioe), ioe, pathStr);
View Full Code Here


  @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) {
    }
  }
View Full Code Here

     *                                       write access if the file is opened for writing
     */
    @Suspendable
    public static FiberFileChannel open(final ExecutorService ioExecutor, final Path path, final Set<? extends OpenOption> options, final FileAttribute<?>... attrs) throws IOException {
        final ExecutorService ioExec = ioExecutor != null ? ioExecutor : fiberFileThreadPool; // FiberAsyncIO.ioExecutor(); //
        AsynchronousFileChannel afc = FiberAsyncIO.runBlockingIO(fiberFileThreadPool, new CheckedCallable<AsynchronousFileChannel, IOException>() {
            @Override
            public AsynchronousFileChannel call() throws IOException {
                return AsynchronousFileChannel.open(path, options, ioExec, attrs);
            }
        });
View Full Code Here

  //and quick!

  @Override
  public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
    if (file.toString().endsWith(".txt")) {
      AsynchronousFileChannel channel = AsynchronousFileChannel.open(file, StandardOpenOption.READ);
      ByteBuffer buffer = ByteBuffer.allocate(100_000);
      Future<Integer> result = channel.read(buffer, 0);
        Path dest = Paths.get(file.toString()+".backup");
                if(!Files.exists(dest)) {
                    Files.createFile(dest);
                }
      try {
                                // makes a buffer ready for a new sequence of channel-write
                                buffer.flip();
                                //these are the most permissive StandardOpenOptions
        AsynchronousFileChannel channelWrite = AsynchronousFileChannel.open(dest, StandardOpenOption.WRITE, StandardOpenOption.CREATE );
        channelWrite.write(buffer,0);
        Integer bytesRead = result.get();
        System.out.println("Bytes read [" + bytesRead + "]");
      } catch (InterruptedException ex) {
        Logger.getLogger(EfficientJackFileVisitor.class.getName()).log(Level.SEVERE, null, ex);
      } catch (ExecutionException ex) {
View Full Code Here

public class Listing_2_9 {

  public static void main(String[] args) {
    try {
      Path file = Paths.get("/usr/karianna/foobar.txt");
      AsynchronousFileChannel channel = AsynchronousFileChannel.open(file);

      ByteBuffer buffer = ByteBuffer.allocate(100_000);

      channel.read(buffer, 0, buffer,
          new CompletionHandler<Integer, ByteBuffer>() {

            public void completed(Integer result, ByteBuffer attachment) {
              System.out.println("Bytes read [" + result + "]");
            }
View Full Code Here

TOP

Related Classes of java.nio.channels.AsynchronousFileChannel

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.