Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  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. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement