JDK-4778091 : (se) Selector.keys() should be immutable
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 1.4.2,5.0
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic,solaris_2.6
  • CPU: generic,sparc
  • Submitted: 2002-11-13
  • Updated: 2017-05-19
  • Resolved: 2003-12-19
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
5.0 b32Fixed
Related Reports
Duplicate :  
Relates :  
Description

Name: auR10023			Date: 11/13/2002



java.nio.channels.Selector.keys().add() doesn't throw 
UnsupportedOperationException.

Javadoc for Selector.keys() method states:

...
The key set is not directly modifiable. A key is removed only after it has been cancelled and its channel has been deregistered. Any attempt to modify the key set will cause an UnsupportedOperationException to be thrown.
...

Here is the example:

-------test.java---------

import java.io.*;
import java.nio.*;
import java.nio.channels.*;

public class test  {
        
    public static void main(String args[]) {
        
        DatagramChannel channel = null;
        Selector sel = null;
        SelectionKey key = null;
        try {
            channel = DatagramChannel.open();
            channel.configureBlocking(false);
            sel = Selector.open();

            key = channel.register(
            sel, SelectionKey.OP_WRITE);
        } catch (IOException e) {
            System.out.println("Unexpected IOException");
            return;
        }

        try {
            sel.keys().add(key);
            System.out.println("UnsupportedOperationException should be " +
                               "thrown");
            return;
        } catch (UnsupportedOperationException e) {
        }
        System.out.println("OKAY");
    }
}

Here is the result
#java -version
java version "1.4.2-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-beta-b06)
Java HotSpot(TM) Client VM (build 1.4.2-beta-b06, mixed mode)

#java test
UnsupportedOperationException should be thrown

======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: tiger tiger-beta FIXED IN: tiger-beta INTEGRATED IN: tiger-b32 tiger-beta VERIFIED IN: tiger-b63
13-09-2004

EVALUATION A selector's key set should be completely immutable per the specification. Both the add and addAll methods should throw UnsupportedOperationException, as should the remove method of any iterator over this set. -- ###@###.### 2002/11/15
11-10-0181