title: InputStream skips bytes
body: <p>For the second time I have this extremely anoying problem with an InputStream.
  This InputStream belongs to a Socket that is supposed to receive an image. The code for reading this image is as below:</p>
  
  <pre><code>InputStream input = socket.getInputStream();
  
  InputStreamReader reader = new InputStreamReader(input);
  BufferedReader bufferedReader = new BufferedReader(reader);
  int total = Integer.parseInt(bufferedReader.readLine());
  int bytesRead = 0;
  byte[] buffer = new byte[total]; // total is the total size of the image
  
  while (bytesRead &lt; total) {
      int next = input.read(buffer, bytesRead, total-bytesRead);
      if (next &gt; 0) {
          bytesRead += next;
          System.out.println("Read: " + bytesRead);
      }
  }
  </code></pre>
  
  <p>Now the strange thing is that this code skips the first 1182 bytes of the image, and then reads the remaining part. So when the <code>total</code> size is 15000 bytes, it reads byte 1182-15000. </p>
  
  <p>I checked Wireshark and the whole image is transmitted. The code throws no exceptions. <code>input.read()</code> returns -1 as usual.</p>
  
  <p>Pervious data has been readed from the stream using a <code>BufferedReader</code>. This data is only 5 characters long so it can't contain the missing 1K, but my guess is that the BufferedReader.readLine() method reads (buffers) more bytes from the InputStream than needed. Could this be correct?</p>
  
  <p>I've had the same problem a few months ago but I absolutely have no clue on how I solved it.</p>
  
  <p>Hope anyone can help.</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>EDIT:</strong> I can solve the problem by adding a 100 ms sleep between sending the image size and the image data. It solves the problem but I would still realy like to know a more appropriate solution</p>