jdk/src/share/classes/sun/nio/ch/DatagramChannelImpl.java
changeset 2057 3acf8e5e2ca0
parent 1247 b4c26443dee5
child 2427 f35f516befc3
--- a/jdk/src/share/classes/sun/nio/ch/DatagramChannelImpl.java	Wed Feb 11 13:16:53 2009 +0000
+++ b/jdk/src/share/classes/sun/nio/ch/DatagramChannelImpl.java	Sun Feb 15 12:25:54 2009 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright 2001-2008 Sun Microsystems, Inc.  All Rights Reserved.
+ * Copyright 2001-2009 Sun Microsystems, Inc.  All Rights Reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -111,8 +111,12 @@
     public DatagramChannelImpl(SelectorProvider sp, ProtocolFamily family) {
         super(sp);
         if ((family != StandardProtocolFamily.INET) &&
-            (family != StandardProtocolFamily.INET6)) {
-            throw new UnsupportedOperationException("Protocol family not supported");
+            (family != StandardProtocolFamily.INET6))
+        {
+            if (family == null)
+                throw new NullPointerException("'family' is null");
+            else
+                throw new UnsupportedOperationException("Protocol family not supported");
         }
         if (family == StandardProtocolFamily.INET6) {
             if (!Net.isIPv6Available()) {
@@ -149,28 +153,28 @@
     public SocketAddress getLocalAddress() throws IOException {
         synchronized (stateLock) {
             if (!isOpen())
-                return null;
+                throw new ClosedChannelException();
             return localAddress;
         }
     }
 
     @Override
-    public SocketAddress getConnectedAddress() throws IOException {
+    public SocketAddress getRemoteAddress() throws IOException {
         synchronized (stateLock) {
             if (!isOpen())
-                return null;
+                throw new ClosedChannelException();
             return remoteAddress;
         }
     }
 
     @Override
-    public DatagramChannel setOption(SocketOption name, Object value)
+    public <T> DatagramChannel setOption(SocketOption<T> name, T value)
         throws IOException
     {
         if (name == null)
             throw new NullPointerException();
-        if (!options().contains(name))
-            throw new IllegalArgumentException("Invalid option name");
+        if (!supportedOptions().contains(name))
+            throw new UnsupportedOperationException("'" + name + "' not supported");
 
         synchronized (stateLock) {
             ensureOpen();
@@ -224,8 +228,8 @@
     {
         if (name == null)
             throw new NullPointerException();
-        if (!options().contains(name))
-            throw new IllegalArgumentException("Invalid option name");
+        if (!supportedOptions().contains(name))
+            throw new UnsupportedOperationException("'" + name + "' not supported");
 
         synchronized (stateLock) {
             ensureOpen();
@@ -273,7 +277,7 @@
         }
     }
 
-    private static class LazyInitialization {
+    private static class DefaultOptionsHolder {
         static final Set<SocketOption<?>> defaultOptions = defaultOptions();
 
         private static Set<SocketOption<?>> defaultOptions() {
@@ -291,8 +295,8 @@
     }
 
     @Override
-    public final Set<SocketOption<?>> options() {
-        return LazyInitialization.defaultOptions;
+    public final Set<SocketOption<?>> supportedOptions() {
+        return DefaultOptionsHolder.defaultOptions;
     }
 
     private void ensureOpen() throws ClosedChannelException {
@@ -864,23 +868,26 @@
     }
 
     // package-private
-    void drop(MembershipKeyImpl key)
-        throws IOException
-    {
-        assert key.getChannel() == this;
+    void drop(MembershipKeyImpl key) {
+        assert key.channel() == this;
 
         synchronized (stateLock) {
             if (!key.isValid())
                 return;
 
-            if (family == StandardProtocolFamily.INET6) {
-                MembershipKeyImpl.Type6 key6 =
-                    (MembershipKeyImpl.Type6)key;
-                Net.drop6(fd, key6.group(), key6.index(), key6.source());
-            } else {
-                MembershipKeyImpl.Type4 key4 =
-                    (MembershipKeyImpl.Type4)key;
-                Net.drop4(fd, key4.group(), key4.interfaceAddress(), key4.source());
+            try {
+                if (family == StandardProtocolFamily.INET6) {
+                    MembershipKeyImpl.Type6 key6 =
+                        (MembershipKeyImpl.Type6)key;
+                    Net.drop6(fd, key6.groupAddress(), key6.index(), key6.source());
+                } else {
+                    MembershipKeyImpl.Type4 key4 = (MembershipKeyImpl.Type4)key;
+                    Net.drop4(fd, key4.groupAddress(), key4.interfaceAddress(),
+                        key4.source());
+                }
+            } catch (IOException ioe) {
+                // should not happen
+                throw new AssertionError(ioe);
             }
 
             key.invalidate();
@@ -895,8 +902,8 @@
     void block(MembershipKeyImpl key, InetAddress source)
         throws IOException
     {
-        assert key.getChannel() == this;
-        assert key.getSourceAddress() == null;
+        assert key.channel() == this;
+        assert key.sourceAddress() == null;
 
         synchronized (stateLock) {
             if (!key.isValid())
@@ -905,19 +912,19 @@
                 throw new IllegalArgumentException("Source address is a wildcard address");
             if (source.isMulticastAddress())
                 throw new IllegalArgumentException("Source address is multicast address");
-            if (source.getClass() != key.getGroup().getClass())
+            if (source.getClass() != key.group().getClass())
                 throw new IllegalArgumentException("Source address is different type to group");
 
             int n;
             if (family == StandardProtocolFamily.INET6) {
                  MembershipKeyImpl.Type6 key6 =
                     (MembershipKeyImpl.Type6)key;
-                n = Net.block6(fd, key6.group(), key6.index(),
+                n = Net.block6(fd, key6.groupAddress(), key6.index(),
                                Net.inet6AsByteArray(source));
             } else {
                 MembershipKeyImpl.Type4 key4 =
                     (MembershipKeyImpl.Type4)key;
-                n = Net.block4(fd, key4.group(), key4.interfaceAddress(),
+                n = Net.block4(fd, key4.groupAddress(), key4.interfaceAddress(),
                                Net.inet4AsInt(source));
             }
             if (n == IOStatus.UNAVAILABLE) {
@@ -930,26 +937,29 @@
     /**
      * Unblock given source.
      */
-    void unblock(MembershipKeyImpl key, InetAddress source)
-        throws IOException
-    {
-        assert key.getChannel() == this;
-        assert key.getSourceAddress() == null;
+    void unblock(MembershipKeyImpl key, InetAddress source) {
+        assert key.channel() == this;
+        assert key.sourceAddress() == null;
 
         synchronized (stateLock) {
             if (!key.isValid())
                 throw new IllegalStateException("key is no longer valid");
 
-            if (family == StandardProtocolFamily.INET6) {
-                MembershipKeyImpl.Type6 key6 =
-                    (MembershipKeyImpl.Type6)key;
-                Net.unblock6(fd, key6.group(), key6.index(),
-                             Net.inet6AsByteArray(source));
-            } else {
-                MembershipKeyImpl.Type4 key4 =
-                    (MembershipKeyImpl.Type4)key;
-                Net.unblock4(fd, key4.group(), key4.interfaceAddress(),
-                             Net.inet4AsInt(source));
+            try {
+                if (family == StandardProtocolFamily.INET6) {
+                    MembershipKeyImpl.Type6 key6 =
+                        (MembershipKeyImpl.Type6)key;
+                    Net.unblock6(fd, key6.groupAddress(), key6.index(),
+                                 Net.inet6AsByteArray(source));
+                } else {
+                    MembershipKeyImpl.Type4 key4 =
+                        (MembershipKeyImpl.Type4)key;
+                    Net.unblock4(fd, key4.groupAddress(), key4.interfaceAddress(),
+                                 Net.inet4AsInt(source));
+                }
+            } catch (IOException ioe) {
+                // should not happen
+                throw new AssertionError(ioe);
             }
         }
     }