Ejemplo n.º 1
0
  private Interfaces.Callback<Long> makeReader(
      AsynchronousFileChannel channel,
      boolean autoClose,
      long position,
      Interfaces.Callback<ByteBuffer> cb) {

    return (err, fileSize) -> {
      if (fileSize > Integer.MAX_VALUE) {
        String message =
            "File size "
                + fileSize
                + " exceeds maximum allowed value. Use fs.createReadStream to read big files.";
        eventLoop.runTicket(() -> cb.invoke(new IOException(message), null));
        return;
      }

      if (err != null) {
        eventLoop.runTicket(() -> cb.invoke(err, null));
        return;
      }

      eventLoop.addBackgroundOperation();

      ByteBuffer buffer = ByteBuffer.allocate(fileSize.intValue());

      CompletionHandler<Integer, ByteBuffer> completion =
          new ReadCompletionHandler(cb, channel, autoClose);
      channel.read(buffer, position, buffer, completion);
    };
  }
Ejemplo n.º 2
0
  private <T> AsynchronousFileChannel openFileChannel(Interfaces.Callback<T> cb, Path file) {
    AsynchronousFileChannel channel = null;
    try {
      channel = AsynchronousFileChannel.open(file);

    } catch (IOException e) {

      eventLoop.runTicket(() -> cb.invoke(new RuntimeException(e), null));
    }

    return channel;
  }