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

 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);
  }


就是我最近很需要的东西,一个异步I/O的实现,十分期待中!!



温少 2007-04-14 12:15 发表评论
posted on 2007-04-14 12:15 温少的日志 阅读(255) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: