Java:用于读写行的异步I / O通道

时间:2016-05-31 12:02:33

标签: java asynchronous text io

我有一个应用程序,使用WM_PAINT: Draw "Hello, Windows 32 API!" Draw Mouse_State WM_MOUSEDOWN: Mouse_State = "L-BUTTON-DOWN" InvalidateRect(client_area) WM_MOUSEUP: Mouse_State = "L-BUTTON-UP" InvalidateRect(client_area) BufferedReader包裹PrintStreamInputStream OutputStream对象来同步读取和写入文本行。所以,我可以使用方法java.net.SocketBufferedReader.readLine(),让Java库将输入分成行并为我格式化输出。

现在我想用异步IO替换这个同步IO。所以我一直在研究PrintStream.println(),它允许异步读写字节。现在,我想有包装类,以便我可以使用字符串异步读/写行。

我在Java库中找不到这样的包装类。在编写自己的实现之前,我想询问是否有其他库允许包装AsynchronousSocketChannel并提供异步文本IO。

1 个答案:

答案 0 :(得分:1)

你可以做这样的事情

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) {
        }
};