title: How can I set a timeout against a BufferedReader based upon a URLConnection in Java?
body: <p><strong>I want to read the contents of a URL but don't want to "hang" if the URL is unresponsive.  I've created a BufferedReader using the URL...</strong></p>
  
  <pre><code>URL theURL = new URL(url);
  URLConnection urlConn = theURL.openConnection();
  urlConn.setDoOutput(true);
  BufferedReader urlReader = new BufferedReader(newInputStreamReader(urlConn.getInputStream()));
  </code></pre>
  
  <p><strong>...and then begun the loop to read the contents...</strong></p>
  
  <pre><code>do
      {
      buf = urlReader.readLine();
      if (buf != null)
          {
          resultBuffer.append(buf);
          resultBuffer.append("\n");
          }
      }
  while (buf != null);
  </code></pre>
  
  <p><strong>...but if the read hangs then the application hangs.</strong></p>
  
  <p><strong>Is there a way, without grinding the code down to the socket level, to "time out" the read if necessary?</strong></p>