I've been playing with NIO for network stuff to see if/how I want to update the code for Head First Java 3rd Edition. The original code used Sockets and Readers/Writers, and I thought there must be a more "modern" way to do this so I've tried updating it a few different ways. I got it working using SocketChannel
and ByteBuffer
(eventually!), but the ByteBuffer stuff is just too fiddly to use to teach people basic network IO in Java. After turning to Twitter for the answers (Twitter Driven Development) I had what looked like a promising hybrid approach.
As an #OpenJDK NIO Contributor I can tell you that NIO, even without async, is never simple. In most cases, using Channels.* factory methods for streams should be sufficient for most cases -- unless you write heavily I/O-bound code. They allow you to do NIO but it feels like IO.
— ☕ Head Crashing Informatics | Party On! 🤘 (@mkarg) December 20, 2021
However, I've run into a problem with using the Channels
factory methods to provide this nice bridge between NIO (SocketChannels
) and classic streaming IO.
I'm posting the code here because you can't post source code this long on Twitter! Maybe someone can help me.
Continue reading "“Simple” Network Connections with Java – a problem"