@Override
  @SuppressWarnings("unchecked")
  public <T> T getOption(SocketOption<T> name) throws IOException {
    if (name == null) throw new NullPointerException();
    if (!supportedOptions().contains(name))
      throw new UnsupportedOperationException("'" + name + "' not supported");

    synchronized (stateLock) {
      ensureOpen();

      if (name == StandardSocketOptions.IP_TOS
          || name == StandardSocketOptions.IP_MULTICAST_TTL
          || name == StandardSocketOptions.IP_MULTICAST_LOOP) {
        return (T) Net.getSocketOption(fd, family, name);
      }

      if (name == StandardSocketOptions.IP_MULTICAST_IF) {
        if (family == StandardProtocolFamily.INET) {
          int address = Net.getInterface4(fd);
          if (address == 0) return null; // default interface

          InetAddress ia = Net.inet4FromInt(address);
          NetworkInterface ni = NetworkInterface.getByInetAddress(ia);
          if (ni == null) throw new IOException("Unable to map address to interface");
          return (T) ni;
        } else {
          int index = Net.getInterface6(fd);
          if (index == 0) return null; // default interface

          NetworkInterface ni = NetworkInterface.getByIndex(index);
          if (ni == null) throw new IOException("Unable to map index to interface");
          return (T) ni;
        }
      }

      if (name == StandardSocketOptions.SO_REUSEADDR && reuseAddressEmulated) {
        return (T) Boolean.valueOf(isReuseAddress);
      }

      // no special handling
      return (T) Net.getSocketOption(fd, Net.UNSPEC, name);
    }
  }
Пример #2
0
  /**
   * Returns boolean value from system property or provided function.
   *
   * @param propName System property host.
   * @param dflt Function that returns {@code Boolean}.
   * @return {@code Boolean} value
   */
  public static boolean boolValue(String propName, boolean dflt) {
    String sysProp = getProperty(propName);

    return (sysProp != null && !sysProp.isEmpty()) ? Boolean.getBoolean(sysProp) : dflt;
  }