42 public static void main(String[] args) throws Exception { |
42 public static void main(String[] args) throws Exception { |
43 // create listener to accept connections |
43 // create listener to accept connections |
44 final AsynchronousServerSocketChannel listener = |
44 final AsynchronousServerSocketChannel listener = |
45 AsynchronousServerSocketChannel.open() |
45 AsynchronousServerSocketChannel.open() |
46 .bind(new InetSocketAddress(0)); |
46 .bind(new InetSocketAddress(0)); |
47 listener.accept(null, new CompletionHandler<AsynchronousSocketChannel,Void>() { |
47 listener.accept((Void)null, new CompletionHandler<AsynchronousSocketChannel,Void>() { |
48 public void completed(AsynchronousSocketChannel ch, Void att) { |
48 public void completed(AsynchronousSocketChannel ch, Void att) { |
49 listener.accept(null, this); |
49 listener.accept((Void)null, this); |
50 } |
50 } |
51 public void failed(Throwable exc, Void att) { |
51 public void failed(Throwable exc, Void att) { |
52 } |
52 } |
53 public void cancelled(Void att) { |
53 public void cancelled(Void att) { |
54 } |
54 } |
79 // the latch counts down when: |
79 // the latch counts down when: |
80 // 1. The read operation fails (expected) |
80 // 1. The read operation fails (expected) |
81 // 2. the close/shutdown completes |
81 // 2. the close/shutdown completes |
82 final CountDownLatch latch = new CountDownLatch(2); |
82 final CountDownLatch latch = new CountDownLatch(2); |
83 |
83 |
84 ch.connect(sa, null, new CompletionHandler<Void,Void>() { |
84 ch.connect(sa, (Void)null, new CompletionHandler<Void,Void>() { |
85 public void completed(Void result, Void att) { |
85 public void completed(Void result, Void att) { |
86 System.out.println("Connected"); |
86 System.out.println("Connected"); |
87 |
87 |
88 // initiate I/O operation that does not complete (successfully) |
88 // initiate I/O operation that does not complete (successfully) |
89 ByteBuffer buf = ByteBuffer.allocate(100); |
89 ByteBuffer buf = ByteBuffer.allocate(100); |
90 ch.read(buf, null, new CompletionHandler<Integer,Void>() { |
90 ch.read(buf, (Void)null, new CompletionHandler<Integer,Void>() { |
91 public void completed(Integer bytesRead, Void att) { |
91 public void completed(Integer bytesRead, Void att) { |
92 throw new RuntimeException(); |
92 throw new RuntimeException(); |
93 } |
93 } |
94 public void failed(Throwable exc, Void att) { |
94 public void failed(Throwable exc, Void att) { |
95 if (!(exc instanceof AsynchronousCloseException)) |
95 if (!(exc instanceof AsynchronousCloseException)) |