What is the difference between Tomcat’s BIO Connector and NIO Connector?

NIO and Comet are completely unrelated: you can mix-and-match them. Using the NIO (or APR for that matter) connector allows you to handle more requests with fewer threads due to the threading model. See http://tomcat.apache.org/tomcat-7.0-doc/config/http.html#Connector_Comparison for a comparison between the Connectors. Comet (and Websocket) have a completely different dispatch model which requires a different application … Read more

Java: Converting String to and from ByteBuffer and associated problems

Check out the CharsetEncoder and CharsetDecoder API descriptions – You should follow a specific sequence of method calls to avoid this problem. For example, for CharsetEncoder: Reset the encoder via the reset method, unless it has not been used before; Invoke the encode method zero or more times, as long as additional input may be … Read more

How to send and receive serialized object in socket channel

Your SocketChannel handling seems to be incomplete, see this complete example for SocketChannels transferring a byte: /* * Writer */ import java.io.IOException; import java.io.ObjectOutputStream; import java.net.InetSocketAddress; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; public class Sender { public static void main(String[] args) throws IOException { System.out.println(“Sender Start”); ServerSocketChannel ssChannel = ServerSocketChannel.open(); ssChannel.configureBlocking(true); int port = 12345; ssChannel.socket().bind(new InetSocketAddress(port)); … Read more

Wrapping a ByteBuffer with an InputStream

There seem to be some bugs with the implementation referred to by Thilo, and also copy and pasted on other sites verbatim: ByteBufferBackedInputStream.read() returns a sign extended int representation of the byte it reads, which is wrong (value should be in range [-1..255]) ByteBufferBackedInputStream.read(byte[], int, int) does not return -1 when there are no bytes … Read more