title: Java nio partial read
body: <p>My goal is to send different kind of messages from client to server, and it will be text based. The thing I am uncertain of is how to del with partial reads here. I will have to be sure that I get a whole message and nothing more.</p>
  
  <p>Do anyone have experience with that?</p>
  
  <p>Here is what I have so far:</p>
  
  <pre><code>    private void handleNewClientMessage(SelectionKey key) throws IOException {
      SocketChannel sendingChannel = (SocketChannel) key.channel();
  
      ByteBuffer receivingBuffer = ByteBuffer.allocate(2048);
      int bytesRead = sendingChannel.read(receivingBuffer);
      if (bytesRead &gt; 0) {
          receivingBuffer.flip();
  
          byte[] array = new byte[receivingBuffer.limit()];
          receivingBuffer.get(array);
          String message = new String(array);
  
          System.out.println("Server received "  +message);           
      }
      selector.wakeup();
  }
  </code></pre>
  
  <p>But I have no way of "ending" the message and be certain to have one full message.</p>
  
  <p>Best regards,
  O</p>