jdk/src/share/classes/sun/nio/ch/SocketChannelImpl.java
changeset 2057 3acf8e5e2ca0
parent 1247 b4c26443dee5
child 2446 07047237e4d4
equal deleted inserted replaced
2056:115e09b7a004 2057:3acf8e5e2ca0
     1 /*
     1 /*
     2  * Copyright 2000-2008 Sun Microsystems, Inc.  All Rights Reserved.
     2  * Copyright 2000-2009 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Sun designates this
     7  * published by the Free Software Foundation.  Sun designates this
   126 
   126 
   127     @Override
   127     @Override
   128     public SocketAddress getLocalAddress() throws IOException {
   128     public SocketAddress getLocalAddress() throws IOException {
   129         synchronized (stateLock) {
   129         synchronized (stateLock) {
   130             if (!isOpen())
   130             if (!isOpen())
   131                 return null;
   131                 throw new ClosedChannelException();
   132             return localAddress;
   132             return localAddress;
   133         }
   133         }
   134     }
   134     }
   135 
   135 
   136     @Override
   136     @Override
   137     public SocketAddress getConnectedAddress() throws IOException {
   137     public SocketAddress getRemoteAddress() throws IOException {
   138         synchronized (stateLock) {
   138         synchronized (stateLock) {
   139             if (!isOpen())
   139             if (!isOpen())
   140                 return null;
   140                 throw new ClosedChannelException();
   141             return remoteAddress;
   141             return remoteAddress;
   142         }
   142         }
   143     }
   143     }
   144 
   144 
   145     @Override
   145     @Override
   146     public SocketChannel setOption(SocketOption name, Object value)
   146     public <T> SocketChannel setOption(SocketOption<T> name, T value)
   147         throws IOException
   147         throws IOException
   148     {
   148     {
   149         if (name == null)
   149         if (name == null)
   150             throw new NullPointerException();
   150             throw new NullPointerException();
   151         if (!options().contains(name))
   151         if (!supportedOptions().contains(name))
   152             throw new IllegalArgumentException("Invalid option name");
   152             throw new UnsupportedOperationException("'" + name + "' not supported");
   153 
   153 
   154         synchronized (stateLock) {
   154         synchronized (stateLock) {
   155             if (!isOpen())
   155             if (!isOpen())
   156                 throw new ClosedChannelException();
   156                 throw new ClosedChannelException();
   157 
   157 
   173     public <T> T getOption(SocketOption<T> name)
   173     public <T> T getOption(SocketOption<T> name)
   174         throws IOException
   174         throws IOException
   175     {
   175     {
   176         if (name == null)
   176         if (name == null)
   177             throw new NullPointerException();
   177             throw new NullPointerException();
   178         if (!options().contains(name))
   178         if (!supportedOptions().contains(name))
   179             throw new IllegalArgumentException("Invalid option name");
   179             throw new UnsupportedOperationException("'" + name + "' not supported");
   180 
   180 
   181         synchronized (stateLock) {
   181         synchronized (stateLock) {
   182             if (!isOpen())
   182             if (!isOpen())
   183                 throw new ClosedChannelException();
   183                 throw new ClosedChannelException();
   184 
   184 
   191             // no options that require special handling
   191             // no options that require special handling
   192             return (T) Net.getSocketOption(fd, Net.UNSPEC, name);
   192             return (T) Net.getSocketOption(fd, Net.UNSPEC, name);
   193         }
   193         }
   194     }
   194     }
   195 
   195 
   196     private static class LazyInitialization {
   196     private static class DefaultOptionsHolder {
   197         static final Set<SocketOption<?>> defaultOptions = defaultOptions();
   197         static final Set<SocketOption<?>> defaultOptions = defaultOptions();
   198 
   198 
   199         private static Set<SocketOption<?>> defaultOptions() {
   199         private static Set<SocketOption<?>> defaultOptions() {
   200             HashSet<SocketOption<?>> set = new HashSet<SocketOption<?>>(8);
   200             HashSet<SocketOption<?>> set = new HashSet<SocketOption<?>>(8);
   201             set.add(StandardSocketOption.SO_SNDBUF);
   201             set.add(StandardSocketOption.SO_SNDBUF);
   210             return Collections.unmodifiableSet(set);
   210             return Collections.unmodifiableSet(set);
   211         }
   211         }
   212     }
   212     }
   213 
   213 
   214     @Override
   214     @Override
   215     public final Set<SocketOption<?>> options() {
   215     public final Set<SocketOption<?>> supportedOptions() {
   216         return LazyInitialization.defaultOptions;
   216         return DefaultOptionsHolder.defaultOptions;
   217     }
   217     }
   218 
   218 
   219     private boolean ensureReadOpen() throws ClosedChannelException {
   219     private boolean ensureReadOpen() throws ClosedChannelException {
   220         synchronized (stateLock) {
   220         synchronized (stateLock) {
   221             if (!isOpen())
   221             if (!isOpen())