Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ommitted: asynchronousSocketChannel.open, connect ...
- eventBus.registerHandler(address, new Handler<Message<JsonObject>>() {
- @Override
- public void handle(final Message<JsonObject> event) {
- final ByteBuffer receivingBuffer = ByteBuffer.allocateDirect(2048);
- final ByteBuffer sendingBuffer = ByteBuffer.wrap("Foo".getBytes());
- asynchronousSocketChannel.write(sendingBuffer, 0L, new CompletionHandler<Integer, Long>() {
- public void completed(final Integer result, final Long attachment) {
- if (sendingBuffer.hasRemaining()) {
- long newFilePosition = attachment + result;
- asynchronousSocketChannel.write(sendingBuffer, newFilePosition, this);
- }
- asynchronousSocketChannel.read(receivingBuffer, 0L, new CompletionHandler<Integer, Long>() {
- CharBuffer charBuffer = null;
- final Charset charset = Charset.defaultCharset();
- final CharsetDecoder decoder = charset.newDecoder();
- public void completed(final Integer result, final Long attachment) {
- if (result > 0) {
- long p = attachment + result;
- asynchronousSocketChannel.read(receivingBuffer, p, this);
- }
- receivingBuffer.flip();
- try {
- charBuffer = decoder.decode(receivingBuffer);
- event.reply(charBuffer.toString()); // pseudo code
- } catch (CharacterCodingException e) { }
- }
- public void failed(final Throwable exc, final Long attachment) { }
- });
- }
- public void failed(final Throwable exc, final Long attachment) { }
- });
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement