Message from discussion
Are there some samples to read or write from AsynchronousSocketChannel?
Received: by 10.35.14.4 with SMTP id r4mr15013633pyi.2.1201858602597;
Fri, 01 Feb 2008 01:36:42 -0800 (PST)
Return-Path: <Alan.Bate...@sun.com>
Received: from gmp-eb-mail-2.sun.com (gmp-eb-mail-2.sun.com [192.18.6.24])
by mx.google.com with ESMTP id a28si6258479pye.0.2008.02.01.01.36.42;
Fri, 01 Feb 2008 01:36:42 -0800 (PST)
Received-SPF: pass (google.com: best guess record for domain of Alan.Bate...@sun.com designates 192.18.6.24 as permitted sender) client-ip=192.18.6.24;
Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of Alan.Bate...@sun.com designates 192.18.6.24 as permitted sender) smtp.mail=Alan.Bate...@sun.com
Received: from fe-emea-10.sun.com (gmp-eb-lb-2-fe3.eu.sun.com [192.18.6.12])
by gmp-eb-mail-2.sun.com (8.13.7+Sun/8.12.9) with ESMTP id m119afHU014816
for <jsr203-interest@googlegroups.com>; Fri, 1 Feb 2008 09:36:41 GMT
Received: from conversion-daemon.fe-emea-10.sun.com by fe-emea-10.sun.com
(Sun Java System Messaging Server 6.2-8.04 (built Feb 28 2007))
id <0JVJ00I01ZLOL...@fe-emea-10.sun.com>
(original mail from Alan.Bate...@Sun.COM)
for jsr203-interest@googlegroups.com; Fri, 01 Feb 2008 09:36:41 +0000 (GMT)
Received: from [192.168.1.100] ([83.70.70.87])
by fe-emea-10.sun.com (Sun Java System Messaging Server 6.2-8.04 (built Feb 28
2007)) with ESMTPSA id <0JVK008CO00WA...@fe-emea-10.sun.com> for
jsr203-interest@googlegroups.com; Fri, 01 Feb 2008 09:36:32 +0000 (GMT)
Date: Fri, 01 Feb 2008 09:33:33 +0000
From: Alan Bateman <Alan.Bate...@Sun.COM>
Subject: Re: [jsr203-interest] Are there some samples to read or write from
AsynchronousSocketChannel?
In-reply-to: <f8755fe8-fb9c-4cc6-976b-326042e13f6f@v29g2000hsf.googlegroups.com>
Sender: Alan.Bate...@Sun.COM
To: jsr203-interest@googlegroups.com
Message-id: <47A2E76D.7030503@sun.com>
MIME-version: 1.0
Content-type: text/plain; format=flowed; charset=ISO-8859-1
Content-transfer-encoding: 7BIT
References: <f8755fe8-fb9c-4cc6-976b-326042e13f6f@v29g2000hsf.googlegroups.com>
User-Agent: Thunderbird 2.0.0.9 (Windows/20071031)
Arzhe...@gmail.com wrote:
> hi,
> my problem is, I dont know how to deal with the method of the
> 'completed( IoFuture<Integer> result )' . what can i do in this
> method? and how i know the byteBuffer is ok? does the following is
> right?
>
It's mostly right. When a read completes you'll almost always need to
access the buffer and perhaps other context (channel, SSLSession, etc)
in order to process the bytes that have been read. In your example, the
completion handler is an anonymous inner class so "asc" and "bb" would
need to be final. The alternative is to pass the ByteBuffer as the
attachment or when you have other context then define a container class
and use an instance as the attachment. As was pointed out by Matthias
Ernst some time back the attachment wasn't generified in the early draft
and that has since been updated. With the buffer as the attachment this
gives you:
ch.read(buf, buf, new CompletionHandler<Integer,ByteBuffer>() {
public void completed(IoFuture<Integer,ByteBuffer> result) {
try {
int bytesRead = result.getNow();
ByteBuffer buf = result.attachment();
} catch (IOException x) {
// read failed
}
}
});
(As an side, if closures are added to the language then this can be made
a lot nicer).
As regards bigger samples. I'm working on moving the implementation to
OpenJDK (some process work needs to be completed first) and that will
allow you try it out. I've created bunch of samples for various aspects
of the API and they should provide a useful start.
-Alan.