1466 *
1467 * @throws IOException if an I/O error occurs, or if the socket is closed.
1468 *
1469 * @throws NullPointerException if name is {@code null}
1470 *
1471 * @throws SecurityException if a security manager is set and if the socket
1472 * option requires a security permission and if the caller does
1473 * not have the required permission.
1474 * {@link java.net.StandardSocketOptions StandardSocketOptions}
1475 * do not require any security permission.
1476 *
1477 * @since 9
1478 */
1479 public <T> T getOption(SocketOption<T> name) throws IOException {
1480 Objects.requireNonNull(name);
1481 if (isClosed())
1482 throw new SocketException("Socket is closed");
1483 return getImpl().getOption(name);
1484 }
1485
1486 private static Set<SocketOption<?>> options;
1487 private static boolean optionsSet = false;
1488
1489 /**
1490 * Returns a set of the socket options supported by this socket.
1491 *
1492 * This method will continue to return the set of options even after
1493 * the socket has been closed.
1494 *
1495 * @return A set of the socket options supported by this socket. This set
1496 * may be empty if the socket's DatagramSocketImpl cannot be created.
1497 *
1498 * @since 9
1499 */
1500 public Set<SocketOption<?>> supportedOptions() {
1501 synchronized(DatagramSocket.class) {
1502 if (optionsSet) {
1503 return options;
1504 }
1505 try {
1506 DatagramSocketImpl impl = getImpl();
1507 options = Collections.unmodifiableSet(impl.supportedOptions());
1508 } catch (IOException e) {
1509 options = Collections.emptySet();
1510 }
1511 optionsSet = true;
1512 return options;
1513 }
1514 }
1515 }
|
1466 *
1467 * @throws IOException if an I/O error occurs, or if the socket is closed.
1468 *
1469 * @throws NullPointerException if name is {@code null}
1470 *
1471 * @throws SecurityException if a security manager is set and if the socket
1472 * option requires a security permission and if the caller does
1473 * not have the required permission.
1474 * {@link java.net.StandardSocketOptions StandardSocketOptions}
1475 * do not require any security permission.
1476 *
1477 * @since 9
1478 */
1479 public <T> T getOption(SocketOption<T> name) throws IOException {
1480 Objects.requireNonNull(name);
1481 if (isClosed())
1482 throw new SocketException("Socket is closed");
1483 return getImpl().getOption(name);
1484 }
1485
1486 private volatile Set<SocketOption<?>> options;
1487 private final Object optionsLock = new Object();
1488
1489 /**
1490 * Returns a set of the socket options supported by this socket.
1491 *
1492 * This method will continue to return the set of options even after
1493 * the socket has been closed.
1494 *
1495 * @return A set of the socket options supported by this socket. This set
1496 * may be empty if the socket's DatagramSocketImpl cannot be created.
1497 *
1498 * @since 9
1499 */
1500 public Set<SocketOption<?>> supportedOptions() {
1501 Set<SocketOption<?>> options = this.options;
1502 if (options != null)
1503 return options;
1504
1505 synchronized (optionsLock) {
1506 options = this.options;
1507 if (options != null)
1508 return options;
1509
1510 try {
1511 DatagramSocketImpl impl = getImpl();
1512 options = Collections.unmodifiableSet(impl.supportedOptions());
1513 } catch (IOException e) {
1514 options = Collections.emptySet();
1515 }
1516 return this.options = options;
1517 }
1518 }
1519 }
|