From 8b14afd605c504673fb13994353de270593b6cb1 Mon Sep 17 00:00:00 2001 From: zzz Date: Mon, 20 Jul 2015 14:44:22 +0000 Subject: [PATCH] Add SSLSocketChannel wrappers after review Requires Java 7 to compile --- .../net/i2p/sam/SSLServerSocketChannel.java | 77 ++++++++++ .../src/net/i2p/sam/SSLSocketChannel.java | 140 ++++++++++++++++++ 2 files changed, 217 insertions(+) create mode 100644 apps/sam/java/src/net/i2p/sam/SSLServerSocketChannel.java create mode 100644 apps/sam/java/src/net/i2p/sam/SSLSocketChannel.java diff --git a/apps/sam/java/src/net/i2p/sam/SSLServerSocketChannel.java b/apps/sam/java/src/net/i2p/sam/SSLServerSocketChannel.java new file mode 100644 index 000000000..f92f43e89 --- /dev/null +++ b/apps/sam/java/src/net/i2p/sam/SSLServerSocketChannel.java @@ -0,0 +1,77 @@ +package net.i2p.sam; + +import java.io.IOException; +import java.net.ServerSocket; +import java.net.SocketAddress; +/* requires Java 7 */ +import java.net.SocketOption; +import java.nio.ByteBuffer; +import java.nio.channels.SocketChannel; +import java.nio.channels.ServerSocketChannel; +import java.nio.channels.spi.SelectorProvider; +import java.util.Collections; +import java.util.Set; + +import javax.net.ssl.SSLServerSocket; +import javax.net.ssl.SSLSocket; + +/** + * Simple wrapper for a SSLServerSocket. + * Cannot be used for asynch ops. + * + * @since 0.9.22 + */ +class SSLServerSocketChannel extends ServerSocketChannel { + + private final SSLServerSocket _socket; + + public SSLServerSocketChannel(SSLServerSocket socket) { + super(SelectorProvider.provider()); + _socket = socket; + } + + //// ServerSocketChannel abstract methods + + public SocketChannel accept() throws IOException { + return new SSLSocketChannel((SSLSocket)_socket.accept()); + } + + public ServerSocket socket() { + return _socket; + } + + /** requires Java 7 */ + public ServerSocketChannel bind(SocketAddress local, int backlog) { + throw new UnsupportedOperationException(); + } + + /** requires Java 7 */ + public ServerSocketChannel setOption(SocketOption name, T value) { + return this; + } + + //// AbstractSelectableChannel abstract methods + + public void implCloseSelectableChannel() throws IOException { + _socket.close(); + } + + public void implConfigureBlocking(boolean block) throws IOException { + if (!block) + throw new UnsupportedOperationException(); + } + + //// NetworkChannel interface methods + + public SocketAddress getLocalAddress() { + return _socket.getLocalSocketAddress(); + } + + public T getOption(SocketOption name) { + return null; + } + + public Set> supportedOptions() { + return Collections.emptySet(); + } +} diff --git a/apps/sam/java/src/net/i2p/sam/SSLSocketChannel.java b/apps/sam/java/src/net/i2p/sam/SSLSocketChannel.java new file mode 100644 index 000000000..9530b396d --- /dev/null +++ b/apps/sam/java/src/net/i2p/sam/SSLSocketChannel.java @@ -0,0 +1,140 @@ +package net.i2p.sam; + +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketAddress; +/* requires Java 7 */ +import java.net.SocketOption; +import java.nio.ByteBuffer; +import java.nio.channels.SocketChannel; +import java.nio.channels.spi.SelectorProvider; +import java.util.Collections; +import java.util.Set; + +import javax.net.ssl.SSLSocket; + +/** + * Simple wrapper for a SSLSocket. + * Cannot be used for asynch ops. + * + * @since 0.9.22 + */ +class SSLSocketChannel extends SocketChannel { + + private final SSLSocket _socket; + + public SSLSocketChannel(SSLSocket socket) { + super(SelectorProvider.provider()); + _socket = socket; + } + + //// SocketChannel abstract methods + + public Socket socket() { + return _socket; + } + + public boolean connect(SocketAddress remote) { + throw new UnsupportedOperationException(); + } + + public boolean finishConnect() { + return true; + } + + public boolean isConnected() { + return _socket.isConnected(); + } + + public boolean isConnectionPending() { + return false; + } + + /** new in Java 7 */ + public SocketAddress getRemoteAddress() { + return _socket.getRemoteSocketAddress(); + } + + /** new in Java 7 */ + public SocketChannel shutdownInput() throws IOException { + _socket.getInputStream().close(); + return this; + } + + /** new in Java 7 */ + public SocketChannel shutdownOutput() throws IOException { + _socket.getOutputStream().close(); + return this; + } + + /** requires Java 7 */ + public SocketChannel setOption(SocketOption name, T value) { + return this; + } + + /** requires Java 7 */ + public SocketChannel bind(SocketAddress local) { + throw new UnsupportedOperationException(); + } + + //// SocketChannel abstract methods + + public int read(ByteBuffer src) throws IOException { + if (!src.hasArray()) + throw new UnsupportedOperationException(); + int pos = src.position(); + int len = src.remaining(); + int read = _socket.getInputStream().read(src.array(), src.arrayOffset() + pos, len); + if (read > 0) + src.position(pos + read); + return read; + } + + public long read(ByteBuffer[] srcs, int offset, int length) { + throw new UnsupportedOperationException(); + } + + public int write(ByteBuffer src) throws IOException { + if (!src.hasArray()) + throw new UnsupportedOperationException(); + int pos = src.position(); + int len = src.remaining(); + _socket.getOutputStream().write(src.array(), src.arrayOffset() + pos, len); + src.position(pos + len); + return len; + } + + public long write(ByteBuffer[] srcs, int offset, int length) { + throw new UnsupportedOperationException(); + } + + //// AbstractSelectableChannel abstract methods + + public void implCloseSelectableChannel() throws IOException { + _socket.close(); + } + + public void implConfigureBlocking(boolean block) throws IOException { + if (!block) + throw new UnsupportedOperationException(); + } + + + //// NetworkChannel interface methods + + public SocketAddress getLocalAddress() { + return _socket.getLocalSocketAddress(); + } + + public T getOption(SocketOption name) { + return null; + } + + public Set> supportedOptions() { + return Collections.emptySet(); + } + + + +}