title: Java NIO: Read data from Channel which does not contain any data. How do I handle this situation?
body: <p>Java code below:</p>
  
  <pre><code>import java.io.IOException;
  import java.net.InetSocketAddress;
  import java.nio.ByteBuffer;
  
  import java.nio.channels.SocketChannel;
  
  public class Test {
  
      public static void main(String[] args) throws IOException {
  
          SocketChannel channel = SocketChannel.open(new InetSocketAddress(
                  "google.com", 80));
  
          ByteBuffer buffer = ByteBuffer.allocate(1024);
  
          while ((channel.read(buffer)) != -1) {
  
              buffer.clear();
          }
  
          channel.close();
      }
  }
  </code></pre>
  
  <p>This code is easy.</p>
  
  <p>But I did not write any data to Channel, so, it does not contain any data to reading.</p>
  
  <p>In this case method <code>channel.read()</code> performed <strong>too long</strong> and do not return any data.</p>
  
  <p>How do I handle this situation?</p>
  
  <p>Thanks.</p>