title: URLConnection inputStream not being read fully
body: <p>I wrote the following code for downloading some files from a server but the problem is that this code isn't reading the complete response (inputStream). File size is 7.5 MB while I am getting 5.5 MB each time and of course adobe reader complains that file is damaged. Here is the code</p>
  
  <pre><code>import java.net.URLConnection;
  public class Downloader {
  URL url;
  public Downloader(){
      try {
  
          url = new URL("https://d396qusza40orc.cloudfront.net/algs4partI/slides%2F13StacksAndQueues.pdf");
          FileOutputStream outStream;
          ObjectOutputStream oStream;
          try {
              URLConnection con = url.openConnection();
              InputStream inStream = con.getInputStream();
              outStream = new FileOutputStream("data.pdf");
              oStream = new ObjectOutputStream(outStream);
              int bytesRead;
              int totalBytesRead = 0;
              byte[] buffer = new byte[100000];
              while((bytesRead = inStream.read(buffer)) &gt; 0){
  
                  //outStream.write(buffer, 0 , bytesRead);
                  oStream.write(buffer, 0, bytesRead);
                  buffer = new byte[100000];
                  totalBytesRead += bytesRead;
              }
              System.out.println("Total Bytes read are = " + totalBytesRead);
              oStream.close();
              outStream.close();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      } catch (MalformedURLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
  }
  public static void main(String[] args){
      Downloader d = new Downloader();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Any ideas what I am doing wrong here? Thanks in advance.</p>