diff --git a/src/test/java/tlschannel/example/AsynchronousChannelServer.java b/src/test/java/tlschannel/example/AsynchronousChannelServer.java index 44abb9a9..df751c06 100644 --- a/src/test/java/tlschannel/example/AsynchronousChannelServer.java +++ b/src/test/java/tlschannel/example/AsynchronousChannelServer.java @@ -6,7 +6,6 @@ import java.nio.channels.CompletionHandler; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; -import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.security.GeneralSecurityException; import javax.net.ssl.SSLContext; @@ -27,8 +26,6 @@ */ public class AsynchronousChannelServer { - private static final Charset utf8 = StandardCharsets.UTF_8; - public static void main(String[] args) throws IOException, GeneralSecurityException { // initialize the SSLContext, a configuration holder, reusable object @@ -48,15 +45,13 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept SocketChannel rawChannel = serverSocket.accept(); rawChannel.configureBlocking(false); - // create TlsChannel builder, combining the raw channel and the SSLEngine, using minimal - // options + // create TlsChannel builder, combining the raw channel and the SSLEngine, using minimal options ServerTlsChannel.Builder builder = ServerTlsChannel.newBuilder(rawChannel, sslContext); // instantiate TlsChannel TlsChannel tlsChannel = builder.build(); - // build asynchronous channel, based in the TLS channel and associated with the global - // group. + // build asynchronous channel, based in the TLS channel and associated with the global group. AsynchronousTlsChannel asyncTlsChannel = new AsynchronousTlsChannel(channelGroup, tlsChannel, rawChannel); @@ -67,7 +62,7 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept public void completed(Integer result, Object attachment) { if (result != -1) { res.flip(); - System.out.print(utf8.decode(res)); + System.out.print(StandardCharsets.UTF_8.decode(res)); res.compact(); // repeat asyncTlsChannel.read(res, null, this); diff --git a/src/test/java/tlschannel/example/NonBlockingClient.java b/src/test/java/tlschannel/example/NonBlockingClient.java index e4bf500a..ecfd33e8 100644 --- a/src/test/java/tlschannel/example/NonBlockingClient.java +++ b/src/test/java/tlschannel/example/NonBlockingClient.java @@ -6,7 +6,6 @@ import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.SocketChannel; -import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.security.GeneralSecurityException; import java.util.Iterator; @@ -19,10 +18,7 @@ /** Client non-blocking example. Connects to a public TLS reporting service. */ public class NonBlockingClient { - private static final Charset utf8 = StandardCharsets.UTF_8; - public static void main(String[] args) throws IOException, GeneralSecurityException { - ByteBuffer requestBuffer = ByteBuffer.wrap(SimpleBlockingClient.httpLine.getBytes(StandardCharsets.US_ASCII)); ByteBuffer responseBuffer = ByteBuffer.allocate(1000); // use small buffer to cause selector loops boolean requestSent = false; @@ -40,13 +36,11 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept // Note that the raw channel (and not the wrapped one) is registered in the selector rawChannel.register(selector, SelectionKey.OP_CONNECT); - // create TlsChannel builder, combining the raw channel and the SSLEngine, using minimal - // options + // create TlsChannel builder, combining the raw channel and the SSLEngine, using minimal options ClientTlsChannel.Builder builder = ClientTlsChannel.newBuilder(rawChannel, sslContext); // instantiate TlsChannel - TlsChannel tlsChannel = builder.build(); - try { + try (TlsChannel tlsChannel = builder.build()) { mainLoop: while (true) { @@ -55,18 +49,14 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept Iterator iterator = selector.selectedKeys().iterator(); while (iterator.hasNext()) { - SelectionKey key = iterator.next(); iterator.remove(); - if (key.isConnectable()) { - if (rawChannel.finishConnect()) { // the channel is registered for writing, because TLS connections are initiated by // clients. rawChannel.register(selector, SelectionKey.OP_WRITE); } - } else if (key.isReadable() || key.isWritable()) { try { if (!requestSent) { @@ -80,7 +70,7 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept int c = tlsChannel.read(responseBuffer); if (c > 0) { responseBuffer.flip(); - System.out.print(utf8.decode(responseBuffer)); + System.out.print(StandardCharsets.UTF_8.decode(responseBuffer)); responseBuffer.compact(); } if (c < 0) { @@ -93,14 +83,11 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept } catch (NeedsWriteException e) { key.interestOps(SelectionKey.OP_WRITE); // overwrites previous value } - } else { throw new IllegalStateException(); } } } - } finally { - tlsChannel.close(); } } } diff --git a/src/test/java/tlschannel/example/NonBlockingServer.java b/src/test/java/tlschannel/example/NonBlockingServer.java index 3abceae5..9148f4b7 100644 --- a/src/test/java/tlschannel/example/NonBlockingServer.java +++ b/src/test/java/tlschannel/example/NonBlockingServer.java @@ -7,7 +7,6 @@ import java.nio.channels.Selector; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; -import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.security.GeneralSecurityException; import java.util.Iterator; @@ -35,8 +34,6 @@ */ public class NonBlockingServer { - private static final Charset utf8 = StandardCharsets.UTF_8; - public static void main(String[] args) throws IOException, GeneralSecurityException { // initialize the SSLContext, a configuration holder, reusable object @@ -48,7 +45,6 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept serverSocket.configureBlocking(false); Selector selector = Selector.open(); serverSocket.register(selector, SelectionKey.OP_ACCEPT); - while (true) { // loop blocks here @@ -56,12 +52,9 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept Iterator iterator = selector.selectedKeys().iterator(); while (iterator.hasNext()) { - SelectionKey key = iterator.next(); iterator.remove(); - if (key.isAcceptable()) { - // we have a new connection ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel(); @@ -79,9 +72,7 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept // reading, because TLS connections are initiated by clients. SelectionKey newKey = rawChannel.register(selector, SelectionKey.OP_READ); newKey.attach(tlsChannel); - } else if (key.isReadable() || key.isWritable()) { - // we have data (or buffer space) in existing connections ByteBuffer buffer = ByteBuffer.allocate(10000); @@ -93,7 +84,7 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept int c = tlsChannel.read(buffer); if (c > 0) { buffer.flip(); - System.out.print(utf8.decode(buffer)); + System.out.print(StandardCharsets.UTF_8.decode(buffer)); } if (c < 0) { tlsChannel.close(); diff --git a/src/test/java/tlschannel/example/NonBlockingServerWithOffLoopTasks.java b/src/test/java/tlschannel/example/NonBlockingServerWithOffLoopTasks.java index f300be2b..b7bb649e 100644 --- a/src/test/java/tlschannel/example/NonBlockingServerWithOffLoopTasks.java +++ b/src/test/java/tlschannel/example/NonBlockingServerWithOffLoopTasks.java @@ -7,7 +7,6 @@ import java.nio.channels.Selector; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; -import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.security.GeneralSecurityException; import java.util.Iterator; @@ -37,8 +36,6 @@ */ public class NonBlockingServerWithOffLoopTasks { - private static final Charset utf8 = StandardCharsets.UTF_8; - private static final Executor taskExecutor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); private static final Set taskReadyKeys = ConcurrentHashMap.newKeySet(); @@ -54,7 +51,6 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept serverSocket.configureBlocking(false); Selector selector = Selector.open(); serverSocket.register(selector, SelectionKey.OP_ACCEPT); - while (true) { // loop blocks here @@ -119,7 +115,7 @@ private static void handleReadyChannel(Selector selector, SelectionKey key) thro int c = tlsChannel.read(buffer); if (c > 0) { buffer.flip(); - System.out.print(utf8.decode(buffer)); + System.out.print(StandardCharsets.UTF_8.decode(buffer)); } if (c < 0) { tlsChannel.close(); diff --git a/src/test/java/tlschannel/example/SimpleBlockingClient.java b/src/test/java/tlschannel/example/SimpleBlockingClient.java index 4cb03354..441903d4 100644 --- a/src/test/java/tlschannel/example/SimpleBlockingClient.java +++ b/src/test/java/tlschannel/example/SimpleBlockingClient.java @@ -4,7 +4,6 @@ import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.SocketChannel; -import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.security.NoSuchAlgorithmException; import javax.net.ssl.SSLContext; @@ -14,8 +13,6 @@ /** Client example. Connects to a public TLS reporting service. */ public class SimpleBlockingClient { - private static final Charset utf8 = StandardCharsets.UTF_8; - public static final String domain = "www.howsmyssl.com"; public static final String httpLine = "GET https://www.howsmyssl.com/a/check HTTP/1.0\nHost: www.howsmyssl.com\n\n"; @@ -28,8 +25,7 @@ public static void main(String[] args) throws IOException, NoSuchAlgorithmExcept try (SocketChannel rawChannel = SocketChannel.open()) { rawChannel.connect(new InetSocketAddress(domain, 443)); - // create TlsChannel builder, combining the raw channel and the SSLEngine, using minimal - // options + // create TlsChannel builder, combining the raw channel and the SSLEngine, using minimal options ClientTlsChannel.Builder builder = ClientTlsChannel.newBuilder(rawChannel, sslContext); // instantiate TlsChannel @@ -44,7 +40,7 @@ public static void main(String[] args) throws IOException, NoSuchAlgorithmExcept // empty } res.flip(); - System.out.println(utf8.decode(res)); + System.out.println(StandardCharsets.UTF_8.decode(res)); } } } diff --git a/src/test/java/tlschannel/example/SimpleBlockingServer.java b/src/test/java/tlschannel/example/SimpleBlockingServer.java index 5627e29f..549f0bda 100644 --- a/src/test/java/tlschannel/example/SimpleBlockingServer.java +++ b/src/test/java/tlschannel/example/SimpleBlockingServer.java @@ -5,7 +5,6 @@ import java.nio.ByteBuffer; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; -import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.security.GeneralSecurityException; import javax.net.ssl.SSLContext; @@ -21,8 +20,6 @@ */ public class SimpleBlockingServer { - private static final Charset utf8 = StandardCharsets.UTF_8; - public static void main(String[] args) throws IOException, GeneralSecurityException { // initialize the SSLContext, a configuration holder, reusable object @@ -36,8 +33,7 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept System.out.println("Waiting for connection..."); try (SocketChannel rawChannel = serverSocket.accept()) { - // create TlsChannel builder, combining the raw channel and the SSLEngine, using minimal - // options + // create TlsChannel builder, combining the raw channel and the SSLEngine, using minimal options ServerTlsChannel.Builder builder = ServerTlsChannel.newBuilder(rawChannel, sslContext); // instantiate TlsChannel @@ -47,7 +43,7 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept ByteBuffer res = ByteBuffer.allocate(10000); while (tlsChannel.read(res) != -1) { res.flip(); - System.out.print(utf8.decode(res)); + System.out.print(StandardCharsets.UTF_8.decode(res)); res.compact(); } } diff --git a/src/test/java/tlschannel/example/SniBlockingServer.java b/src/test/java/tlschannel/example/SniBlockingServer.java index ac86f24d..b92398c5 100644 --- a/src/test/java/tlschannel/example/SniBlockingServer.java +++ b/src/test/java/tlschannel/example/SniBlockingServer.java @@ -5,7 +5,6 @@ import java.nio.ByteBuffer; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; -import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.security.GeneralSecurityException; import java.util.Optional; @@ -25,8 +24,6 @@ */ public class SniBlockingServer { - private static final Charset utf8 = StandardCharsets.UTF_8; - public static void main(String[] args) throws IOException, GeneralSecurityException { // initialize the SSLContext, a configuration holder, reusable object @@ -71,7 +68,7 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept ByteBuffer res = ByteBuffer.allocate(10000); while (tlsChannel.read(res) != -1) { res.flip(); - System.out.print(utf8.decode(res)); + System.out.print(StandardCharsets.UTF_8.decode(res)); res.compact(); } }