SHARE
TWEET

Untitled

a guest Feb 19th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ommitted: asynchronousSocketChannel.open, connect ...
  2.  
  3.     eventBus.registerHandler(address, new Handler<Message<JsonObject>>() {
  4.         @Override
  5.         public void handle(final Message<JsonObject> event) {
  6.             final ByteBuffer receivingBuffer = ByteBuffer.allocateDirect(2048);
  7.             final ByteBuffer sendingBuffer = ByteBuffer.wrap("Foo".getBytes());
  8.  
  9.             asynchronousSocketChannel.write(sendingBuffer, 0L, new CompletionHandler<Integer, Long>() {
  10.                 public void completed(final Integer result, final Long attachment) {
  11.                     if (sendingBuffer.hasRemaining()) {
  12.                         long newFilePosition = attachment + result;
  13.                         asynchronousSocketChannel.write(sendingBuffer, newFilePosition, this);
  14.                     }
  15.  
  16.                     asynchronousSocketChannel.read(receivingBuffer, 0L, new CompletionHandler<Integer, Long>() {
  17.                         CharBuffer charBuffer = null;
  18.                         final Charset charset = Charset.defaultCharset();
  19.                         final CharsetDecoder decoder = charset.newDecoder();
  20.  
  21.                         public void completed(final Integer result, final Long attachment) {
  22.                             if (result > 0) {
  23.                                 long p = attachment + result;
  24.                                 asynchronousSocketChannel.read(receivingBuffer, p, this);
  25.                             }
  26.  
  27.                             receivingBuffer.flip();
  28.  
  29.                             try {
  30.                                 charBuffer = decoder.decode(receivingBuffer);
  31.                                 event.reply(charBuffer.toString()); // pseudo code
  32.                             } catch (CharacterCodingException e) { }
  33.  
  34.  
  35.                         }
  36.  
  37.                         public void failed(final Throwable exc, final Long attachment) { }
  38.                     });
  39.                 }
  40.  
  41.                 public void failed(final Throwable exc, final Long attachment) { }
  42.             });
  43.         }
  44.     });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top