AsynchronousFileChannel中的CompletionHandler读取数据有什么用?
What is the use of CompletionHandler in AsynchronousFileChannel for reading data?
我正在使用 AsynchronousFileChannel 读取数据。
为了读取数据,我找到了如下两种读取方法:
//1.
Future<Integer> java.nio.channels.AsynchronousFileChannel.read(ByteBuffer dst, long position);
//2.
void java.nio.channels.AsynchronousFileChannel.read(ByteBuffer dst, long position, A attachment, CompletionHandler<Integer, ? super A> handler)
由于java文档如下所述,没有关于将CompletionHandler用作函数的第三个参数的信息:
Reads a sequence of bytes from this channel into the given buffer, starting at the given file position.
This method initiates the reading of a sequence of bytes from this channel into the given buffer, starting at the given file position. The result of the read is the number of bytes read or -1 if the given position is greater than or equal to the file's size at the time that the read is attempted.
This method works in the same manner as the AsynchronousByteChannel.read(ByteBuffer, Object, CompletionHandler) method, except that bytes are read starting at the given file position. If the given file position is greater than the file's size at the time that the read is attempted then no bytes are read.
任何人都可以让我知道第三个参数,以及 CompletionHandler 的任何工作示例吗?为什么我们需要 CompletionHandler 以及它的用途是什么?
这是我搜索并按如下方式运行的示例:
try(AsynchronousFileChannel asyncfileChannel = AsynchronousFileChannel.open(Paths.get("/Users/***/Documents/server_pull/system_health_12_9_TestServer.json"), StandardOpenOption.READ)){
ByteBuffer buffer = ByteBuffer.allocate(1024);
ByteBuffer attachment = ByteBuffer.allocate(1024);
asyncfileChannel.read(buffer, 0, attachment, new CompletionHandler<Integer, ByteBuffer>() {
@Override
public void completed(Integer result, ByteBuffer attachment) {
System.out.println("result = " + result);
attachment.flip();
byte[] data = new byte[attachment.limit()];
attachment.get(data);
System.out.println(new String(data));
attachment.clear();
}
@Override
public void failed(Throwable exc, ByteBuffer attachment) {
}
});
}catch(Exception e){
e.printStackTrace();
}
下面是处理细节:
Once the read operation finishes, the CompletionHandler's completed() method will be called. As parameters to the completed() method are passed an Integer telling how many bytes were read, and the "attachment" which was passed to the read() method. The "attachment" is the third parameter to the read() method. In this case it was the ByteBuffer into which the data is also read.
如果读取操作失败,将改为调用 CompletionHandler 的 failed() 方法。
我正在使用 AsynchronousFileChannel 读取数据。 为了读取数据,我找到了如下两种读取方法:
//1.
Future<Integer> java.nio.channels.AsynchronousFileChannel.read(ByteBuffer dst, long position);
//2.
void java.nio.channels.AsynchronousFileChannel.read(ByteBuffer dst, long position, A attachment, CompletionHandler<Integer, ? super A> handler)
由于java文档如下所述,没有关于将CompletionHandler用作函数的第三个参数的信息:
Reads a sequence of bytes from this channel into the given buffer, starting at the given file position.
This method initiates the reading of a sequence of bytes from this channel into the given buffer, starting at the given file position. The result of the read is the number of bytes read or -1 if the given position is greater than or equal to the file's size at the time that the read is attempted.
This method works in the same manner as the AsynchronousByteChannel.read(ByteBuffer, Object, CompletionHandler) method, except that bytes are read starting at the given file position. If the given file position is greater than the file's size at the time that the read is attempted then no bytes are read.
任何人都可以让我知道第三个参数,以及 CompletionHandler 的任何工作示例吗?为什么我们需要 CompletionHandler 以及它的用途是什么?
这是我搜索并按如下方式运行的示例:
try(AsynchronousFileChannel asyncfileChannel = AsynchronousFileChannel.open(Paths.get("/Users/***/Documents/server_pull/system_health_12_9_TestServer.json"), StandardOpenOption.READ)){
ByteBuffer buffer = ByteBuffer.allocate(1024);
ByteBuffer attachment = ByteBuffer.allocate(1024);
asyncfileChannel.read(buffer, 0, attachment, new CompletionHandler<Integer, ByteBuffer>() {
@Override
public void completed(Integer result, ByteBuffer attachment) {
System.out.println("result = " + result);
attachment.flip();
byte[] data = new byte[attachment.limit()];
attachment.get(data);
System.out.println(new String(data));
attachment.clear();
}
@Override
public void failed(Throwable exc, ByteBuffer attachment) {
}
});
}catch(Exception e){
e.printStackTrace();
}
下面是处理细节:
Once the read operation finishes, the CompletionHandler's completed() method will be called. As parameters to the completed() method are passed an Integer telling how many bytes were read, and the "attachment" which was passed to the read() method. The "attachment" is the third parameter to the read() method. In this case it was the ByteBuffer into which the data is also read.
如果读取操作失败,将改为调用 CompletionHandler 的 failed() 方法。