2424import java .nio .channels .NotYetBoundException ;
2525import java .nio .channels .NotYetConnectedException ;
2626import java .nio .channels .spi .AbstractInterruptibleChannel ;
27-
28- import org .eclipse .jdt .annotation .NonNull ;
27+ import java .util .Objects ;
2928
3029/**
3130 * Helper methods when working with {@link AbstractInterruptibleChannel} subclasses.
@@ -53,8 +52,8 @@ interface EndMethod {
5352 * @param exception An optional exception that was caught in the try-catch-finally block.
5453 * @throws AsynchronousCloseException on error.
5554 */
56- static void endInterruptable (AbstractInterruptibleChannel channel , EndMethod end ,
57- boolean complete , Exception exception ) throws AsynchronousCloseException {
55+ static void endInterruptable (AFSomeSocketChannel channel , EndMethod end , boolean complete ,
56+ Exception exception ) throws AsynchronousCloseException {
5857 if (!complete ) {
5958 if (exception instanceof ClosedChannelException ) {
6059 // we already have caught a valid exception; we don't need to throw one from within "end"
@@ -68,8 +67,8 @@ static void endInterruptable(AbstractInterruptibleChannel channel, EndMethod end
6867 }
6968 }
7069
71- private static <T extends Exception > T closeAndThrow (AbstractInterruptibleChannel channel ,
72- @ NonNull T exc ) {
70+ private static <T extends Exception > T closeAndThrow (AFSomeSocketChannel channel , T exc ) {
71+ Objects . requireNonNull ( exc );
7372 if (channel .isOpen ()) {
7473 try {
7574 channel .close ();
@@ -99,14 +98,27 @@ static IOException ioExceptionOrThrowRuntimeException(Exception exception) {
9998 * @param e The exception
10099 * @return The exception.
101100 */
102- @ SuppressWarnings ("null " )
103- static Exception handleException (AbstractInterruptibleChannel channel , IOException e ) {
101+ @ SuppressWarnings ("PMD.CognitiveComplexity " )
102+ static Exception handleException (AFSomeSocketChannel channel , IOException e ) {
104103 if (e instanceof NotConnectedSocketException ) {
105104 return (NotYetConnectedException ) new NotYetConnectedException ().initCause (e );
106105 } else if (e instanceof NotBoundSocketException ) {
107106 return (NotYetBoundException ) new NotYetBoundException ().initCause (e );
108107 }
109108
109+ if (e instanceof InvalidArgumentSocketException ) {
110+ if (channel instanceof AFServerSocketChannel <?>) {
111+ AFServerSocketChannel <?> sc = (AFServerSocketChannel <?>) channel ;
112+ if (!sc .socket ().isBound ()) {
113+ return (NotYetBoundException ) new NotYetBoundException ().initCause (e );
114+ }
115+ } else if (channel instanceof AFSocketChannel <?>) {
116+ if (!((AFSocketChannel <?>) channel ).socket ().isConnected ()) {
117+ return (NotYetConnectedException ) new NotYetConnectedException ().initCause (e );
118+ }
119+ }
120+ }
121+
110122 if (e instanceof SocketClosedException || e instanceof ClosedChannelException
111123 || e instanceof BrokenPipeSocketException ) {
112124 Thread t = Thread .currentThread ();
0 commit comments