I believe that I have discovered a bug in sun.nio.ch.IOUtil.write(java.io.FileDescriptor, java.nio.ByteBuffer, long, sun.nio.ch.NativeDispatcher, java.lang.Object). The bug triggers the following stack trace in the test case:
Exception in thread "Main Thread" java.nio.ReadOnlyBufferException
at java.nio.ByteBuffer.array(ByteBuffer.java:723)
at sun.nio.ch.IOUtil.write(IOUtil.java:168)
at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:334)
at NioBug.main(NioBug.java:15)
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
public class NioBug {
public static void main(String[] args) throws IOException {
SocketChannel sc = SocketChannel.open(new InetSocketAddress("www.google.com", 80));
sc.configureBlocking(false);
ByteBuffer write = ByteBuffer.wrap("GET /\n".getBytes()).asReadOnlyBuffer();
while (write.hasRemaining()) {
sc.write(write);
}
}
}
The test attempts to write the contents of a read-only heap ByteBuffer to a SocketChannel. Inside sun.nio.ch.IOUtil.write(...) in order to copy the heap data in to a temporary direct ByteBuffer an unguarded call is made to ByteBuffer.array() which triggers the ReadOnlyBufferException seen in the test as the supplied buffer is not allowed to expose its contents to potential mutators.