-
Bug
-
Resolution: Fixed
-
P2
-
1.4.0
-
hopper
-
sparc
-
solaris_8
-
Verified
Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-2048961 | 1.4.0 | Michael Mccloskey | P2 | Resolved | Fixed | rc1 |
java full version "1.4.0-beta3-b84"
When SocketChannel.read() is called with multiple direct ByteBuffers
it appears to use the limits of the buffers to determine how many
bytes to read (a good thing), but then fills the buffers to capacity
instead of limit.
For example if you do the following:
bufs[0] = ByteBuffer.allocateDirect(1024);
bufs[1] = ByteBuffer.allocateDirect(1024);
bufs[0].limit(128);
bufs[1].limit(128);
sc.read(bufs, 0, 2);
The SocketChannel will read 256 bytes, but it places
all 256 bytes into the first ByteBuffer and 0 bytes into
the second. It also sets 'limit' on the first byte buffer
to 256.
If you use normal ByteBuffers (not direct) then 128 bytes
are placed into each buffer as expected.
The attached test case demonstrates this. It starts two threads.
One is a "client" thread that writes 256 bytes to the "server" thread.
The "server" thread uses two buffers to read the data as described
above. It displays the buffers before and after the read. As you
can see when a direct buffer is used all 256 bytes are read into the
first buffer:
+ /usr/java1.4/bin/java -classpath . PacketTest
Client: starting
Client: writing 256 bytes java.nio.DirectByteBuffer[pos=0 lim=256 cap=256]
Client: write complete
SERVER: Accepted client connection
SERVER: before read:
java.nio.DirectByteBuffer[pos=0 lim=128 cap=1024]
java.nio.DirectByteBuffer[pos=0 lim=128 cap=1024]
SERVER: after read:
java.nio.DirectByteBuffer[pos=256 lim=256 cap=1024]
java.nio.DirectByteBuffer[pos=0 lim=128 cap=1024]
If you then run it with the "-nodirect" option the application will
use normal buffers. In this case 128 bytes are read into each buffer:
+ /usr/java1.4/bin/java -classpath . PacketTest -nodirect
Client: starting
Client: writing 256 bytes java.nio.DirectByteBuffer[pos=0 lim=256 cap=256]
Client: write complete
SERVER: Accepted client connection
SERVER: before read:
java.nio.HeapByteBuffer[pos=0 lim=128 cap=1024]
java.nio.HeapByteBuffer[pos=0 lim=128 cap=1024]
SERVER: after read:
java.nio.HeapByteBuffer[pos=128 lim=128 cap=1024]
java.nio.HeapByteBuffer[pos=128 lim=128 cap=1024]
- backported by
-
JDK-2048961 (ch) Scattering reads ignore buffer limits
-
- Resolved
-