Name: gm110360 Date: 04/05/2002
FULL PRODUCT VERSION :
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)
FULL OPERATING SYSTEM VERSION :
SunOS jersy5 5.7
Generic_106542-19 i86pc i386 i86pc
A DESCRIPTION OF THE PROBLEM :
The SocketChannel is registered as:
channel.register(selector, SelectionKey.OP_READ);
When SelectionKey for that channel indicates that channel
is ready for reading ( key.isReadable() returns true),
call to
channel.socket().getInputStream().available() returns 0.
EXPECTED VERSUS ACTUAL BEHAVIOR :
The call to:
channel.socket().getInputStream().available()
should return number of bytes available in the InputStream.
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.net.*;
import java.util.*;
public class AvailableBytesTest implements Runnable {
int port = 9999;
Selector selector;
InetSocketAddress isa;
public AvailableBytesTest() throws IOException {
ServerSocketChannel channel = ServerSocketChannel.open();
channel.configureBlocking(false);
InetAddress ia = InetAddress.getLocalHost();
isa = new InetSocketAddress(ia, port);
channel.socket().bind(isa);
selector = Selector.open();
channel.register(selector, SelectionKey.OP_ACCEPT);
}
public void run() {
SocketChannel channel = null;
try {
channel = SocketChannel.open();
channel.connect(isa);
byte[] data = new byte[64];
ByteBuffer buf = ByteBuffer.wrap(data);
channel.write(buf);
} catch(IOException e) {
e.printStackTrace();
}
try {
Thread.sleep(1000);
selector.wakeup();
Thread.sleep(1000);
channel.close();
} catch(Exception e) {
e.printStackTrace();
}
}
public void exec() throws IOException {
new Thread(this).start();
ByteBuffer buf = ByteBuffer.allocate(64);
for (;;) {
System.out.println("Waiting...");
if ( selector.select() == 0 ) {
System.out.println("Exiting...");
break;
}
System.out.println("Working...");
Iterator it = selector.selectedKeys().iterator();
while (it.hasNext()) {
SelectionKey key = (SelectionKey) it.next();
it.remove();
if (key.isAcceptable()) {
System.out.println("Got acceptable key.");
ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
SocketChannel channel = (SocketChannel) ssc.accept();
channel.configureBlocking(false);
channel.register(selector, SelectionKey.OP_READ);
}
if (key.isReadable()) {
System.out.println("Got readable key.");
SocketChannel channel = (SocketChannel) key.channel();
int num = channel.socket().getInputStream().available();
System.out.println("Number of available bytes: " + num);
num = channel.read(buf);
System.out.println("Number of bytes read: " + num);
}
}
}
}
public static void main( String[] args ) {
try {
new AvailableBytesTest().exec();
} catch (IOException e) {
e.printStackTrace();
}
}
}
---------- END SOURCE ----------
(Review ID: 145070)
======================================================================
- duplicates
-
JDK-4648049 (ch) Stream adaptors do not usefully implement available()
-
- Closed
-