diff --git a/junixsocket-common/src/main/java/org/newsclub/net/unix/AFDatagramChannel.java b/junixsocket-common/src/main/java/org/newsclub/net/unix/AFDatagramChannel.java
index 9e4d7abd0..405410cb4 100644
--- a/junixsocket-common/src/main/java/org/newsclub/net/unix/AFDatagramChannel.java
+++ b/junixsocket-common/src/main/java/org/newsclub/net/unix/AFDatagramChannel.java
@@ -143,14 +143,15 @@ public final AFDatagramChannel disconnect() throws IOException {
@Override
public final A receive(ByteBuffer dst) throws IOException {
boolean complete = false;
- IOException exception = null;
+ Exception exception = null;
try {
begin();
A ret = afSocket.getAFImpl().receive(dst);
complete = true;
return ret;
} catch (IOException e) {
- throw (exception = InterruptibleChannelUtil.handleException(this, e)); // NOPMD.PreserveStackTrace
+ throw InterruptibleChannelUtil.ioExceptionOrThrowRuntimeException( // NOPMD.PreserveStackTrace
+ (exception = InterruptibleChannelUtil.handleException(this, e)));
} finally {
InterruptibleChannelUtil.endInterruptable(this, this::end, complete, exception);
}
@@ -159,14 +160,15 @@ public final A receive(ByteBuffer dst) throws IOException {
@Override
public final int send(ByteBuffer src, SocketAddress target) throws IOException {
boolean complete = false;
- IOException exception = null;
+ Exception exception = null;
try {
begin();
int ret = afSocket.getAFImpl().send(src, target);
complete = true;
return ret;
} catch (IOException e) {
- throw (exception = InterruptibleChannelUtil.handleException(this, e)); // NOPMD.PreserveStackTrace
+ throw InterruptibleChannelUtil.ioExceptionOrThrowRuntimeException( // NOPMD.PreserveStackTrace
+ (exception = InterruptibleChannelUtil.handleException(this, e)));
} finally {
InterruptibleChannelUtil.endInterruptable(this, this::end, complete, exception);
}
@@ -175,14 +177,15 @@ public final int send(ByteBuffer src, SocketAddress target) throws IOException {
@Override
public final int read(ByteBuffer dst) throws IOException {
boolean complete = false;
- IOException exception = null;
+ Exception exception = null;
try {
begin();
int ret = afSocket.getAFImpl().read(dst, null);
complete = true;
return ret;
} catch (IOException e) {
- throw (exception = InterruptibleChannelUtil.handleException(this, e)); // NOPMD.PreserveStackTrace
+ throw InterruptibleChannelUtil.ioExceptionOrThrowRuntimeException( // NOPMD.PreserveStackTrace
+ (exception = InterruptibleChannelUtil.handleException(this, e)));
} finally {
InterruptibleChannelUtil.endInterruptable(this, this::end, complete, exception);
}
@@ -200,14 +203,15 @@ public final long read(ByteBuffer[] dsts, int offset, int length) throws IOExcep
@Override
public final int write(ByteBuffer src) throws IOException {
boolean complete = false;
- IOException exception = null;
+ Exception exception = null;
try {
begin();
int ret = afSocket.getAFImpl().write(src);
complete = true;
return ret;
} catch (IOException e) {
- throw (exception = InterruptibleChannelUtil.handleException(this, e)); // NOPMD.PreserveStackTrace
+ throw InterruptibleChannelUtil.ioExceptionOrThrowRuntimeException( // NOPMD.PreserveStackTrace
+ (exception = InterruptibleChannelUtil.handleException(this, e)));
} finally {
InterruptibleChannelUtil.endInterruptable(this, this::end, complete, exception);
}
diff --git a/junixsocket-common/src/main/java/org/newsclub/net/unix/AFServerSocketChannel.java b/junixsocket-common/src/main/java/org/newsclub/net/unix/AFServerSocketChannel.java
index b061f8d57..2fafff00d 100644
--- a/junixsocket-common/src/main/java/org/newsclub/net/unix/AFServerSocketChannel.java
+++ b/junixsocket-common/src/main/java/org/newsclub/net/unix/AFServerSocketChannel.java
@@ -111,14 +111,15 @@ public final AFServerSocket socket() {
@Override
public AFSocketChannel accept() throws IOException {
boolean complete = false;
- IOException exception = null;
+ Exception exception = null;
try {
begin();
AFSocket socket = afSocket.accept1(false);
complete = true;
return socket == null ? null : socket.getChannel();
} catch (IOException e) {
- throw (exception = InterruptibleChannelUtil.handleException(this, e)); // NOPMD.PreserveStackTrace
+ throw InterruptibleChannelUtil.ioExceptionOrThrowRuntimeException( // NOPMD.PreserveStackTrace
+ (exception = InterruptibleChannelUtil.handleException(this, e)));
} finally {
InterruptibleChannelUtil.endInterruptable(this, this::end, complete, exception);
}
diff --git a/junixsocket-common/src/main/java/org/newsclub/net/unix/AFSocketChannel.java b/junixsocket-common/src/main/java/org/newsclub/net/unix/AFSocketChannel.java
index 6b134f3fc..e971bc759 100644
--- a/junixsocket-common/src/main/java/org/newsclub/net/unix/AFSocketChannel.java
+++ b/junixsocket-common/src/main/java/org/newsclub/net/unix/AFSocketChannel.java
@@ -200,7 +200,7 @@ public final boolean isConnectionPending() {
@Override
public final boolean connect(SocketAddress remote) throws IOException {
boolean complete = false;
- IOException exception = null;
+ Exception exception = null;
try {
begin();
boolean connected = afSocket.connect0(remote, 0);
@@ -210,7 +210,8 @@ public final boolean connect(SocketAddress remote) throws IOException {
complete = true;
return connected;
} catch (IOException e) {
- throw (exception = InterruptibleChannelUtil.handleException(this, e)); // NOPMD.PreserveStackTrace
+ throw InterruptibleChannelUtil.ioExceptionOrThrowRuntimeException( // NOPMD.PreserveStackTrace
+ (exception = InterruptibleChannelUtil.handleException(this, e)));
} finally {
InterruptibleChannelUtil.endInterruptable(this, this::end, complete, exception);
}
@@ -225,7 +226,7 @@ public final boolean finishConnect() throws IOException {
}
boolean complete = false;
- IOException exception = null;
+ Exception exception = null;
try {
begin();
boolean connected = NativeUnixSocket.finishConnect(afSocket.getFileDescriptor())
@@ -236,7 +237,8 @@ public final boolean finishConnect() throws IOException {
complete = true;
return connected;
} catch (IOException e) {
- throw (exception = InterruptibleChannelUtil.handleException(this, e)); // NOPMD.PreserveStackTrace
+ throw InterruptibleChannelUtil.ioExceptionOrThrowRuntimeException( // NOPMD.PreserveStackTrace
+ (exception = InterruptibleChannelUtil.handleException(this, e)));
} finally {
InterruptibleChannelUtil.endInterruptable(this, this::end, complete, exception);
}
@@ -255,14 +257,15 @@ public final A getRemoteSocketAddress() {
@Override
public final int read(ByteBuffer dst) throws IOException {
boolean complete = false;
- IOException exception = null;
+ Exception exception = null;
try {
begin();
int read = afSocket.getAFImpl().read(dst, null);
complete = true;
return read;
} catch (IOException e) {
- throw (exception = InterruptibleChannelUtil.handleException(this, e)); // NOPMD.PreserveStackTrace
+ throw InterruptibleChannelUtil.ioExceptionOrThrowRuntimeException( // NOPMD.PreserveStackTrace
+ (exception = InterruptibleChannelUtil.handleException(this, e)));
} finally {
InterruptibleChannelUtil.endInterruptable(this, this::end, complete, exception);
}
@@ -289,14 +292,15 @@ public final long write(ByteBuffer[] srcs, int offset, int length) throws IOExce
@Override
public final int write(ByteBuffer src) throws IOException {
boolean complete = false;
- IOException exception = null;
+ Exception exception = null;
try {
begin();
int written = afSocket.getAFImpl().write(src);
complete = true;
return written;
} catch (IOException e) {
- throw (exception = InterruptibleChannelUtil.handleException(this, e)); // NOPMD.PreserveStackTrace
+ throw InterruptibleChannelUtil.ioExceptionOrThrowRuntimeException( // NOPMD.PreserveStackTrace
+ (exception = InterruptibleChannelUtil.handleException(this, e)));
} finally {
InterruptibleChannelUtil.endInterruptable(this, this::end, complete, exception);
}
diff --git a/junixsocket-common/src/main/java/org/newsclub/net/unix/InterruptibleChannelUtil.java b/junixsocket-common/src/main/java/org/newsclub/net/unix/InterruptibleChannelUtil.java
index c07f448cf..f04c44bde 100644
--- a/junixsocket-common/src/main/java/org/newsclub/net/unix/InterruptibleChannelUtil.java
+++ b/junixsocket-common/src/main/java/org/newsclub/net/unix/InterruptibleChannelUtil.java
@@ -21,6 +21,8 @@
import java.nio.channels.AsynchronousCloseException;
import java.nio.channels.ClosedByInterruptException;
import java.nio.channels.ClosedChannelException;
+import java.nio.channels.NotYetBoundException;
+import java.nio.channels.NotYetConnectedException;
import java.nio.channels.spi.AbstractInterruptibleChannel;
import org.eclipse.jdt.annotation.NonNull;
@@ -52,7 +54,7 @@ interface EndMethod {
* @throws AsynchronousCloseException on error.
*/
static void endInterruptable(AbstractInterruptibleChannel channel, EndMethod end,
- boolean complete, IOException exception) throws AsynchronousCloseException {
+ boolean complete, Exception exception) throws AsynchronousCloseException {
if (!complete) {
if (exception instanceof ClosedChannelException) {
// we already have caught a valid exception; we don't need to throw one from within "end"
@@ -78,6 +80,16 @@ private static T closeAndThrow(AbstractInterruptibleChanne
return exc;
}
+ static IOException ioExceptionOrThrowRuntimeException(Exception exception) {
+ if (exception instanceof IOException) {
+ return (IOException) exception;
+ } else if (exception instanceof RuntimeException) {
+ throw (RuntimeException) exception;
+ } else {
+ throw new IllegalStateException(exception);
+ }
+ }
+
/**
* Makes sure that upon an exception that is documented to have the channel be closed the channel
* is indeed closed before throwing that exception. If the exception is also documented to have
@@ -88,7 +100,13 @@ private static T closeAndThrow(AbstractInterruptibleChanne
* @return The exception.
*/
@SuppressWarnings("null")
- static IOException handleException(AbstractInterruptibleChannel channel, IOException e) {
+ static Exception handleException(AbstractInterruptibleChannel channel, IOException e) {
+ if (e instanceof NotConnectedSocketException) {
+ return (NotYetConnectedException) new NotYetConnectedException().initCause(e);
+ } else if (e instanceof NotBoundSocketException) {
+ return (NotYetBoundException) new NotYetBoundException().initCause(e);
+ }
+
if (e instanceof SocketClosedException || e instanceof ClosedChannelException
|| e instanceof BrokenPipeSocketException) {
Thread t = Thread.currentThread();