429 throw new IllegalArgumentException("Invalid value '" + value + "'");
430
431 if (isClosedOrPending())
432 throw new SocketException("Socket closed");
433
434 if (name == StandardSocketOptions.SO_KEEPALIVE) {
435 setOption(SocketOptions.SO_KEEPALIVE, value);
436 } else if (name == StandardSocketOptions.SO_SNDBUF) {
437 if (((Integer)value).intValue() < 0)
438 throw new IllegalArgumentException("Invalid send buffer size:" + value);
439 setOption(SocketOptions.SO_SNDBUF, value);
440 } else if (name == StandardSocketOptions.SO_RCVBUF) {
441 if (((Integer)value).intValue() < 0)
442 throw new IllegalArgumentException("Invalid recv buffer size:" + value);
443 setOption(SocketOptions.SO_RCVBUF, value);
444 } else if (name == StandardSocketOptions.SO_REUSEADDR) {
445 setOption(SocketOptions.SO_REUSEADDR, value);
446 } else if (name == StandardSocketOptions.SO_REUSEPORT) {
447 setOption(SocketOptions.SO_REUSEPORT, value);
448 } else if (name == StandardSocketOptions.SO_LINGER ) {
449 setOption(SocketOptions.SO_LINGER, value);
450 } else if (name == StandardSocketOptions.IP_TOS) {
451 int i = ((Integer)value).intValue();
452 if (i < 0 || i > 255)
453 throw new IllegalArgumentException("Invalid IP_TOS value: " + value);
454 setOption(SocketOptions.IP_TOS, value);
455 } else if (name == StandardSocketOptions.TCP_NODELAY) {
456 setOption(SocketOptions.TCP_NODELAY, value);
457 } else if (extendedOptions.isOptionSupported(name)) {
458 extendedOptions.setOption(fd, name, value);
459 } else {
460 throw new AssertionError("unknown option: " + name);
461 }
462 }
463
464 @Override
465 @SuppressWarnings("unchecked")
466 protected <T> T getOption(SocketOption<T> name) throws IOException {
467 Objects.requireNonNull(name);
468 if (!supportedOptions().contains(name))
469 throw new UnsupportedOperationException("'" + name + "' not supported");
470
471 if (isClosedOrPending())
472 throw new SocketException("Socket closed");
473
474 if (name == StandardSocketOptions.SO_KEEPALIVE) {
475 return (T)getOption(SocketOptions.SO_KEEPALIVE);
476 } else if (name == StandardSocketOptions.SO_SNDBUF) {
477 return (T)getOption(SocketOptions.SO_SNDBUF);
478 } else if (name == StandardSocketOptions.SO_RCVBUF) {
479 return (T)getOption(SocketOptions.SO_RCVBUF);
480 } else if (name == StandardSocketOptions.SO_REUSEADDR) {
481 return (T)getOption(SocketOptions.SO_REUSEADDR);
482 } else if (name == StandardSocketOptions.SO_REUSEPORT) {
483 return (T)getOption(SocketOptions.SO_REUSEPORT);
484 } else if (name == StandardSocketOptions.SO_LINGER) {
485 return (T)getOption(SocketOptions.SO_LINGER);
486 } else if (name == StandardSocketOptions.IP_TOS) {
487 return (T)getOption(SocketOptions.IP_TOS);
488 } else if (name == StandardSocketOptions.TCP_NODELAY) {
489 return (T)getOption(SocketOptions.TCP_NODELAY);
490 } else if (extendedOptions.isOptionSupported(name)) {
491 return (T) extendedOptions.getOption(fd, name);
492 } else {
493 throw new AssertionError("unknown option: " + name);
494 }
495 }
496
497 /**
498 * The workhorse of the connection operation. Tries several times to
499 * establish a connection to the given <host, port>. If unsuccessful,
500 * throws an IOException indicating what went wrong.
501 */
502
503 synchronized void doConnect(InetAddress address, int port, int timeout) throws IOException {
504 synchronized (fdLock) {
505 if (!closePending && !isBound) {
|
429 throw new IllegalArgumentException("Invalid value '" + value + "'");
430
431 if (isClosedOrPending())
432 throw new SocketException("Socket closed");
433
434 if (name == StandardSocketOptions.SO_KEEPALIVE) {
435 setOption(SocketOptions.SO_KEEPALIVE, value);
436 } else if (name == StandardSocketOptions.SO_SNDBUF) {
437 if (((Integer)value).intValue() < 0)
438 throw new IllegalArgumentException("Invalid send buffer size:" + value);
439 setOption(SocketOptions.SO_SNDBUF, value);
440 } else if (name == StandardSocketOptions.SO_RCVBUF) {
441 if (((Integer)value).intValue() < 0)
442 throw new IllegalArgumentException("Invalid recv buffer size:" + value);
443 setOption(SocketOptions.SO_RCVBUF, value);
444 } else if (name == StandardSocketOptions.SO_REUSEADDR) {
445 setOption(SocketOptions.SO_REUSEADDR, value);
446 } else if (name == StandardSocketOptions.SO_REUSEPORT) {
447 setOption(SocketOptions.SO_REUSEPORT, value);
448 } else if (name == StandardSocketOptions.SO_LINGER ) {
449 if (((Integer)value).intValue() < 0)
450 setOption(SocketOptions.SO_LINGER, false);
451 else
452 setOption(SocketOptions.SO_LINGER, value);
453 } else if (name == StandardSocketOptions.IP_TOS) {
454 int i = ((Integer)value).intValue();
455 if (i < 0 || i > 255)
456 throw new IllegalArgumentException("Invalid IP_TOS value: " + value);
457 setOption(SocketOptions.IP_TOS, value);
458 } else if (name == StandardSocketOptions.TCP_NODELAY) {
459 setOption(SocketOptions.TCP_NODELAY, value);
460 } else if (extendedOptions.isOptionSupported(name)) {
461 extendedOptions.setOption(fd, name, value);
462 } else {
463 throw new AssertionError("unknown option: " + name);
464 }
465 }
466
467 @Override
468 @SuppressWarnings("unchecked")
469 protected <T> T getOption(SocketOption<T> name) throws IOException {
470 Objects.requireNonNull(name);
471 if (!supportedOptions().contains(name))
472 throw new UnsupportedOperationException("'" + name + "' not supported");
473
474 if (isClosedOrPending())
475 throw new SocketException("Socket closed");
476
477 if (name == StandardSocketOptions.SO_KEEPALIVE) {
478 return (T)getOption(SocketOptions.SO_KEEPALIVE);
479 } else if (name == StandardSocketOptions.SO_SNDBUF) {
480 return (T)getOption(SocketOptions.SO_SNDBUF);
481 } else if (name == StandardSocketOptions.SO_RCVBUF) {
482 return (T)getOption(SocketOptions.SO_RCVBUF);
483 } else if (name == StandardSocketOptions.SO_REUSEADDR) {
484 return (T)getOption(SocketOptions.SO_REUSEADDR);
485 } else if (name == StandardSocketOptions.SO_REUSEPORT) {
486 return (T)getOption(SocketOptions.SO_REUSEPORT);
487 } else if (name == StandardSocketOptions.SO_LINGER) {
488 Object value = getOption(SocketOptions.SO_LINGER);
489 if (value instanceof Boolean) {
490 assert ((Boolean)value).booleanValue() == false;
491 value = -1;
492 }
493 return (T)value;
494 } else if (name == StandardSocketOptions.IP_TOS) {
495 return (T)getOption(SocketOptions.IP_TOS);
496 } else if (name == StandardSocketOptions.TCP_NODELAY) {
497 return (T)getOption(SocketOptions.TCP_NODELAY);
498 } else if (extendedOptions.isOptionSupported(name)) {
499 return (T) extendedOptions.getOption(fd, name);
500 } else {
501 throw new AssertionError("unknown option: " + name);
502 }
503 }
504
505 /**
506 * The workhorse of the connection operation. Tries several times to
507 * establish a connection to the given <host, port>. If unsuccessful,
508 * throws an IOException indicating what went wrong.
509 */
510
511 synchronized void doConnect(InetAddress address, int port, int timeout) throws IOException {
512 synchronized (fdLock) {
513 if (!closePending && !isBound) {
|