Created
September 30, 2016 16:48
-
-
Save headius/8eba1c78feeea8c76b0ae624839eb7a6 to your computer and use it in GitHub Desktop.
turning jnr-enxio NativeSocketChannel into a SocketChannel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/main/java/jnr/enxio/channels/NativeSocketChannel.java b/src/main/java/jnr/enxio/channels/NativeSocketChannel.java | |
index c97637d..0882ace 100644 | |
--- a/src/main/java/jnr/enxio/channels/NativeSocketChannel.java | |
+++ b/src/main/java/jnr/enxio/channels/NativeSocketChannel.java | |
@@ -20,35 +20,36 @@ package jnr.enxio.channels; | |
import jnr.constants.platform.Shutdown; | |
import java.io.IOException; | |
+import java.net.Socket; | |
+import java.net.SocketAddress; | |
+import java.net.SocketOption; | |
import java.nio.ByteBuffer; | |
import java.nio.channels.ByteChannel; | |
import java.nio.channels.SelectionKey; | |
+import java.nio.channels.SocketChannel; | |
import java.nio.channels.spi.AbstractSelectableChannel; | |
import java.nio.channels.spi.SelectorProvider; | |
+import java.util.Set; | |
-public class NativeSocketChannel extends AbstractSelectableChannel | |
- implements ByteChannel, NativeSelectableChannel { | |
+public class NativeSocketChannel extends SocketChannel | |
+ implements NativeSelectableChannel { | |
private final int fd; | |
- private final int validOps; | |
+ private volatile boolean open = true; | |
public NativeSocketChannel(int fd) { | |
- this(NativeSelectorProvider.getInstance(), fd, SelectionKey.OP_READ | SelectionKey.OP_WRITE); | |
+ this(NativeSelectorProvider.getInstance(), fd); | |
} | |
- public NativeSocketChannel(int fd, int ops) { | |
- this(NativeSelectorProvider.getInstance(), fd, ops); | |
- } | |
- | |
- NativeSocketChannel(SelectorProvider provider, int fd, int ops) { | |
+ NativeSocketChannel(SelectorProvider provider, int fd) { | |
super(provider); | |
this.fd = fd; | |
- this.validOps = ops; | |
} | |
@Override | |
protected void implCloseSelectableChannel() throws IOException { | |
- Native.close(fd); | |
+ open = false; | |
+ Native.close(fd); | |
} | |
@Override | |
@@ -56,10 +57,22 @@ public class NativeSocketChannel extends AbstractSelectableChannel | |
Native.setBlocking(fd, block); | |
} | |
- @Override | |
- public final int validOps() { | |
- return validOps; | |
+ public SocketChannel bind(SocketAddress local) throws IOException { | |
+ throw new UnsupportedOperationException("bind is not supported"); | |
+ } | |
+ | |
+ public <T> SocketChannel setOption(SocketOption<T> name, T value) throws IOException { | |
+ throw new UnsupportedOperationException("setOption is not supported"); | |
} | |
+ | |
+ public <T> T getOption(SocketOption<T> name) throws IOException { | |
+ throw new UnsupportedOperationException("getOption is not supported"); | |
+ } | |
+ | |
+ public Set<SocketOption<?>> supportedOptions() { | |
+ throw new UnsupportedOperationException("supportedOptions is not supported"); | |
+ } | |
+ | |
public final int getFD() { | |
return fd; | |
} | |
@@ -85,6 +98,21 @@ public class NativeSocketChannel extends AbstractSelectableChannel | |
} | |
} | |
+ public long read(ByteBuffer[] dsts, int offset, int length) throws IOException { | |
+ long total = 0; | |
+ | |
+ for (int i = 0; i < length; i++) { | |
+ ByteBuffer dst = dsts[offset + i]; | |
+ long read = read(dst); | |
+ if (read == -1) { | |
+ return read; | |
+ } | |
+ total += read; | |
+ } | |
+ | |
+ return total; | |
+ } | |
+ | |
public int write(ByteBuffer src) throws IOException { | |
int n = Native.write(fd, src); | |
if (n < 0) { | |
@@ -93,21 +121,66 @@ public class NativeSocketChannel extends AbstractSelectableChannel | |
return n; | |
} | |
- | |
- public void shutdownInput() throws IOException { | |
+ | |
+ public long write(ByteBuffer[] srcs, int offset, int length) throws IOException { | |
+ long total = 0; | |
+ | |
+ for (int i = 0; i < length; i++) { | |
+ ByteBuffer src = srcs[offset + i]; | |
+ long write = write(src); | |
+ if (write == -1) { | |
+ return write; | |
+ } | |
+ total += write; | |
+ } | |
+ | |
+ return total; | |
+ } | |
+ | |
+ public SocketAddress getLocalAddress() throws IOException { | |
+ throw new UnsupportedOperationException("getLocalAddress is not supported"); | |
+ } | |
+ | |
+ public SocketChannel shutdownInput() throws IOException { | |
int n = Native.shutdown(fd, SHUT_RD); | |
if (n < 0) { | |
throw new IOException(Native.getLastErrorString()); | |
} | |
+ return this; | |
} | |
- public void shutdownOutput() throws IOException { | |
+ public SocketChannel shutdownOutput() throws IOException { | |
int n = Native.shutdown(fd, SHUT_WR); | |
if (n < 0) { | |
throw new IOException(Native.getLastErrorString()); | |
} | |
+ return this; | |
} | |
- | |
+ | |
+ public Socket socket() { | |
+ throw new UnsupportedOperationException("socket is not supported"); | |
+ } | |
+ | |
+ public boolean isConnected() { | |
+ return open; | |
+ } | |
+ | |
+ public boolean isConnectionPending() { | |
+ return false; | |
+ } | |
+ | |
+ public boolean connect(SocketAddress remote) throws IOException { | |
+ return true; | |
+ } | |
+ | |
+ public boolean finishConnect() throws IOException { | |
+ return true; | |
+ } | |
+ | |
+ public SocketAddress getRemoteAddress() throws IOException { | |
+ throw new UnsupportedOperationException("getRemoteAddress is not supported"); | |
+ } | |
+ | |
private final static int SHUT_RD = Shutdown.SHUT_RD.intValue(); | |
private final static int SHUT_WR = Shutdown.SHUT_WR.intValue(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment