Java NIO2 AsynchronousFileChannel returns Future<Integer> 没有关于实际值的文档

Java NIO2 AsynchronousFileChannel returns Future<Integer> with no documnetation on actual values

所以我根据我在网上找到的文档和示例想出了以下功能,以异步方式写入文件:

    public static Future<Integer> createAndWriteToFile(String fullFileName, String content) throws IOException {
        Path file = Utils.createFile(fullFileName);
        AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(file, StandardOpenOption.WRITE, StandardOpenOption.CREATE);

        ByteBuffer buffer = ByteBuffer.wrap(content.getBytes());
        Future<Integer> operation = fileChannel.write(buffer, 0);
        buffer.clear();

        return operation;
}

但是,绝对没有关于调用 operation.get().intValue() 的预期结果的文档! 在调试模式下,成功文件 creation/writing returns 整数值 23。 其他可能的值是什么? 文档: https://docs.oracle.com/javase/8/docs/api/java/nio/channels/AsynchronousFileChannel.html#write-java.nio.ByteBuffer-long-

这里: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Future.html#get--

Returns: the computed result

有用,没用。

作为一个旁白的问题 - 让我感到惊讶的是,Java 被认为是记录最完备的语言之一,为什么这个函数没有记录?

尽管在 return 中没有直接说明,但在 docs 中键入。如果您仔细查看此处描述的段落:

This method works in the same manner as the AsynchronousByteChannel.write(ByteBuffer) method, except that bytes are written starting at the given file position. If the given position is greater than the file's size, at the time that the write is attempted, then the file will be grown to accommodate the new bytes; the values of any bytes between the previous end-of-file and the newly-written bytes are unspecified.

所以它说它的行为就像 AsynchronousByteChannel.write(ByteBuffer) 一样,它将您重定向到 Future<Integer> write(ByteBuffer src)。它指定了 Integer 值的含义,基本上就是写入的字节数。

This method initiates an asynchronous write operation to write a sequence of bytes to this channel from the given buffer. The method behaves in exactly the same manner as the write(ByteBuffer,Object,CompletionHandler) method except that instead of specifying a completion handler, this method returns a Future representing the pending result. The Future's get method returns the number of bytes written.