JSR-000203 More New I/O APIs for the Java Platform - Early Draft Review
http://jcp.org/aboutJava/communityprocess/edr/jsr203/index.html
API的Early Draft Review出来了,就意味很快就要真的出来啦!!
以下是其文档的一个Sample
就是我最近很需要的东西,一个异步I/O的实现,十分期待中!!
http://jcp.org/aboutJava/communityprocess/edr/jsr203/index.html
API的Early Draft Review出来了,就意味很快就要真的出来啦!!
以下是其文档的一个Sample
static class IOTransaction {
public ByteBuffer buffer() {
}
public long position() {
}
public long updatePosition(int transferred) {
}
}
static class WriteHandler implements CompletionHandler<Integer> {
public WriteHandler(AsynchronousFileChannel ch) {
}
private AsynchronousFileChannel channel() {
}
public void completed(IoFuture<Integer> result) {
int bytesTransferred;
try {
bytesTransferred = result.getNow();
} catch (ExecutionException x) {
}
IOTransaction transaction = (IOTransaction)result.attachment();
ByteBuffer buffer = transaction.buffer();
if (buffer.remaining() > 0) {
long position = transaction.updatePosition(bytesTransferred);
channel().write(buffer, position, transaction, this);
}
}
}
FileReference file =
List<IOTransaction> transactionList =
AsynchronousFileChannel ch = AsynchronousFileChannel.open(file, OpenFlag.WRITE);
WriteHandler handler = new WriteHandler(ch);
for (IOTransaction transaction: transactionList) {
// use the transaction as the attachment
ch.write(transaction.buffer(), transaction.position(), transaction, handler);
}
public ByteBuffer buffer() {

public long position() {

public long updatePosition(int transferred) {

}
static class WriteHandler implements CompletionHandler<Integer> {
public WriteHandler(AsynchronousFileChannel ch) {

private AsynchronousFileChannel channel() {

public void completed(IoFuture<Integer> result) {
int bytesTransferred;
try {
bytesTransferred = result.getNow();
} catch (ExecutionException x) {

IOTransaction transaction = (IOTransaction)result.attachment();
ByteBuffer buffer = transaction.buffer();
if (buffer.remaining() > 0) {
long position = transaction.updatePosition(bytesTransferred);
channel().write(buffer, position, transaction, this);
}
}
}
FileReference file =

List<IOTransaction> transactionList =

AsynchronousFileChannel ch = AsynchronousFileChannel.open(file, OpenFlag.WRITE);
WriteHandler handler = new WriteHandler(ch);
for (IOTransaction transaction: transactionList) {
// use the transaction as the attachment
ch.write(transaction.buffer(), transaction.position(), transaction, handler);
}
就是我最近很需要的东西,一个异步I/O的实现,十分期待中!!