InputStream created from a ServerSocketChannel.accept() always returns available() = 0

591 views
Skip to first unread message

Remi

unread,
Jul 31, 2012, 11:20:53 AM7/31/12
to av...@googlegroups.com
InputStream created from a ServerSocketChannel.accept() always returns available() = 0, but read can be performed and bytes will be returned

Joel Dice

unread,
Jul 31, 2012, 12:30:22 PM7/31/12
to av...@googlegroups.com
On Tue, 31 Jul 2012, Remi wrote:

> InputStream created from a ServerSocketChannel.accept() always returns
> available() = 0, but read can be performed and bytes will be returned

Can you be more specific? ServerSocketChannel.accept returns a
SocketChannel, not an InputStream, and the only way to convert that to an
InputStream is using Channels.newInputStream (since Avian's class library
doesn't currently have Socket.getInputStream). Is that what you're doing?
Can you supply a test case that works on e.g. OpenJDK but fails on Avian?

Thanks.

Remi

unread,
Aug 1, 2012, 4:06:23 AM8/1/12
to av...@googlegroups.com
Am Dienstag, 31. Juli 2012 18:30:22 UTC+2 schrieb Joel Dice:
On Tue, 31 Jul 2012, Remi wrote:

> InputStream created from a ServerSocketChannel.accept() always returns
> available() = 0, but read can be performed and bytes will be returned

Can you be more specific?  ServerSocketChannel.accept returns a
SocketChannel, not an InputStream, and the only way to convert that to an
InputStream is using Channels.newInputStream (...) Is that what you're doing?

Yes, this is what I'm doing. I thought I was specific enough, because there is no another way to do it, isn't it? :)

// create the server socket
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().bind(new InetSocketAddress("10.0.2.3", 80)); // On sun-vm I can specify new InetSocketAddress((InetAddress)null, 80) to bind to all interfaces, but here in Avian I need to pass an IP-Address.... :(

// listen for a connection
SocketChannel socketchannel = serverSocketChannel.accept();

// Now I'm connecting with Chrome to that socket. 10.0.2.3 is my own IP
// After the connection established, I receive a SocketChannel object. Then I do..
InputStream inputStream = Channels.newInputStream(socketChannel);

// then in a loop I'm reading the incoming HTTP-Header
for(;;) {
   int n = inputStream.available();
   // n = 1024;
   if (n > 0) {
       // read it here...
       byte[] data = new byte[n];
       int readBytes = inputStream.read(data)
   (...)
   } else {
     // relax
     Thread.sleep(1);
   }
}

==> n is always = 0
==> but if I set n to 1024 to enter the if (n > 0), then read(data) reads the incoming bytes and returns the correct read number of bytes (in my case there are 417 bytes)
in sun-vm available behaves correctly and returns the correct number of available bytes

Joel Dice

unread,
Aug 1, 2012, 6:59:03 PM8/1/12
to av...@googlegroups.com
When I run the attached test on OpenJDK 7, I get the following output upon
connecting with Chromium to localhost:9200:

$ javac ChannelTest.java && java ChannelTest
sun.nio.ch.ChannelInputStream
seekable? false
0 available!
0 available!
0 available!
0 available!
0 available!
...

$ java -version
java version "1.7.0_03"
OpenJDK Runtime Environment (IcedTea7 2.1.1) (7~u3-2.1.1-1)
OpenJDK 64-Bit Server VM (build 22.0-b10, mixed mode)

Indeed, if you look at OpenJDK's sun.nio.ch.ChannelInputStream
implementation, you'll see that available() returns zero except in the
case of SeekableByteChannel, and SocketChannel does not implement
SeekableByteChannel. What version of Java are you using?
ChannelTest.java

Remi

unread,
Aug 2, 2012, 3:42:54 AM8/2/12
to av...@googlegroups.com
You are right! I checked it again today and saw the inputStream.available() method returning a 0 also in the Sun-VM. Then I wonder, why my code have work the last years.... After checking whats going on, I saw, that while using the Socket-Class in the Sun-VM the input stream behaves correctly returning the available bytes. But if I use the channel-version of the API, (the only possibility in Avian), then available() returns 0. I thought, I did the tests in Sun-VM with the channel-thing, but I was wrong, it was socket. Sorry for stealing your time!

This bug-report should be marked as invalid.
Reply all
Reply to author
Forward
0 new messages