I have an application that synchronously reads and writes lines of text using a BufferedReader
and a PrintStream
wrapping the InputStream
and OutputStream
of a java.net.Socket
object. So, I can just use the methods BufferedReader.readLine()
and PrintStream.println()
and let the Java library split the input into lines and format the output for me.
Now I want to replace this synchronous IO with asynchronous IO. So I have been looking into AsynchronousSocketChannel
which allows to read and write bytes asynchronously. Now, I would like to have wrapper classes so that I can asynchronously read / write lines using strings.
I cannot find such wrapper classes in the Java library. Before I write my own implementation, I wanted to ask if there are any other libraries that allow to wrap AsynchronousSocketChannel
and provide asynchronous text IO.
You can do something like this
public void nioAsyncParse(AsynchronousSocketChannel channel, final int bufferSize) throws IOException, ParseException, InterruptedException {
ByteBuffer byteBuffer = ByteBuffer.allocate(bufferSize);
BufferConsumer consumer = new BufferConsumer(byteBuffer, bufferSize);
channel.read(consumer.buffer(), 0l, channel, consumer);
}
class BufferConsumer implements CompletionHandler<Integer, AsynchronousSocketChannel> {
private ByteBuffer bytes;
private StringBuffer chars;
private int limit;
private long position;
private DateFormat frmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public BufferConsumer(ByteBuffer byteBuffer, int bufferSize) {
bytes = byteBuffer;
chars = new StringBuffer(bufferSize);
frmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
limit = bufferSize;
position = 0l;
}
public ByteBuffer buffer() {
return bytes;
}
@Override
public synchronized void completed(Integer result, AsynchronousSocketChannel channel) {
if (result!=-1) {
bytes.flip();
final int len = bytes.limit();
int i = 0;
try {
for (i = 0; i < len; i++) {
byte by = bytes.get();
if (by=='\n') {
// ***
// The code used to process the line goes here
// ***
chars.setLength(0);
}
else {
chars.append((char) by);
}
}
}
catch (Exception x) {
System.out.println("Caught exception " + x.getClass().getName() + " " + x.getMessage() + " i=" + String.valueOf(i) + ", limit=" + String.valueOf(len) + ", position="+String.valueOf(position));
}
if (len==limit) {
bytes.clear();
position += len;
channel.read(bytes, position, channel, this);
}
else {
try {
channel.close();
}
catch (IOException e) { }
bytes.clear();
buffers.add(bytes);
}
}
else {
try {
channel.close();
}
catch (IOException e) { }
bytes.clear();
buffers.add(bytes);
}
}
@Override
public void failed(Throwable e, AsynchronousSocketChannel channel) {
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With